当前位置:软件学习 > Word >>

C#替换Word文本框中的内容(不使用Bookmark)

主要参考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
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,