PDA

View Full Version : VBA select page header (not first page hdr)



drichird
07-28-2008, 03:33 PM
'...word 2007 VBA on XP sp2
'multipage document has a different firstpage header and standard headers
'(no even/odd) on rest of the pages. I need to add shape to header for rest
'of pages (not first page)
'this works for page 2,3,4 etc... in Word 2003. But in word 2007
'it changes first page header only (is there a bug in Word 2007 ? )

ActiveDocument.Sections(1).Headers _
(wdHeaderFooterPrimary).Shapes. _
AddPicture("C:\page234etc.bmp", False, True).Select



I also tried the more involved code below:

'go to the "first page" header
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageHeader
'insert a picture into the shapes of the "first page" header
Selection.HeaderFooter.Shapes.AddPicture _
(FileName:="C:\first.bmp", _
LinkToFile:=False, SaveWithDocument:=True).Select
'give the "first page" header picture a name
Selection.ShapeRange.Name = "firstpage"
'goto 2nd page, the 2nd type of header for all other pages
'starts on the second page
ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, , "2").Select
'Select the header on page 2, but I think it selects page 1 instead
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
'insert 2nd picture into 2nd page header
'this does not work, picture ends up in first header
Selection.HeaderFooter.Shapes.AddPicture _
(FileName:="C:\second.bmp", _
LinkToFile:=False, SaveWithDocument:=True).Select
Selection.ShapeRange.Name = "secondpage"

macropod
07-29-2008, 06:18 AM
Hi drichard,

Does your code have a line like:
ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
Your problem description sounds rather like the document has been set up with 'different odd & even' rather than 'different first page' properties (I don't have Word 2007 to test with right now, but what you describe happens if this is true in Word 2000).

Cheers

drichird
07-29-2008, 05:07 PM
I think ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter is more of an object attribute that reflects how the existing headers were set up. I can override the existing value, but that changes the structure of the existing headers which is not my intent. I simply want to add pictures into the headers.

In any case, this boolean is already set to true if the VBA code is looking at a document with "different first page" header. I can verify that in the debugger. I reset it to true (-1) just to try it, but of course that did not change anything.

drichird
07-30-2008, 11:43 AM
To put the whole thing in the simplest terms:

If anyone has any VBA code that can add a picture to every header of a Word 2007 document, (and actually verified that it works on Word 2007, NOT 2003) where the header option wdHeaderFooterFirstPageIsDifferent = TRUE (meaning the first page header is different than all the rest) I would fall over in my chair.

mdmackillop
08-03-2008, 09:46 AM
Sub AddPicToHeader()
Dim Header As HeaderFooter
Dim s As Section
For Each s In ActiveDocument.Sections
Set Header = s.Headers(1)
Header.Shapes.AddPicture ("C:\MyPic.jpg")
Next
End Sub

drichird
08-06-2008, 10:51 AM
Thanks mdmackillop but that still only gave me a logo/pic on the first page header. To get to pages 2,3,4 I finally, after endless attempts, stumbled upon inline shapes and ended up using this to put a logo shape into the headers of pages 2,3,4,5 etc...

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary). _
Range.InlineShapes.AddPicture _
("C:\Temp\allpagesafterthefirst.bmp").Select

Selection.InlineShapes(1).ConvertToShape