问题
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?
回答1:
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...";
回答2:
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.
回答3:
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).
来源:https://stackoverflow.com/questions/19533877/error-string-parameter-too-long-at-microsoft-office-interop-word-find-execute