Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to create a world document using C#. So this is my code for replace word 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 in here if the "replaceWithText" too lone there is error and it says

String parameter too long.

So how can I replace long string?

like image 304
aruni Avatar asked Oct 23 '13 05:10

aruni


Video Answer


1 Answers

Instead of replacing using Find.Execute(): find a text, get its position, insert new text. That would not limit you in a length of the new string.

Example to replace specific text

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

Example to add a 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...";
like image 90
user2316116 Avatar answered Sep 30 '22 14:09

user2316116