Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

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,
0
alcani
Asked:
alcani
  • 4
  • 4
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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
 
alcaniAuthor Commented:
Thanks!!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now