7

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
aruni
  • 2,664
  • 10
  • 44
  • 68
  • 2
    It is a horrible idea to use Office Interop from ASP.NET or another server technology. These APIs were written for use in a desktop application, for automating Office (a suite of desktop applications). Server applications are different in many ways that make it a very, very bad idea to use Office Interop in them. It's also unsupported by Microsoft, and may violate your Office license. See [Considerations for server-side Automation of Office](http://support.microsoft.com/kb/257757) – John Saunders Oct 23 '13 at 06:18

4 Answers4

8

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...";
user2316116
  • 6,726
  • 1
  • 21
  • 35
1

I recognize that this is a year old but since the technical side of the question never got answered I figured I take the time to post this for who ever stumbles upon it.

You could do something like this of you really must do it this way.

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(...) being a wrapper over the Word interop Function. Like so:

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);
}

And SequentialReplaceToken being a string constant that is known to never come up in the document.

Florian
  • 26
  • 1
  • 4
  • Just to add, the limit for Find.Execute appears to be 255 so more than 20 chars could be replaced at a time if preferred. – MoonBoots89 Dec 29 '17 at 12:15
1

I have been tried this code

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

the problem is (it's replace all the doucument) i want it just to replace specific text like "textwanttoreplace" for exmaple

0

Simply split your long string into e.g. array of substrings, chars.. (whatever you want) and call your FindAndReplace(..) multiple times (for all of your newly generated array-items).

fen89
  • 539
  • 4
  • 10