【问题标题】:How To replace text in footer/header in batch of folder of word files using VBA如何使用VBA替换批量word文件文件夹中的页脚/页眉中的文本
【发布时间】:2022-01-11 08:24:18
【问题描述】:

我有一个关于使用 VBA 替换页脚和页眉的问题 我有一个代码,该代码可以替换一个word文件的一个文件夹中的所有文本 我从 https://www.datanumen.com/blogs/find-replace-contents-multiple-word-documents/ 这里是代码

  Sub FindAndReplaceInFolder()
  Dim objDoc As Document
  Dim strFile As String
  Dim strFolder As String
  Dim strFindText As String
  Dim strReplaceText As String
 
  '  Pop up input boxes for user to enter folder path, the finding and replacing texts.
  strFolder = InputBox("Enter folder path here:")
  strFile = Dir(strFolder & "\" & "*.docx", vbNormal)
  strFindText = InputBox("Enter finding text here:")
  strReplaceText = InputBox("Enter replacing text here:")
 
  '  Open each file in the folder to search and replace texts. Save and close the file after the action.
  While strFile <> ""
    Set objDoc = Documents.Open(FileName:=strFolder & "\" & strFile)
    With objDoc
      With Selection
        .HomeKey Unit:=wdStory
        With Selection.Find
          .text = strFindText
          .Replacement.text = strReplaceText
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
      End With
      objDoc.Save
      objDoc.Close
      strFile = Dir()
    End With
  Wend
End Sub

我的问题是如何专门为页脚/页眉替换? 因为这段代码只能替换word文件正文中的一段文字

【问题讨论】:

标签: vba ms-word


【解决方案1】:

我已尝试修改代码,它可以很好地回答我的案例问题

这是我的代码

Sub FindAndReplaceInFolder()
 Dim objDoc As Document
  Dim strFile As String
  Dim strFolder As String
  Dim strFindText As String
  Dim strReplaceText As String
  Dim xSelection As Selection
  Dim xSec As Section
  Dim xHeader As HeaderFooter
  Dim xFooter As HeaderFooter
 
  '  Pop up input boxes for user to enter folder path, the finding and replacing texts.
  strFolder = InputBox("Enter folder path here:")
  strFile = Dir(strFolder & "\" & "*.docx", vbNormal)
  strFindText = InputBox("Enter finding text here:")
  strReplaceText = InputBox("Enter replacing text here:")
 
  '  Open each file in the folder to search and replace texts. Save and close the file after the action.
  While strFile <> ""
    Set objDoc = Documents.Open(FileName:=strFolder & "\" & strFile)
    With objDoc
    For Each xSec In objDoc.Sections
        For Each xHeader In xSec.Headers
            xHeader.Range.Select
            Set xSelection = objDoc.Application.Selection
            With xSelection
                .HomeKey Unit:=wdStory
                With xSelection.Find
                .Text = strFindText
                .Replacement.Text = strReplaceText
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                End With
                xSelection.Find.Execute Replace:=wdReplaceAll
            End With
        Next xHeader
        For Each xFooter In xSec.Footers
            xFooter.Range.Select
            Set xSelection = objDoc.Application.Selection
            With xSelection
                .HomeKey Unit:=wdStory
                With xSelection.Find
                .Text = strFindText
                .Replacement.Text = strReplaceText
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                End With
                xSelection.Find.Execute Replace:=wdReplaceAll
            End With
        Next xFooter
    Next xSec
    objDoc.Save
    objDoc.Close
    strFile = Dir()
    End With
  Wend
End Sub

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 无需选择页眉或页脚。 Find 方法也可从Range 对象获得。编写 VBA 代码时应避免使用Selection
猜你喜欢
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2017-08-01
  • 2015-07-09
  • 2019-08-27
  • 2021-03-13
  • 2013-05-30
相关资源
最近更新 更多