在Word文档的标题部分中搜索文本

时间:2012-02-08 01:28:44

标签: vba word-vba

我正在尝试确认文档是否包含某些文本,唯一的问题是此文本位于标题中。这是我使用的代码,即使文本存在,它也会不断返回false:

Set CurrentDoc = Documents.Open("a.doc")

With CurrentDoc.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Find
    .Text = "This is the text to find"
    .Forward = True
    .Execute
    If (.Found = True) Then Debug.Print "Match"
End With

以下似乎也不起作用(我假设.Content不包含页眉/页脚):

With CurrentDoc.Content.Find
    .Text = "This is the text to find"
    .Forward = True
    .Execute
    If (.Found = True) Then Debug.Print "Match"
End With

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您可能正在尝试搜索错误的部分/ headertype。您可以尝试以下代码:

Dim rng As Range
Dim intSecCount As Integer
Dim intHFType As Integer
intSecCount = ActiveDocument.Sections.Count
For intSection = 1 To intSecCount
    With ActiveDocument.Sections(intSection)
        For intHFType = 1 To 3 
            Set rng = ActiveDocument.Sections(intSection).Headers(intHFType).Range
            rng.Find.Execute findtext:="This is the text to find", Forward:=True
            If rng.Find.Found = True Then
                Debug.Print "Match" 
            End If
        Next intHFType
    End With
Next intSection

答案 1 :(得分:1)

我在这个网站上找到了答案,它比最初的想法复杂得多:http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

以下代码来自上面的网站,除了搜索整个文档外,它还包含文本替换功能:

Public Sub FindReplaceAnywhere()
    Dim rngStory As Word.Range
    Dim pFindTxt As String
    Dim pReplaceTxt As String
    Dim lngJunk As Long
    Dim oShp As Shape

    pFindTxt = InputBox("Enter the text that you want to find.", "FIND" )

    If pFindTxt = "" Then
        MsgBox "Cancelled by User"
        Exit Sub
    End If

    TryAgain:
        pReplaceTxt = InputBox( "Enter the replacement." , "REPLACE" )

        If pReplaceTxt = "" Then
            If MsgBox( "Do you just want to delete the found text?", vbYesNoCancel) = vbNo Then
                GoTo TryAgain
            ElseIf vbCancel Then
                MsgBox "Cancelled by User."
            Exit Sub
        End If
    End If

    '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 WdStoryType.wdEvenPagesHeaderStory, _
                     WdStoryType.wdPrimaryHeaderStory, _
                     WdStoryType.wdEvenPagesFooterStory, _
                     WdStoryType.wdPrimaryFooterStory, _
                     WdStoryType.wdFirstPageHeaderStory, _
                     WdStoryType.wdFirstPageFooterStory
                    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

Public 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
相关问题