Excel VBA查找和替换word文本框
我有一个Excel程序 操作word文档并查找/替换文本。我的代码如下所示Excel VBA查找和替换word文本框,excel,vba,ms-word,Excel,Vba,Ms Word,我有一个Excel程序 操作word文档并查找/替换文本。我的代码如下所示 Dim wApp As Word.Application Dim wDoc As Word.Document Set wApp = CreateObject("Word.Application") wApp.Visible = False Set wDoc = wApp.Documents.Open(myFile) With wDoc.Con
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = False
Set wDoc = wApp.Documents.Open(myFile)
With wDoc.Content.Find
.Text = "<Customer Name>"
.Replacement.Text = "Customer Name"
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
.Text = "<Billing Address>"
.Replacement.Text = "Billing Address"
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Dim wApp作为Word.Application
将wDoc设置为Word.Document
设置wApp=CreateObject(“Word.Application”)
wApp.Visible=False
设置wDoc=wApp.Documents.Open(myFile)
使用wDoc.Content.Find
.Text=“”
.Replacement.Text=“客户名称”
.Wrap=wdFindContinue
.MatchWholeWord=True
.Execute Replace:=wdReplaceAll
.Text=“”
.Replacement.Text=“账单地址”
.Wrap=wdFindContinue
.MatchWholeWord=True
.Execute Replace:=wdReplaceAll
以
这很好用
但是,word文档的文本框中有一些单词,我想在其中查找并替换,但我无法使其工作(上面的代码只搜索word文档的主体,而不是文本框)
我试过了
wDoc.Shapes.Range(Array("Text Box 14")).Select
With wDoc.Selection.Find
.Text = "<Profile Code>"
.Replacement.Text = "Profile Code"
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
wDoc.Shapes.Range(数组(“文本框14”))。选择
使用wDoc.Selection.Find
.Text=“”
.Replacement.Text=“配置文件代码”
.Wrap=wdFindContinue
.MatchWholeWord=True
.Execute Replace:=wdReplaceAll
以
但那没用。
我得到一个对象不支持此属性或方法错误消息
请帮忙 如果文本框名为“文本框14”,则以下内容适用于您(在O365中适用于我)
带有wDoc.Shapes(“文本框14”).TextFrame.TextRange.Find
.Text=“”
.Replacement.Text=“配置文件代码”
.Wrap=wdFindContinue
.MatchWholeWord=True
.Execute Replace:=wdReplaceAll
以
With wDoc.Shapes("Text Box 14").TextFrame.TextRange.Find
.Text = "<Profile Code>"
.Replacement.Text = "Profile Code"
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With