word2007 vba can only select (different) firstpage header, not 2nd

D

drichird

'multipage document has a different firstpage header and standard headers
'on rest of the pages (different first header). I need to select header
'for rest of pages (not the different, first page header)
'but every change I make seems to affect firstpage header only

'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
'neither of the following statements seems to work
'ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryHeader
'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"
 
D

Doug Robbins - Word MVP

To add a picture to the first page header in a section, use:

ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Shapes.AddPicture

To add it to the primary header use:

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.AddPicture

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

drichird

Hi Doug

Are you running Word 2007 or 2003? I followed your suggestion and it does
not work in 2007. It does, however, work in 2003. I think the problem is
there's a bug in 2007, namely: wdHeaderFooterPrimary is acting like
wdHeaderFooterFirstPage
 
D

Doug Robbins - Word MVP

I do not think there is a bug such as that. In Word 2007, using

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Select

selects the primary header, not the first page header.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

drichird

Hi Doug

I simply cannot get this vba statement to work on my Word2007/XP/sp2:

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

it simply changes the first page header only. It does, however, work fine
 
D

Doug Robbins - Word MVP

You must first set the DifferentFirstPageHeaderFooter option

With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Shapes.AddPicture "C:\logo.jpg", False,
True
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

drichird

I think that's an attribute of the header object(s), if the headers are
created with a different first header then that option is already set to
true. When I debug that option shows true already so setting it to true
again does not really change anything. If I create headers without a
different first header, that option shows false, as would be expected.
 
D

Doug Robbins - Word MVP

If I run the following code:

With ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Shapes.AddPicture "C:\logo.jpg", False,
True

when the document has more than one page, it inserts a logo into the the
primary header only.

If the document however only has one page, even using

With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Shapes.AddPicture "C:\logo.jpg", False,
True

The logo wdHeaderFooterPrimary is ignored and the loge gets inserted into
the FirstPageHeader

The following is a work around that checks to see how many pages there are
in the document, inserts a page break at the end of the document if there is
only one page, inserts the logo (this time into the primary header) and
deletes the added page:

Dim Flag As Boolean
Flag = False
Selection.HomeKey wdStory
With ActiveDocument
If .BuiltInDocumentProperties(wdPropertyPages) = 1 Then
Flag = True
With .Range
.Collapse wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
End If
With .Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Shapes.AddPicture "C:\logo.jpg",
False, True
End With
If Flag = True Then
.Range.Collapse wdCollapseEnd
.Range.Bookmarks("\Page").Range.Delete
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

drichird

Doug, you are a bulldog with this, thanks. Unfortunately I run your code and
only see a logo appear in the first page header of a 10 page document with
"different first page" specified in "Header Footer Tools" Ribbon menu. Maybe
our Word installations are different.
 
D

drichird

'starting with 10 page document, "different first page" specified in
'header and footer tools menu, Word 2007, why does Range.Text
work but Shapes.AddPicture does not work?

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

'this only puts logo on header of page 1
.headers(wdHeaderFooterPrimary).Shapes _
.AddPicture("C:\logo1.bmp", False, True).Select

'this puts text into headers of pages 2 through 10
.headers(wdHeaderFooterPrimary).Range.Text = _
"***** PRIMARY ********"

End With
 
D

Doug Robbins - Word MVP

Using:

Selection.HomeKey wdStory
With ActiveDocument
With .Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Shapes.AddPicture "C:\logo.jpg",
False, True
End With
End With

or

Selection.HomeKey wdStory
With ActiveDocument
With .Sections(1)
.Headers(wdHeaderFooterPrimary).Shapes.AddPicture "C:\logo.jpg",
False, True
End With
End With


here on a multipage document where under the page setup has previously been
set to Different First Page by accessing the Page Setup dialog via the Page
Layout tab, the logo is only inserted into the primary header and it does
not appear on the first page.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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