VBA 在 Word 文档的页脚中查找和替换
VBA find and replace in a word doc's footer
我有一份文档,其页脚中有一堆 <<system>>
。
到目前为止,我只是将它们突出显示并粘贴过来,这非常乏味。
我想 VBA 为我做这件事。
我编写了一个宏来处理文档中的所有查找和替换,但它在页脚中不起作用。
查看 google,似乎我所发现的只是如何使用 word 中的 vba 来完成,但我需要从 vba 开始工作 excel。有可能让它从那里开始工作吗?
vba 已使用(适用于 word doc 中的所有内容,但页脚中的内容除外):
Dim objword
Set objword = GetObject(, "Word.Application")
With objword.ActiveDocument.Content
With .Find
.Execute FindText:="thing to search", ReplaceWith:="thing to replace", Replace:=1 '(or 2)
End With
End With
如果在 Excel 中添加对 Word 对象的引用,生活会轻松很多,否则,这可能适用于 Excel:
Public Sub UpdateWord()
Dim wrdObj As Object ' Application object
Dim wrdDoc As Object ' Word object
Dim sect As Object ' Section
Dim footr As Object ' Footer
Dim sel As Object ' Selection
Dim kwrd_find As String: kwrd_find = "<<system>>" ''' Keyword to find
Dim kwrd_replace As String: kwrd_replace = "Hello" ''' Word to replace keyword
Dim DocPath As String: DocPath = "C:\path\to\my\word-document.docx"
Set wrdObj = GetObject(, "Word.Application")
''' Open word document
''' Change `Visible` to False if you don't want to see it.
Set wrdDoc = wrdObj.Documents.Open(DocPath, ReadOnly:=False, Visible:=True)
On Error Resume Next
With wrdDoc
''' Run through all sections and footers (you can add headers, too).
For Each sect In .Sections
For Each footr In sect.Footers
With footr.Range.Find
.Text = kwrd_find ''' Keyword to find
.Replacement.Text = kwrd_replace ''' Word to replace keyword
.Wrap = wdFindContinue ''' Wrap it up.
Call .Execute(Replace:=wdReplaceAll)
End With
Next
Next sect
End With
''' Close and save document
Call wrdDoc.Close(SaveChanges:=True)
''' Clean up e'rything.
Set wrdDoc = Nothing
Set wrdObj = Nothing
End Sub
我有一份文档,其页脚中有一堆 <<system>>
。
到目前为止,我只是将它们突出显示并粘贴过来,这非常乏味。
我想 VBA 为我做这件事。
我编写了一个宏来处理文档中的所有查找和替换,但它在页脚中不起作用。
查看 google,似乎我所发现的只是如何使用 word 中的 vba 来完成,但我需要从 vba 开始工作 excel。有可能让它从那里开始工作吗?
vba 已使用(适用于 word doc 中的所有内容,但页脚中的内容除外):
Dim objword
Set objword = GetObject(, "Word.Application")
With objword.ActiveDocument.Content
With .Find
.Execute FindText:="thing to search", ReplaceWith:="thing to replace", Replace:=1 '(or 2)
End With
End With
如果在 Excel 中添加对 Word 对象的引用,生活会轻松很多,否则,这可能适用于 Excel:
Public Sub UpdateWord()
Dim wrdObj As Object ' Application object
Dim wrdDoc As Object ' Word object
Dim sect As Object ' Section
Dim footr As Object ' Footer
Dim sel As Object ' Selection
Dim kwrd_find As String: kwrd_find = "<<system>>" ''' Keyword to find
Dim kwrd_replace As String: kwrd_replace = "Hello" ''' Word to replace keyword
Dim DocPath As String: DocPath = "C:\path\to\my\word-document.docx"
Set wrdObj = GetObject(, "Word.Application")
''' Open word document
''' Change `Visible` to False if you don't want to see it.
Set wrdDoc = wrdObj.Documents.Open(DocPath, ReadOnly:=False, Visible:=True)
On Error Resume Next
With wrdDoc
''' Run through all sections and footers (you can add headers, too).
For Each sect In .Sections
For Each footr In sect.Footers
With footr.Range.Find
.Text = kwrd_find ''' Keyword to find
.Replacement.Text = kwrd_replace ''' Word to replace keyword
.Wrap = wdFindContinue ''' Wrap it up.
Call .Execute(Replace:=wdReplaceAll)
End With
Next
Next sect
End With
''' Close and save document
Call wrdDoc.Close(SaveChanges:=True)
''' Clean up e'rything.
Set wrdDoc = Nothing
Set wrdObj = Nothing
End Sub