我尝试使用 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] 删除。
我来说两句