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 / March 2008

Tip: Looking for answers? Try searching our database.

Adding an image to headers dynamically using VBA Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brandon M. Hunter - 12 Mar 2008 15:29 GMT
Hi,
I've been having the hardest time adding an image to a header in word using
VBA Word. Below is the following code that I'm using to add an image to the
header. In full I want to have the ability to add to the header and footer
for each page.

docActive.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
docActive.Sections(1).Headers(1).InlineShapes.AddPicture sFileName, False,
true
With docActive.PageSetup
   .DifferentFirstPageHeaderFooter = False 'Set this to false will put text
on first page, else will not.
End With
Doug Robbins - Word MVP - 12 Mar 2008 20:54 GMT
Why don't you start with a template that already contains the image? (and
use code to delete it if there are some occasions when you do not want it.)

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

> Hi,
> I've been having the hardest time adding an image to a header in word
[quoted text clipped - 12 lines]
> on first page, else will not.
> End With
Brandon M. Hunter - 12 Mar 2008 21:18 GMT
I've tried and it doesn't work when it comes to adding the image to the
header or footer. That would be nice, but all the code that I've tried,
fails. Do you have any sample code that I take a look at, or maybe help me
out with the code I posted. I've been at this for about 2 days. I can add
text to both the header and footer, but I can't add images.

> Why don't you start with a template that already contains the image? (and
> use code to delete it if there are some occasions when you do not want it.)
[quoted text clipped - 15 lines]
> > on first page, else will not.
> > End With
Doug Robbins - Word MVP - 12 Mar 2008 23:50 GMT
To add a picture to the .Range of the primary header of the first section in
the document, you would use:

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture
FileName:="Path\Filename"

As you probably want other things in the header, it is usually best to
insert a table in the header and then insert the picture into a particular
cell of that table
Brandon M. Hunter - 13 Mar 2008 00:05 GMT
Below is the code that I'm using now, s is the picture name that already
exists on the document. The image is not inserted into the header. I tried
your code and it didn't work. Do see why this wouldn't work? Thank you for
the code. Please help, this is killing me.
Thanks in advance
Sub AddHeader(s)
   Dim docTemplate
   Dim hdr1
   Set docActive = objWord.ActiveDocument
   docActive.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
   docActive.Shapes(s).Select
   Set hdr1 = docActive.Sections(1).Headers(1)
   hdr1.Range.Copy
   hdr1.Range.Paste
   With docActive.PageSetup
           .DifferentFirstPageHeaderFooter = False
   End With
End Sub

> To add a picture to the .Range of the primary header of the first section in
> the document, you would use:
[quoted text clipped - 5 lines]
> insert a table in the header and then insert the picture into a particular
> cell of that table
Doug Robbins - Word MVP - 13 Mar 2008 01:46 GMT
You are not copying the picture.  You are copying the range of the header
and pasting it into itself.

It might work if you had:

docActive.Shapes(s).Select
Selection.Copy  'maybe Selection.CopyAsPicture
Set hdr1 = docActive.Sections(1).Headers(1)
hdr1.Range.Paste

You don't need to open the header pane if you are operating on the ..Range
of the header

Hence,

docActive.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

should not be necessary.
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

> Below is the code that I'm using now, s is the picture name that already
> exists on the document. The image is not inserted into the header. I tried
[quoted text clipped - 53 lines]
> .DifferentFirstPageHeaderFooter = False 'Set this to false will put>> >
> text>> > on first page, else will not.>> > End With>> >>>>>>>
Brandon M. Hunter - 13 Mar 2008 02:07 GMT
Thank you so much much. The Code works, but I was wondering, the code to add
the same image to the footer, just overlap the header image. Here's my code.
I call the AddHeader First, and then AddFooter second. Is there any reason
why I don't get the footer image in the actual footer of my document?
Thanks,
P.S. Where do I sign up for the newsgroup?
Sub AddHeader(s)
   Dim docTemplate
   Dim hdr1
   Set docActive = objWord.ActiveDocument'.Shapes(s).Select
   docActive.Shapes(s).Select
   objWord.Selection.Copy
   Set hdr1 = docActive.Sections(1).Headers(1)
   hdr1.Range.Paste
   With docActive.PageSetup
          .DifferentFirstPageHeaderFooter = False
   End With
End Sub
Sub AddFooter(s)
   Dim fdr1
   Set docActive = objWord.ActiveDocument
   docActive.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
   docActive.Shapes(s).Select
   objWord.Selection.Copy
   Set fdr1 = docActive.Sections(1).Footers(1)
   fdr1.Range.Paste
   With docActive.PageSetup
          .DifferentFirstPageHeaderFooter = False
   End With
End sub

> You are not copying the picture.  You are copying the range of the header
> and pasting it into itself.
[quoted text clipped - 71 lines]
> > .DifferentFirstPageHeaderFooter = False 'Set this to false will put>> >
> > text>> > on first page, else will not.>> > End With>> >>>>>>>
Doug Robbins - Word MVP - 13 Mar 2008 06:31 GMT
I think that you need to declare fdr as a Range

   Dim fdr1 As Range

then use

   Set fdr1 = docActive.Sections(1).Footers(1).Range
   fdr1.Paste

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

> Thank you so much much. The Code works, but I was wondering, the code to
> add
[quoted text clipped - 119 lines]
>> > .DifferentFirstPageHeaderFooter = False 'Set this to false will put>> >
>> > text>> > on first page, else will not.>> > End With>> >>>>>>>
Brandon M. Hunter - 13 Mar 2008 14:04 GMT
It doesn't work. I don't think the Dim Frd1 As Range works for me. Also I've
tried it without the first line and it stills overlaps the image. Is there
another way?

> I think that you need to declare fdr as a Range
>
[quoted text clipped - 128 lines]
> >> > .DifferentFirstPageHeaderFooter = False 'Set this to false will put>> >
> >> > text>> > on first page, else will not.>> > End With>> >>>>>>>
Doug Robbins - Word MVP - 13 Mar 2008 20:58 GMT
If I select a picture in the body of the document and run a macro with the
following code:

Dim fdr1 As Range
Selection.Copy
Set fdr1 = ActiveDocument.Sections(1).Footers(1).Range
fdr1.Paste

the picture is pasted into the footer.

And, if I run a macro with the following code

Dim fdr1 As Range
Selection.Copy
With ActiveDocument.Sections(1)
   Set fdr1 = .Footers(1).Range
   fdr1.Paste
   Set fdr1 = .Headers(1).Range
   fdr1.Paste
End With
End Sub

The picture is pasted into both the header and the footer.

As I have mentioned before, you do not need to open the header/footer pane
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

> It doesn't work. I don't think the Dim Frd1 As Range works for me. Also
> I've
[quoted text clipped - 152 lines]
>> >> > put>> >
>> >> > text>> > on first page, else will not.>> > End With>> >>>>>>>
Brandon M. Hunter - 12 Mar 2008 21:20 GMT
Could you provide some sample code on how to do this?

> Why don't you start with a template that already contains the image? (and
> use code to delete it if there are some occasions when you do not want it.)
[quoted text clipped - 15 lines]
> > on first page, else will not.
> > End With

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.