Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Adding an image to headers dynamically using VBA Word

920 views
Skip to first unread message

Doug Robbins - Word MVP

unread,
Mar 12, 2008, 3:54:00 PM3/12/08
to
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.)

--
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

"Brandon M. Hunter" <Brandon M. Hun...@discussions.microsoft.com> wrote in
message news:3787A7AE-C934-4CD5...@microsoft.com...
> 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
>


Brandon M. Hunter

unread,
Mar 12, 2008, 4:18:00 PM3/12/08
to
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.

Brandon M. Hunter

unread,
Mar 12, 2008, 4:20:02 PM3/12/08
to
Could you provide some sample code on how to do this?

Doug Robbins - Word MVP

unread,
Mar 12, 2008, 6:50:22 PM3/12/08
to
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

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1,1).Range.InlineShapes.AddPicture Filename:="path\filename"--Hope this helps.Please reply to the newsgroup unless you wish to avail yourself of myservices on a paid consulting basis.Doug Robbins - Word MVP"Brandon M. Hunter" <Brandon...@discussions.microsoft.com> wrote inmessage news:523FFE3F-F65E-4931...@microsoft.com...> 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.>> "Doug Robbins - Word MVP" wrote:>>> 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 wantit.)>>>> -->> 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>>>> "Brandon M. Hunter" <Brandon M. Hun...@discussions.microsoft.com> wrotein>> message news:3787A7AE-C934-4CD5...@microsoft.com...>> > 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 andfooter>> > 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>> >>>>>>>

Brandon M. Hunter

unread,
Mar 12, 2008, 7:05:01 PM3/12/08
to
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

Doug Robbins - Word MVP

unread,
Mar 12, 2008, 8:46:12 PM3/12/08
to
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.
--
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

"Brandon M. Hunter" <Brandon...@discussions.microsoft.com> wrote in
message news:A42ACCA1-5A27-4C20...@microsoft.com...

Brandon M. Hunter

unread,
Mar 12, 2008, 9:07:01 PM3/12/08
to
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

Doug Robbins - Word MVP

unread,
Mar 13, 2008, 1:31:56 AM3/13/08
to
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

--
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

"Brandon M. Hunter" <Brandon...@discussions.microsoft.com> wrote in

message news:BB163EFA-5091-4C6E...@microsoft.com...

Brandon M. Hunter

unread,
Mar 13, 2008, 9:04:01 AM3/13/08
to

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?

Doug Robbins - Word MVP

unread,
Mar 13, 2008, 3:58:23 PM3/13/08
to
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
--
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

"Brandon M. Hunter" <Brandon...@discussions.microsoft.com> wrote in

message news:E13036CD-0297-4D66...@microsoft.com...

0 new messages