The error "string is too long." at microsoft.office.interop.word.find.execute

I want to create a world document using C #. So this is my code to replace document document variables.

 private void FindAndReplace(Microsoft.Office.Interop.Word.Application WordApp, object findText, object replaceWithText) 
 {
    try {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        WordApp.Selection.Find.Execute(ref findText,
        ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike,
        ref nmatchAllWordForms, ref forward,
        ref wrap, ref format, ref replaceWithText,
        ref replace, ref matchKashida,
        ref matchDiacritics, ref matchAlefHamza,
        ref matchControl);
    } catch (Exception error) {
        lblerror.Visible = true;
        lblerror.Text = error.ToString();
    }
}

but here, if "replaceWithText" is too lonely, there is an error and it says

String parameter too long.

So how can I replace a long string?

+4
source share
3 answers

Instead of replacing with Find.Execute (): find the text, get its position, insert new text. This will not limit you to the length of the new line.

An example of replacing a specific text

// Find text 
Range range = doc.Content;
range.Find.Execute(findText);
range.Text = "new text...";

Example of adding new text after specific text

// Find text 
Range range = doc.Content;
range.Find.Execute(findText);
// Define new range 
range = doc.Range(range.End + 1, range.End + 1);
range.Text = "new text...";
+3
source

, , , , , , - .

- , .

FindAndReplace(word, replacementKey, SequentialReplaceToken);
var restOfText = replaceWithText;
while (restOfText.Length > 20)
{
    var firstTwentyChars = restOfText.Substring(0, 20);
    firstTwentyChars += SequentialReplaceToken;
    restOfText = restOfText.Substring(20);
    FindAndReplace(word, SequentialReplaceToken, firstTwentyChars);
}
FindAndReplace(word, SequentialReplaceToken, restOfText);

FindAndReplace (...) Word. :

private void FindAndReplace(Application doc, object findText, object replaceWithText)
{
    //options
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;
    //clear previous formatting
    doc.Selection.Find.ClearFormatting();
    //execute find and replace
    doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
        ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}

SequentialReplaceToken , , , .

+1

, . , .. ( ) FindAndReplace (..) ( ).

0

All Articles