如何使用Interop在.docx(Microsoft Word文件)中查找和替换文本

huangapple go评论79阅读模式
英文:

how can i find and replace text in .docx(Microsoft word file) using Interop

问题

我想打开一个docx文件,并替换我想要的特定单词。
例如,我想把A改成B,对于文档中的所有文本...

我对此一无所知... 请帮帮我... 谢谢

英文:

enter image description here
i wanna open a docx file and replacing a specific word that i want.
for example i wanna change A with B for all the texts inside the word file..

i have no idea for this... please help me.. thank you

答案1

得分: 1

你已经完成了一半。在你的示例中,你打开了文档:

Document docs = word.Documents.Open("path/file.docx");

现在我们需要替换文本:

docs.Content.Find.Execute(FindText: "oldtext", ReplaceWith: "newtext", Replace: WdReplace.wdReplaceAll, Missing: System.Reflection.Missing.Value);

docs.Save();
docs.Close();
wordApp.Quit();
英文:

Hey you are already halfway there. In your example you opend the doc

Document docs = word.Documents.Open("path/file.docx");

Now we need to replace the text

docs.Content.Find.Execute(FindText: "oldtext", ReplaceWith: "newtext", Replace: WdReplace.wdReplaceAll, Missing: System.Reflection.Missing.Value);

docs.Save();
docs.Close();
wordApp.Quit();

huangapple
  • 本文由 发表于 2023年7月18日 03:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定