Illustration in headers

C

Cor van der Bliek

Word 2007 NL
I have a doc with more than 1 section, of which the headers are not linked.
I have a macro to put a logo in the header of each section.
I can't seem to get it working:

Sub logo()
Const nvPic As String = "G:\Templates\wst.png"
With ActiveDocument
For i = 1 To .Sections.Count
With .Sections(i)
Set oShape =
..Headers(wdHeaderFooterPrimary).Shapes.AddPicture(nvPic)
End With
Next i
End With
End Sub

The result is as many logos in the header of section 1 as the total of
sections.
 
P

Pesach Shelnitz

Hi Cor,

I tried your macro, and it worked fine for me when I created section breaks
that start on a new page, but I would expect to see what you observed with
continuous section breaks. Let's say that you insert 2 continuous section
breaks in a doc that consists of only one page. You will then have 3
sections, and AddPicture will be called 3 times in your loop to add the logo
to the same header. You can avoid this by using section breaks that start a
new page or by adding code to your macro that checks whether the logo has
already been added.
 
C

Cor van der Bliek

Hello Pesach
I'm puzzled, because this doesn't seems to work with me. The illustration
simply only shows up in section 1, not in the other sections.

The sections are not linked to one another and start on new pages every time.

Are you using Word 2007?
 
P

Pesach Shelnitz

Hi Cor,

I've taken a more careful look, and now I'm also puzzled. When I create a
new document and divide it into three sections, your macro appears to work
fine, as I reported. However, my three sections were linked according to the
default settings. As a result, the logo appeared in the headings on all the
pages, but there were actually three superimposed copies of it in the first
section. Now I understand the issue and will try to find a solution.
 
P

Pesach Shelnitz

Hi Cor,

The following macro can place one copy of the logo in the header of each
section.

Sub Logo()

Const nvPic As String = "G:\Templates\wst.png"
With ActiveDocument
.PageSetup.DifferentFirstPageHeaderFooter = False
.PageSetup.OddAndEvenPagesHeaderFooter = False
For i = 2 To .Sections.Count
.Sections(i).Headers(wdHeaderFooterPrimary).LinkToPrevious = True
Next
.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.AddPicture nvPic
For i = 2 To .Sections.Count
.Sections(i).Headers(wdHeaderFooterPrimary).LinkToPrevious = False
Next
End With
End Sub

This macro, however, temporarily links the headers and thus copies any other
existing content in the header of the first section into the headers of the
other sections. There are two ways to resolve this issue. One would be to
insert the logo before inserting any other content, and the other would be to
save the content of each header and restore it after the logo is added. Let
me know if you need any help in implementing either of these solutions.
 
C

Cor van der Bliek

This does the trick of putting the picture in the different -nonlinked
sections. As you mention, temporarily setting LinkToPrevious destroys the
other contents of the header. Besides there's the deleting of the
illustrations as well.
It is meant to function with 2 macro's, one to create the pictures and one
to destroy them.

However I found out it's a bug with the dotx (dotm) file format:
consider these simple lines in a doc with 2 nonlinked sections:

Sub logo()
Const nvPic As String = "G:\Templates\wst.png"
With ActiveDocument
.Sections(1).Headers(1).Shapes.AddPicture (nvPic)
.Sections(2).Headers(1).Shapes.AddPicture (nvPic)
End With
End Sub

Running the macro in Word XP or Word 2007 in compatibilty modus works fine.

When I save the doc as dotx or dotm I get 2 logo's, both in the header of
section 1 and of course the header of section 2 stays empty!!
 

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