在 Word 文档的标题部分中搜索文本 [英] Searching for Text in Header Section of A Word Document

查看:68
本文介绍了在 Word 文档的标题部分中搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确认一个文档是否包含一些文本,唯一的问题是这个文本在页眉中.这是我正在使用的代码,即使文本存在,它也会不断返回 false:

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 though the text exists:

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 不包括页眉/页脚):

The following also doesn't seem to work (I assume .Content doesn't include header/footers):

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

任何帮助将不胜感激.

推荐答案

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

I found the answer on this site and it's a lot more complex than initially thought: http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

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

The following code is from the site above, in addition to searching the entire document it includes text replace functionality:

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

这篇关于在 Word 文档的标题部分中搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆