Delete odd & even headers in MS Word

Hi,

I'm looking for a simple VBA code in order to create a macro that will examine all header and footers (odd & even) of a MS Word document and delete the content.

Thank you.

Regards,
alcaniAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
That would work, but the text would end up as "LINE 2". The second line overwrites the text that the first line has line has written.

There are several ways to get two lines. The simplest is:
ftr.Range.Text = "LINE 1" & VbCr & "LINE 2"
0
 
GrahamSkanRetiredCommented:
This removes all the text from all the headers and footers in a document
Sub HeadersAndFootersToBlank(Doc As Word.Document)
    Dim sec As Word.Section
    Dim ftr As Word.HeaderFooter
    Dim hdr As Word.HeaderFooter
    For Each sec In Doc.Sections
        For Each ftr In sec.Footers
            ftr.Range.Text = ""
        Next ftr
        For Each hdr In sec.Headers
            hdr.Range.Text = ""
        Next hdr
    Next sec
End Sub

Open in new window

0
 
alcaniAuthor Commented:
Ok thank you, this works ok.
If I would like to add juste after remove the text other information in a odd header /footer, which will will the syntax?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
GrahamSkanRetiredCommented:
This addresses each footer type separately. The headers work in the same way.
Sub EachFooterTypeToBlank(Doc As Word.Document)
    Dim sec As Word.Section
    Dim ftr As Word.HeaderFooter
    Dim hdr As Word.HeaderFooter
    For Each sec In Doc.Sections
        Set ftr = sec.Footers(wdHeaderFooterPrimary)
        ftr.Range.Text = ""
        Set ftr = sec.Footers(wdHeaderFooterFirstPage)
        ftr.Range.Text = ""
        Set ftr = sec.Footers(wdHeaderFooterEvenPages)
        ftr.Range.Text = ""
    Next sec
End Sub

Open in new window

0
 
alcaniAuthor Commented:
Thanks. I'm sorry ... I'm learning...

I copied your code and pasted it in my VBA editor.

However I'm not able to see the macro "EachFooterTypeToBlank".

I saw that the variable "Doc As Word.Document" is doing something because I'm not able to see  the macro when I try to execute it.
I tried like this:
Sub EachFooterTypeToBlank()
Dim Doc As Word.Document
........
Then I could execute it but it send me an error.
I'm using Word 2003.

What is the reason?
0
 
GrahamSkanRetiredCommented:
You have to tell the macro which document the Doc variable represents.

Try:

Sub EachFooterTypeToBlank()
    Dim Doc As Word.Document
    Dim sec As Word.Section
    Dim ftr As Word.HeaderFooter
    Dim hdr As Word.HeaderFooter
 
    Set Doc = ActiveDocument '<----- New line
    For Each sec In Doc.Sections
        Set ftr = sec.Footers(wdHeaderFooterPrimary)
        ftr.Range.Text = ""
        Set ftr = sec.Footers(wdHeaderFooterFirstPage)
        ftr.Range.Text = ""
        Set ftr = sec.Footers(wdHeaderFooterEvenPages)
        ftr.Range.Text = ""
    Next sec
End Sub

Open in new window

0
 
alcaniAuthor Commented:
Thank you. I really appreciate your help.
Is it posible to insert a header/footer with several lines eg.

Something like that
For Each sec In Doc.Sections
        Set ftr = sec.Footers(wdHeaderFooterPrimary)
        ftr.Range.Text = "LINE 1"
        ftr.Range.Text = "LINE 2"

etc.....
In the same header?
0
 
alcaniAuthor Commented:
Thanks!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.