VBA Problem with headers and footers

S

Sascha Stops

Hello,

I have a problem with inserting images into headers and footers into a Word
Document using VBA. The problem seems simple but somehow it does not do what
it is supposed to do. I want to insert an image into a header and a footer
and different image on the header of the first page. What happens is that if
there is only one page in the document it put all headers on the first page
with the Primary Header on top of the First Page Header. If there are
multiple pages in the document it does starting with page two and leaves
page one empty.

The template is for Word 2007

I hope someone can help.

Thank you
Sascha


I have the following code:

Sub insertHeaderAndFooterForFax(ByVal control As IRibbonControl)
CleanHeaders

ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter =
True

AddFooter "footGen.jpg", "FaxFooter", wdHeaderFooterPrimary, 120, 350

AddHeader "headFirst.jpg", "FaxHeaderFirst", wdHeaderFooterFirstPage

AddHeader "headGen.bmp", "FaxHeader", wdHeaderFooterPrimary

End Sub


Sub AddHeader(ByVal fileName As String, ByVal nameSuffix As String,
headerType As WdHeaderFooterIndex)
Dim section As Word.section
Dim Document As Word.Document
Set Document = Application.ActiveDocument

For Each section In Document.Sections
If (section.Headers(headerType).Exists) Then
Set headerShape =
section.Headers(headerType).Shapes.AddPicture(fileName:= _
ImagesRoot & fileName _
, LinkToFile:=False, SaveWithDocument:=True)
headerShape.Name = nameSuffix

headerShape.ZOrder (msoSendBehindText)
headerShape.LockAspectRatio = msoTrue
headerShape.Left = -1 * section.PageSetup.LeftMargin
headerShape.Top = section.PageSetup.HeaderDistance * -1 '
headerShape.Width = section.PageSetup.PageWidth
End If

Next
End Sub

Sub AddFooter(ByVal fileName As String, ByVal nameSuffix As String,
headerType As WdHeaderFooterIndex, xDelta, yDelta)
Dim section As Word.section
Dim Document As Word.Document
Set Document = Application.ActiveDocument

For Each section In Document.Sections
Set footerShape =
section.Footers(headerType).Shapes.AddPicture(fileName:= _
ImagesRoot & fileName _
, LinkToFile:=False, SaveWithDocument:=True)
footerShape.Name = fileName

footerShape.ZOrder (msoSendBehindText)
footerShape.LockAspectRatio = msoTrue
footerShape.Width = 100
footerShape.Left = section.PageSetup.PageWidth -
section.PageSetup.LeftMargin - xDelta
footerShape.Top = -1 * (yDelta - section.PageSetup.FooterDistance)
Next

End Sub

Sub CleanHeaders()
Dim section As Word.section
Dim Document As Word.Document
Set Document = Application.ActiveDocument

For Each section In Document.Sections
section.PageSetup.DifferentFirstPageHeaderFooter = False
section.PageSetup.OddAndEvenPagesHeaderFooter = False

On Error Resume Next 'Cleaning required - rewrite section

section.Headers.Item(wdHeaderFooterEvenPages).Shapes.SelectAll
Selection.Delete

section.Headers.Item(wdHeaderFooterFirstPage).Shapes.SelectAll
Selection.Delete

section.Headers.Item(wdHeaderFooterPrimary).Shapes.SelectAll
Selection.Delete

section.Footers.Item(wdHeaderFooterEvenPages).Shapes.SelectAll
Selection.Delete

section.Footers.Item(wdHeaderFooterFirstPage).Shapes.SelectAll
Selection.Delete

section.Footers.Item(wdHeaderFooterPrimary).Shapes.SelectAll
Selection.Delete

On Error GoTo 0

Next
End Sub
 
N

NOPIK

For Each section In Document.Sections

Definitely includes all sections, so check for first page (not first!)
header.

P.S.
I'm always use shape names as tags like "UniqueName0000" so, I can
carefully detect, redefine or remove elements, added by specific
script or scripts family.
 
S

Sascha Stops

Hello and thank you for your reply. I thought I did by adding calling my

AddHeader "headFirst.jpg", "FaxHeaderFirst", wdHeaderFooterFirstPage

Sub and passing the wdHeaderFooterFirstPage parameter. What do you mean with
"first page (not first!) header."

Because I only have one section in my test documents it shoudl mater because
the shape names are unique with "FaxFooter", "FaxHeader" and
"FaxHeaderFirst". But thank you for the hint.

Thanks for your help

Sascha
 
N

NOPIK

By all sections I meaned "Including the first one", so you processing
already assigned section.
first page by count not always first header by count (unless, you
create whole focument with your macro).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top