Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2007

Tip: Looking for answers? Try searching our database.

adding a logo to headers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anne Schouten - 27 Nov 2007 20:58 GMT
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
Doug Robbins - Word MVP - 28 Nov 2007 01:56 GMT
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

Signature

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

>I made a macro to place a logo on each header of a Word document.
>
[quoted text clipped - 44 lines]
>
> Anne
fumei - 28 Nov 2007 19:13 GMT
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.
fumei - 28 Nov 2007 19:21 GMT
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.
Anne Schouten - 29 Nov 2007 19:10 GMT
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

> Try:
>
[quoted text clipped - 64 lines]
> >
> > Anne

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.