查找文本并替换文本 excel 字词 vba

find text and replace text excel word vba

我有以下代码

  Sub DocSearch()
Dim wdApp As Object, wdDoc As Object
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-F-380.1.doc")
With wdDoc.Content.Find
  .Text = "Date:"
  .Replacement.Text = "Datetest"
  .Wrap = wdFindContinue
  .Execute Replace:=wdReplaceAll
End With
Set wdApp = Nothing: Set wdDoc = Nothing
End Sub

以上工作正常,我在上面 vba 代码中添加了以下内容,但它不起作用

With wdDoc.Content.Find
  .Text = "Date:"
  .Replacement.Text = "Datetest"
   Text = "Prime Lending Rate"
  .Replacement.Text =" Repo Rate"
  .Wrap = wdFindContinue
  .Execute Replace:=wdReplaceAll

非常感谢您的帮助

谢谢

您需要:

.Execute Replace:=wdReplaceAll

在每组 F/R 表达式之间,因此:

Sub DocSearch()
Dim wdApp As Object, wdDoc As Object
Set wdApp = CreateObject("word.application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-F-380.1.doc")
With wdDoc.Content.Find
  .Wrap = wdFindContinue
  .Text = "Date:"
  .Replacement.Text = "Datetest"
  .Execute Replace:=wdReplaceAll
  .Text = "Prime Lending Rate"
  .Replacement.Text = " Repo Rate"
  .Execute Replace:=wdReplaceAll
End With
Set wdApp = Nothing: Set wdDoc = Nothing
End Sub