如何强制替换宏也适用于标题

时间:2012-07-26 15:51:27

标签: vba replace ms-word word-vba

我想找到&替换word文档中的文本。我在下面创建了一个宏。

Sub Macro1()
  ActiveDocument.Content.Find.Execute FindText:="#Text1", ReplaceWith:="acca", _
     Replace:=wdReplaceAll   
End Sub

它取代了所有发生但不在页眉/页脚中!! 如何强制处理整个文档包括标题/正文/页脚?

4 个答案:

答案 0 :(得分:6)

我一直使用这个VBA代码进行查找/替换,它将与文档正文一起执行页眉/页脚:

    Dim myStoryRange As Range


        For Each myStoryRange In ActiveDocument.StoryRanges
        With myStoryRange.Find
            .Text = "Text to find to replace goes here"
            .Replacement.Text = "And the replacement text goes here"
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
        Do While Not (myStoryRange.NextStoryRange Is Nothing)
            Set myStoryRange = myStoryRange.NextStoryRange
            With myStoryRange.Find
                .Text = "Text to find to replace goes here"
                .Replacement.Text = "And the replacement text goes here"
                .Wrap = wdFindContinue
                .Execute Replace:=wdReplaceAll
            End With
        Loop
    Next myStoryRange

您也可以在同一个Sub中复制并粘贴多次,以便同时替换不同的字符串。

答案 1 :(得分:1)

应该有更好的方法,但我找不到它:

Sub ReplaceHeaderFooterandBody(findString As String, replaceString As String)
ActiveDocument.Windows(1).View.SeekView = wdSeekPrimaryHeader
With Selection.Find
        .Text = findString
        .Replacement.Text = replaceString
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.Windows(1).View.SeekView = wdSeekPrimaryFooter
With Selection.Find
        .Text = findString
        .Replacement.Text = replaceString
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.Windows(1).View.SeekView = wdSeekMainDocument
With Selection.Find
        .Text = findString
        .Replacement.Text = replaceString
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Word似乎拒绝搜索某个区域,除非它是您当前的视图(在我看来这很荒谬)。你甚至无法搜索包括标题和文件在内的整个文档。通过用户界面立即获得页脚。 Here's a question在另一个似乎得到相同答案的网站上。

答案 2 :(得分:0)

我没有看到任何方法“强制”查找和替换对话框以包含页眉和页脚文本。我在更改标题文本时记录了一个宏,并得到了这段代码:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/26/2012 by Jimmy Peña
'
  If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
  End If
  If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
  End If
  ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
  Selection.MoveRight Unit:=wdCharacter, Count:=1
  Selection.Delete Unit:=wdCharacter, Count:=1
  Selection.TypeText Text:="d"
End Sub

我去了View»Header / Footer,删除了一个字符并键入了一个新字符。

您可能需要做的是Find&amp;在VBA中替换:

  • 将标题的内容读入字符串变量
  • 解析String变量,必要时替换文本,然后
  • 将String变量的内容写回标题

重复页脚。

答案 3 :(得分:0)

我找到了正确的代码here即使在页脚/标题中的文本框中也会进行文本替换。

 Sub FindReplaceAnywhere(ByVal pFindTxt As String, ByVal pReplaceTxt As String)
  Dim rngStory As Word.Range
  Dim lngJunk As Long
  Dim oShp As Shape
TryAgain:
  'Fix the skipped blank Header/Footer problem
  lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
  'Iterate through all story types in the current document
  For Each rngStory In ActiveDocument.StoryRanges
    'Iterate through all linked stories
    Do
      SearchAndReplaceInStory rngStory, pFindTxt, pReplaceTxt
      On Error Resume Next
      Select Case rngStory.StoryType
      Case 6, 7, 8, 9, 10, 11
        If rngStory.ShapeRange.Count > 0 Then
          For Each oShp In rngStory.ShapeRange
            If oShp.TextFrame.HasText Then
              SearchAndReplaceInStory oShp.TextFrame.TextRange, _
                  pFindTxt, pReplaceTxt
            End If
          Next
        End If
      Case Else
        'Do Nothing
      End Select
      On Error GoTo 0
      'Get next linked story (if any)
      Set rngStory = rngStory.NextStoryRange
    Loop Until rngStory Is Nothing
  Next
End Sub
Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
    ByVal strSearch As String, ByVal strReplace As String)
  With rngStory.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = strSearch
    .Replacement.Text = strReplace
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End Sub