Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:31:15+00:00 2026-05-29T11:31:15+00:00

I am trying to confirm if a document contains some text, the only problem

  • 0

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

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

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:31:16+00:00Added an answer on May 29, 2026 at 11:31 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a custom :confirm message for a rails form that returns data
Using Watin, I'm trying to handle a confirm dialog box and tell watin to
i've been trying to get a confirm box to work, i am using php
Following up from this question , I'm trying to implement an unobtrusive confirm dialog.
I am trying to put some jquery UI code into an existing asp.net app,
I'm trying to find a way to get the text of the new confirm
I'm trying to confirm a sale order via xml rpc with java. My code
I'm trying to show Value of Html.TextAreaFor in Javascript Confirm Box but my code
I'm trying to confirm a user a unplugged my embedded device from a network
I'm trying to confirm my findings on permissions. In order for the SharePoint object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.