word文档中如何替换文字.急!急! ( 积分: 50 )

  • 主题发起人 主题发起人 bigfacefox1
  • 开始时间 开始时间
B

bigfacefox1

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高人:
   本人遇到一个问题如何替换WORD文档中的指定文字,请指教.
 
各位高人:
   本人遇到一个问题如何替换WORD文档中的指定文字,请指教.
 
我以前用VB.NET 写的代码
Public Sub ReplaceWord(ByVal SourceWord As String, ByVal DestWord As String)
Dim Selection As Word.Selection = _WordApp.Selection
Selection.Find.ClearFormatting()
Selection.Find.Replacement.ClearFormatting()
With Selection.Find
.Text = SourceWord
.Replacement.Text = DestWord
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End Sub
 
需更多的帮助,你可以在word中录制一个宏,然后参看其代码.
下面就是一个文字替换的宏的vba代码.
Sub Macro2()
'
' Macro2 Macro
' 宏在 2005-3-4 由 smart01 录制
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "aaaa"
.Replacement.Text = "bbbb"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
楼上两位都给出了答案,如果文字是固定的,则可以定义书签的方式解决,在word里定义好书签,在delphi里使用vba语言寻找书签,替换。主要是楼上的办法,现录制宏,然后查看宏语法。然后在delphi里使用。
 
OLE.OleObject.Application.ActiveDocument.Content.Find.Execute('%JGMC%', False,
True, False, False, False, True,wdFindStop, False, '测试机构名称', wdReplaceAll);
OLE.OleObject.Application.ActiveDocument.Content.Find.Execute('%AAAA%', False,
True, False, False, False, True,wdFindStop, False, '020-86695326', wdReplaceAll);
OLE.SetFocus;
OLE.UpdateObject;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3009328
这个贴子里我贴上去过一些资料,太长,我就不重复了,你看看吧
 
后退
顶部