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


本文整理自网络,侵删。

 
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 webservices传base64字串

Delphi combobox 只读 text属性可用的方法

Delphi 递归实现从m 个集合中 任取一个元素 生成组合

Delphi异常信息捕捉

Delphi md5加密算法

Delphi xe7开发的获取网页中字符串的编码是否是utf8

Delphi2010读取mysql数据库text类型乱码的解决方案

Delphi 关闭uac 单元

Delphi 判断是否是系统管理员身份

Delphi hexstrtostream

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



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...