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?
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...";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With