Delphi 如何替换Word文档中的文本

2022-12-27 14:33:11 作者:admin

本文整理自网络,侵删。

 use ActiveX, ComObj;
const   wdFindContinue = 1;  wdReplaceOne = 1;  wdReplaceAll = 2;
var   WordApp: Variant;
begin  // create OLE object for MS Word application:  WordApp := CreateOLEObject('Word.Application');  // load a document from your file  WordApp.Documents.Open(yourDocFileName);  WordApp.Selection.Find.ClearFormatting;  WordApp.Selection.Find.Text := yourStringForSearch;  WordApp.Selection.Find.Replacement.Text := yourNewStringForReplace;  WordApp.Selection.Find.Forward := True;  WordApp.Selection.Find.MatchAllWordForms := False;  WordApp.Selection.Find.MatchCase := Flase;  WordApp.Selection.Find.MatchWildcards := False;  WordApp.Selection.Find.MatchSoundsLike := False;  WordApp.Selection.Find.MatchWholeWord := False;  WordApp.Selection.Find.MatchFuzzy := False;  WordApp.Selection.Find.Wrap := wdFindContinue;   WordApp.Selection.Find.Format := False;    WordApp.Selection.Find.Execute(replace := wdReplaceAll)end;
//要替换首次出现的文字,请使用WordApp.Selection.Find.Execute(replace := wdReplaceOne);替换WordApp.Selection.Find.Execute(replace := wdReplaceAll)
//要检查是否找到了文本,请使用Found方法:if WordApp.Selection.Find.Found then  {do something}Save the modified document with:
WordApp.ActiveDocument.SaveAs(yourDocFileName);
//最后,使用以下命令关闭MS Word实例:WordApp.ActiveDocument.Close;WordApp.Quit;WordApp := Unassigned;注意:如果要更改字体而不是文本,请使用WordApp.Selection.Find.Replacement的Font 属性而不是 Text。

相关阅读 >>

Delphi 只限制到4位小数级别

Delphi 如何检查字符串是否为数字

阻止删除文件(文件占坑)的Delphi代码

Delphi winsock api写的邮件发送单元

Delphi 获取打印机纸型的例子

Delphi 控制台程序获取系统信息

Delphi打印各类文档

Delphi侦测按键输入

Delphi 根据快捷方式路径取源文件地址

Delphi实现二分查找

更多相关阅读请进入《Delphi》频道 >>



在线咨询 拨打电话