Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Remove Text from Even Page Header - Section 1 in a Word doc (2013) using VBA? 1

Status
Not open for further replies.

PBREP

MIS
Mar 14, 2003
75
US

History: I have a macro (not shown) that deletes the blank page that comes between the cover page and the proprietary page of a Word document that is generated from RoboHelp. However, the cover and the proprietary pages become the same section and the footer section (macro driven as well) is added to the cover. After, another round of coding and testing to address the footer appearing on the cover page (recall, after deleting the blank page), we found a way to delete the footer info, but it would have to be done for both the Cover page and the Proprietary page (have no issues with that). However, I have an issue that I need assitance on from one of the VBA gurus out there.

Problem: I need to remove the chapter name in the top-right hand corner (image link below).

Prior to the Word 2013 macros running, the chapter titles are listed in the header on every other page (even page has the name of the chapter in the document, e.g. Navigation). When running the macros, it removes the chapter names in the header, and does all the other custom formatting.

What I believe is happening: It is because we’re not processing the headers and footers in the first two sections (see red below in code excerpt). I think I might need to make an adjustment so that only the footer logic is skipped if the section index is less than or equal to 2. That way the headers should still be processed as necessary. So, how do I go about this?

I am guessing I will need to add some logic (within the existing code I have) that will look for ANY Text = “ ” only in Even Page Header - Section 1 and remove it ???

Thank you,
~PM

CODE:

Sub headerFooter()
Dim s As Section
For Each s In ActiveDocument.Sections
' Skip first two sections
If s.Index <= 2 Then GoTo NextSection

DoHeader s.Headers(wdHeaderFooterFirstPage)
DoFirstFooter s.Footers(wdHeaderFooterFirstPage)

DoHeader s.Headers(wdHeaderFooterPrimary)
DoOddFooter s.Footers(wdHeaderFooterPrimary)

DoHeader s.Headers(wdHeaderFooterEvenPages)
DoEvenFooter s.Footers(wdHeaderFooterEvenPages)
NextSection:
Next s
End Sub

 
 http://files.engineering.com/getfile.aspx?folder=82a95fa6-3ffa-4999-9fca-df47311e46b0&file=image2.jpg
As is apparent from your screenshot, your document has an even-pages header in Section 1. That's easily deleted via:
ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages).Range.Text = vbNullString
If you need to preserve that header for subsequent Sections, precede that line with:
ActiveDocument.Sections(2).Headers(wdHeaderFooterEvenPages).LinkToPrevious = False


Cheers
Paul Edstein
[MS MVP - Word]
 
Thank you Paul for your response and assistance. Your logic (code) worked. Just making sure that I placed the code in the appropriate spot (see below).

Thanks again,
~PM

Sub headerFooter()
Dim s As Section
For Each s In ActiveDocument.Sections
' Skip first two sections
If s.Index <= 2 Then GoTo NextSection
ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages).Range.Text = vbNullString ' Delete text in even-pages header in Section 1
ActiveDocument.Sections(2).Headers(wdHeaderFooterEvenPages).LinkToPrevious = False ' To preserve header for subsequent Sections

DoHeader s.Headers(wdHeaderFooterFirstPage)
DoFirstFooter s.Footers(wdHeaderFooterFirstPage)

DoHeader s.Headers(wdHeaderFooterPrimary)
DoOddFooter s.Footers(wdHeaderFooterPrimary)

DoHeader s.Headers(wdHeaderFooterEvenPages)
DoEvenFooter s.Footers(wdHeaderFooterEvenPages)
NextSection:
Next s
End Sub
 
I can't see the point in putting the code I supplied inside a loop. A document can only contain one first section and, if there's more than one section, only one second section also. Moreover, your loop explicitly excludes the first two Sections so, if it only contains two sections, putting the code I provided inside your loop means it won't execute.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

I cleaned up the logic (see below), which works as well. Thnks, ~PM

Sub headerFooter()
Dim s As Section
For Each s In ActiveDocument.Sections
' Configure header on all but first section
If s.Index > 1 Then
DoHeader s.Headers(wdHeaderFooterFirstPage)
DoHeader s.Headers(wdHeaderFooterPrimary)
DoHeader s.Headers(wdHeaderFooterEvenPages)
End If

' Configure footer on all but first two sections
If s.Index > 2 Then
DoFirstFooter s.Footers(wdHeaderFooterFirstPage)
DoOddFooter s.Footers(wdHeaderFooterPrimary)
DoEvenFooter s.Footers(wdHeaderFooterEvenPages)
End If
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top