Searching for Text in Header Section of A Word Document

后端 未结 2 501
南笙
南笙 2020-12-21 15:18

I am trying to confirm if a document contains some text, the only problem is this text is in the Header. This is the code I am using which constantly returns false even thou

2条回答
  •  孤城傲影
    2020-12-21 15:42

    You're probably trying to search in the wrong section/headertype. You could try this code:

    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
    

提交回复
热议问题