主要參考StoryRanges,NextStoryRange,WdStoryType,Range這幾個類。
Replace text in Word textbox objects using VSTO and C#
01
/// <summary>
02
/// 查找並替換文本
03
/// </summary>
04
/// <param name="wordApp"></param>
05
/// <param name="oldStr"></param>
06
/// <param name="newStr"></param>
07
public
void
SearchReplace(
string
oldStr,
string
newStr)
08
{
09
#region 文字區域
10
object
replaceAll = WdReplace.wdReplaceAll;
11
12
wordApp.Selection.Find.ClearFormatting();
13
wordApp.Selection.Find.Text = oldStr;
14
15
wordApp.Selection.Find.Replacement.ClearFormatting();
16
wordApp.Selection.Find.Replacement.Text = newStr;
17
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);
22
#endregion
23
24
#region 文本框
25
StoryRanges sr = wordDoc.StoryRanges;
26