does anyone know how to insert on demand an eps logo in the header of a document programatically. I need to be able to insert (or not insert) a file in the header based on a flag setting in a useform but I cannot seem to get the thing to work. Help would be greatly appreciated
Thank you
Hi Robin
Here are two suggestions:
1. If the logo must remain in a separate file (for instance, if it's updated
frequently), you can use code like this:
Sub AddLogoFromFile()
Dim MyRange As Range
Dim MyInlineShape As InlineShape
Set MyRange = Selection.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range
MyRange.Collapse wdCollapseEnd
Set MyInlineShape = MyRange.InlineShapes.AddPicture _
(FileName:="C:\Graphics\NightWatch.jpg", _
linktofile:=False, savewithdocument:=True, _
Range:=MyRange)
With MyInlineShape
.Height = 0.5 * .Height
.Width = 0.5 * .Width
End With
End Sub
Of course, the FileName should point to your file. As long as it's a file
type that Word has a converter for, this will work. You can insert it as an
InlineShape, or you can change the macro to insert it as a floating Shape --
then you'll want to add statements to the With group to set its .Top and
.Left as well.
Also, you may need to change where MyRange is -- possibly in the first-page
header (change the constant to wdHeaderFooterFirstPage) or in some specific
section independent of the Selection.
2. Less convenient for updating but easier to distribute to others, you can
make an AutoText entry containing the pre-sized logo and store it in the
template on which the document will be based. Then the code looks like this:
Sub AddLogoFromAutoText()
Dim MyRange As Range
Dim ATtemplate As Template
Set MyRange = Selection.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range
MyRange.Collapse wdCollapseEnd
Set ATtemplate = ActiveDocument.AttachedTemplate
ATtemplate.AutoTextEntries("NightWatch").Insert _
where:=MyRange, RichText:=True
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
When you want to position something with respect to the page, then you do
need a floating Shape object instead of an InlineShape. Here's a modified
version of the first macro from before:
Sub AddLogoFromFile()
Dim MyRange As Range
Dim MyShape As Shape
Set MyRange = Selection.Sections(1) _
.Headers(wdHeaderFooterPrimary).Range
MyRange.Collapse wdCollapseEnd
Set MyShape = ActiveDocument.Shapes.AddPicture _
(FileName:="C:\Graphics\NightWatch.jpg", _
linktofile:=False, savewithdocument:=True, _
Anchor:=MyRange)
With MyShape
.Height = 0.5 * .Height
.LockAspectRatio = msoTrue
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
.Left = InchesToPoints(1#)
.Top = InchesToPoints(1.25)
End With
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Robin wrote:
> This is some tremendous information. I guess the only question is
> regarding the floating shapes. I am trying to get my logo to be 1.25
> inches from the top of the page and now matter what I seem to do it
> stays at the default (around .75 of an inch). The other two examples
> when modified worked like a charm and I would be in great shape if I
> could just move it down. Is the floating shape where you need to use
> the Shape collection?
>
> Thanks very very much!
Please don't multi-post. I've just wasted my time answering this question in
microsoft.public.word.vba.customization, when Jays provided you with a perfectly
satisfactory answer here.
Thanks - Peter
"Robin" <anon...@discussions.microsoft.com>, said:
>Outstanding. This is perfect. Thank you very much. I have spent a little over 2 weeks on this.
HTH + Cheers - Peter