OleVarient (type used to pass your parameters to COM server of MS Word) has length limit of 255 charaters. To wordaround your case, you can try the following method:
searchAndReplaceLongTextInStory(wordApp.Selection, strSearch, strReplace);
/*********************************/
private void searchAndReplaceLongTextInStory(Word.Range rngStory, string strSearch, string strReplace, Word.WdFindWrap wrap = Word.WdFindWrap.wdFindStop)
{
List<string> subs = new List<string>();
int counter = 0;
while (counter <= strSearch.Length)
{
if (strSearch.Length < counter + 250)
{
subs.Add(strSearch.Substring(counter, strSearch.Length - counter));
}
else
{
subs.Add(strSearch.Substring(counter, 250) + "#r#");
}
counter += 250;
}
rngStory.Find.ClearFormatting();
rngStory.Find.Replacement.ClearFormatting();
rngStory.Find.Text = strSearch;
rngStory.Find.Wrap = wrap;
rngStory.Find.Replacement.Text = subs[0];
rngStory.Find.Execute(Forward: true, Replace: Word.WdReplace.wdReplaceAll);
rngStory.Find.Text = "#r#";
for (int i = 1; i < subs.Count; i++)
{
rngStory.Find.Replacement.Text = subs[i];
rngStory.Find.Execute(Forward: true, Replace: Word.WdReplace.wdReplaceAll);
}
}
Note that I just coded it from my mind modifying other's example, you may have to debug before using it, but you get the idea.
No comments:
Post a Comment