| View previous topic :: View next topic |
| Author |
Message |
Anne Schouten Guest
|
Posted: Tue Nov 27, 2007 8:58 pm Post subject: adding a logo to headers |
|
|
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 |
|
| Back to top |
|
 |
Google Sponsor

|
Posted: Tue Nov 27, 2007 8:58 pm Post subject: Advertisement |
|
|
|
|
| Back to top |
|
 |
Doug Robbins - Word MVP Guest
|
Posted: Wed Nov 28, 2007 1:56 am Post subject: Re: adding a logo to headers |
|
|
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
"Anne Schouten" <aaschouten@hotmail.com> wrote in message
news:474c8538$0$45824$dbd49001@news.wanadoo.nl...
| Quote: | 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
|
|
|
| Back to top |
|
 |
fumei via OfficeKB.com Guest
|
Posted: Wed Nov 28, 2007 7:13 pm Post subject: Re: adding a logo to headers |
|
|
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.
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200711/1 |
|
| Back to top |
|
 |
fumei via OfficeKB.com Guest
|
Posted: Wed Nov 28, 2007 7:21 pm Post subject: Re: adding a logo to headers |
|
|
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.
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200711/1 |
|
| Back to top |
|
 |
Anne Schouten Guest
|
Posted: Thu Nov 29, 2007 7:10 pm Post subject: Re: adding a logo to headers |
|
|
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
"Doug Robbins - Word MVP" <dkr@REMOVECAPSmvps.org> wrote in message
news:On%23d0HWMIHA.4740@TK2MSFTNGP02.phx.gbl...
| Quote: | 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
"Anne Schouten" <aaschouten@hotmail.com> wrote in message
news:474c8538$0$45824$dbd49001@news.wanadoo.nl...
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
|
|
|
| Back to top |
|
 |
|