Share via

VBA code which can change or replace current text in Header and Footer as per our requirement

Anonymous
2017-10-25T11:53:53+00:00

Hi,

Hope everyone doing fine.

My name is Raghu and i need help in MS word VBA code.

I have 1800 word documents each document has not less than 10 pages , i am trying to replace invoice number which has 17 characters with 16 characters, which i am able to do it when it is a story but i am not able to do the same job when this invoice number is mentioned in Header section.

i have tried lot of ways to do but i am not sucess, can some please help with code where i can connect to headers and change 17 characters to 16 characters

17 Character = RAGHU2017FCM00001

16 Character = RAGHU2017FCM0001

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2017-10-25T12:16:19+00:00

    Try this:

    Sub SearchAllHeadersAndFooters()

        Dim rng As Word.Range, s As Word.Section, hf As Word.HeaderFooter

        For Each s In ActiveDocument.Sections

            'headers and footers are part of each section in a document

            'all sections must be searched

            For Each hf In s.Headers

                'there are 3 headers that will be searched (primary, even, & first page)

                Set rng = hf.Range

                If InStr(1, rng.Text, "your string") = True Then

                    'perform your custom action

                End If

            Next hf

            For Each hf In s.Footers

                'there are 3 footers that will be searched (primary, even, & first page)

                Set rng = hf.Range

                If InStr(1, rng.Text, "your string") = True Then

                    'perform your custom action

                End If

            Next hf

        Next s

    End Sub

    Hope this helps

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. HansV 462.6K Reputation points
    2017-10-25T12:05:35+00:00

    See https://wordmvp.com/FAQs/Customization/ReplaceAnywhere.htm for a macro that will replace text anywhere in a document, including headers and footers.

    Was this answer helpful?

    0 comments No comments