C# Microsoft.Office.Interop.Word 替换为 Selection.Find.Execute() 超过第一个单词

用户3883648

我尝试使用 Microsoft.Office.Interop.Word 在 C# 中制作报告。我有一个模板和一些我想替换的不同单词。但如果我使用

> Selection.Find.Execute()

它只替换第一个单词。我的代码:

 Word.Application app = new Word.Application();
            Word.Document doc = null;

            object template = "ReportTest.dotx";
            object toFile = "ReportFinish.doc";

            object missing = System.Reflection.Missing.Value;

            doc = app.Documents.Open(template, missing, missing);
            app.Selection.Find.ClearFormatting();
            app.Selection.Find.Replacement.ClearFormatting();

            app.Selection.Find.Execute("[organisation]", missing, missing, missing, missing, missing, missing, missing,
                missing, "Firedepartment");
            app.Selection.Find.Execute("[trainer]", missing, missing, missing, missing, missing, missing, missing,
                missing, "John Silver");

            app.Selection.Find.Execute("[trainee]", missing, missing, missing, missing, missing, missing, missing,
                missing, "Smith");

            object Save = (object) toFile;
            doc.SaveAs(Save,missing,missing,missing);

            doc.Close(false,missing,missing);
            app.Quit(false,false,false);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

所以如果它成立了组织,它会用 Firedepartment 取代它,但另一个没有改变。那么有没有办法替换所有不同的单词?

单词模板看起来像:

Department: [organisation] 
Trainer: [trainer] 
Trainee: [trainee]`

完成文件应如下所示: 部门:Firedepartment 培训师:John Silver 实习生:Smith

谢谢你的帮助

网络魔法师

使用文档Selection.Find

 When the item is found that matches the search criteria, it is automatically selected.

我会打电话给

app.Selection.Collapse();

就在第二次和第三次调用之前Find.Execute

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用f#Microsoft.Office.Interop.Word搜索和替换

C# Microsoft.Office.Interop.Word 保护段落

使用参考C#添加Microsoft.Office.InterOp.Word

使用带有Office.Interop.Word的C#将文本替换为多行

将一个文档插入另一个 Microsoft.Office.Interop.Word

Microsoft.Office.Interop.Word OMath

如何通过 Microsoft.Office.Interop.Word 在 wdRevisionsViewFinal 中将 word 转换为 html?

Microsoft.Office.Interop.Word中仅可访问的第一页

如何使用 Microsoft.Office.Interop.Word 在 C# 中编辑 docx 文件

Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition出现C#编译器错误;

Microsoft.Office.Interop.Word:在C#中为图像添加标题

C#Microsoft.Office.Interop.Word SpellingChecked不返回真实值

带有C#的Microsoft.Office.Interop.Word-标识现有目录对象

无法使用microsoft.office.interop.word在当前一字之后插入图像

在VBScript中使用Microsoft.Office.Interop.Word枚举常量

使用Microsoft.Office.Interop Word和Excel

Microsoft.Interop.Office.Word完整样式不适用

C#-Microsoft.Office.Interop.Excel.Sheets到Microsoft.Office.Interop.Excel.Worksheet []

在没有Microsoft.Office.Interop的.NET Core中将Word doc和docx格式转换为PDF

如何使用 Microsoft Word Interop Bookmarks C# 选中一个框

性能-Microsoft.Office.Interop.Outlook.Items.Find()与LINQ

使用Microsoft.Office.Interop.Word启用内置的Microsoft Word按钮控件

使用Microsoft.Office.Interop.Word在每行中加粗一部分字符串/范围

C#使用Microsoft.Office.Interop.Excel读取数据

C#-TableOfContent条目的Office Interop Word格式缩进

机器中的单独Microsoft.Office.Interop.Word实例和Word应用程序

是否可以使用Microsoft.Office.Interop.Word以编程方式从Word文档中删除行号?

Microsoft.Office.Interop.Word - Word 在尝试打开文件时遇到错误

无法将类型为“ microsoft.Office.Interop.Excel.ApplicationClass”的COM对象转换为“ microsoft.Office.Interop.Excel.Application”