主要参考StoryRanges,NextStoryRange,WdStoryType,Range这几个类。
Replace text in Word textbox objects using VSTO and C#
04 | /// <param name="wordApp"></param> |
05 | /// <param name="oldStr"></param> |
06 | /// <param name="newStr"></param> |
07 | public void SearchReplace( string oldStr, string newStr) |
10 | object replaceAll = WdReplace.wdReplaceAll; |
12 | wordApp.Selection.Find.ClearFormatting(); |
13 | wordApp.Selection.Find.Text = oldStr; |
15 | wordApp.Selection.Find.Replacement.ClearFormatting(); |
16 | wordApp.Selection.Find.Replacement.Text = newStr; |
18 | wordApp.Selection.Find.Execute( |
19 | ref missing, ref missing, ref missing, ref missing, ref missing, |
20 | ref missing, ref missing, ref missing, ref missing, ref missing, |
21 | ref replaceAll, ref missing, ref missing, ref missing, ref missing); |
25 | StoryRanges sr = wordDoc.StoryRanges; |
26 | foreach (Range r in sr) |
29 | if (WdStoryType.wdTextFrameStory == r.StoryType) |
33 | r1.Find.ClearFormatting(); |
34 | r1.Find.Text = oldStr; |
36 | r1.Find.Replacement.ClearFormatting(); |
37 | r1.Find.Replacement.Text = newStr; |
40 | ref missing, ref missing, ref missing, ref missing, ref missing, |
41 | ref missing, ref missing, ref missing, ref missing, ref missing, |
42 | ref replaceAll, ref missing, ref missing, ref missing, ref missing); |
44 | r1 = r1.NextStoryRange; |