adding a logo to headers

A

Anne Schouten

I made a macro to place a logo on each header of a Word document.

The logo is a jpg-file and stored as an autotext (Logo) in the template.

As a document can have several sections I only place a logo as the header is
not linked to the previous one.



The (shortened) code is:



bytNumberOfSections = ActiveDocument.Sections.Count

For n = 1 To bytNumberOfSections

ActiveWindow.ActivePane.View.NextHeaderFooter

If Selection.HeaderFooter.LinkToPrevious = False Then

.TypeText Text:= "Logo"

.Range.InsertAutoText

End If

Next



With Word 2003 I encounter the following problems:

1.. With more then 1 section on a page he does not go to the next Header,
so the last sections do not have a logo (if the header is not linked to the
previous one)
2.. If I run the macro with F8 the rest goes fine, but if I just run the
macro (F5) he does not always react on the statement:
If Selection.HeaderFooter.LinkToPrevious = False Then

and insert a second logo on top of the other one when the header
is linked to the previous header.
If I add to the code some lines inserting text and deleting the
inserted text it works alright, but I do not think that is an very elegant
solution.


This second problem (by running a macro he skips some lines) I had also in
other macro's in Word 2003 (not in Word 2000).


I do hope someone can give me an answer for these problems.



Anne
 
D

Doug Robbins - Word MVP

Try:

Dim i As Long
With ActiveDocument
For i = 1 To .Sections.Count
With .Sections(i).Headers(wdHeaderFooterFirstPage)
If .LinkToPrevious = False Then
.Range.Text = logo
.Range.InsertAutoText
End If
End With
With .Sections(i).Headers(wdHeaderFooterPrimary)
If .LinkToPrevious = False Then
.Range.Text = logo
.Range.InsertAutoText
End If
End With
Next i
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
 
F

fumei via OfficeKB.com

As often with VBA, there are possble alternative routes.

Sub Eachheader()
Dim oSection As Section
Dim oHF As HeaderFooter

For Each oSection In ActiveDocument.Sections()
For Each oHF In oSection.Headers
If oHF.LinkToPrevious = False Then
NormalTemplate.AutoTextEntries("logo").Insert _
Where:=oHF.Range, RichText:=True
End If
Next
Next
End Sub

This avoids the instructions to put "logo" text, THEN use that for the
AutoText. It inserts the Autotext directly to the header range.
 
F

fumei via OfficeKB.com

Ooops, missed something. It was mentioned that there may be previous content.
In which case, you can simply delete all content before adding the Autotext.


Sub Eachheader()
Dim oSection As Section
Dim oHF As HeaderFooter

For Each oSection In ActiveDocument.Sections()
For Each oHF In oSection.Headers
If oHF.LinkToPrevious = False Then
oHF.Range.Delete
NormalTemplate.AutoTextEntries("logo").Insert _
Where:=oHF.Range, RichText:=True
End If
Next
Next
End Sub

Oh, and it actions to ALL the header objects in each Section, including
DifferentOddEven, which was not included in Doug's code. That may, or may
not, be of significance.
 
A

Anne Schouten

Thanks a lot Doug Robbins and fumei,

Your answers are a great help I'll test them to morrow, but I trust your
solutions are better then mine.

Do you know why Word 2003 vba skips sometimes certain lines with running the
macro with F5 and not with F8. And why it helps to add some lines (to slow
down??) to solve that problem.

Anne
 

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