Link to home
Start Free TrialLog in
Avatar of alcani
alcani

asked on

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,
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of alcani
alcani

ASKER

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?
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

Avatar of alcani

ASKER

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?
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

Avatar of alcani

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alcani

ASKER

Thanks!!!