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