PDA

View Full Version : Headers and Watermarks



mikenorgate
11-04-2010, 08:31 AM
Hi,

I need to write a small script that will add an image to the center of the header on each page and also place an image watermark on each page.

Struggling to find examples on the internet so any help greatly appreciated

fumei
11-04-2010, 01:56 PM
Watermarks, by their nature are on each page.

Headers, by their nature, are on each page. Although, it depends on whether you are using DifferentFirstPage and/or DifferntOddEven. If you are, you must insert the header image individually for each of the different headers.

Is there existing content in the header? That is, are you adding the image, or are you just putting in the image.

It makes it easier if you use an explicit Style.


Sub ImageIntoHeader()
Dim oHF As HeaderFooter
Dim oSection As Section
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
With oHF.Range
.Style = "HeaderImage"
.InlineShapes.AddPicture _
FileName:="C:\Gerry\ExplodingHead.gif", _
LinkToFile:=False, SaveWithDocument:=True
End With
Next
Next
End Sub
This makes every header (Primary, DifferentFirstPage, DifferentOddEven), in every Section have the image ExplodingHead.gif inserted, and they are centered, because the Style HeaderImage is applied...and it is centered.

fumei
11-04-2010, 01:58 PM
For the watermark, try recording a macro. It is straightforward. Is it a text watermark, or an image?

Sebastian H
11-04-2010, 02:11 PM
(duplicate message deleted)

mikenorgate
11-05-2010, 01:31 AM
With this I get an "Item with specified name does not exist" error of the .Style = "Header Image" line


Watermarks, by their nature are on each page.

Headers, by their nature, are on each page. Although, it depends on whether you are using DifferentFirstPage and/or DifferntOddEven. If you are, you must insert the header image individually for each of the different headers.

Is there existing content in the header? That is, are you adding the image, or are you just putting in the image.

It makes it easier if you use an explicit Style.


Sub ImageIntoHeader()
Dim oHF As HeaderFooter
Dim oSection As Section
For Each oSection In ActiveDocument.Sections
For Each oHF In oSection.Headers
With oHF.Range
.Style = "HeaderImage"
.InlineShapes.AddPicture _
FileName:="C:\Gerry\ExplodingHead.gif", _
LinkToFile:=False, SaveWithDocument:=True
End With
Next
Next
End Sub
This makes every header (Primary, DifferentFirstPage, DifferentOddEven), in every Section have the image ExplodingHead.gif inserted, and they are centered, because the Style HeaderImage is applied...and it is centered.

It is a image watermark

Sebastian H
11-05-2010, 09:01 AM
Have you tried recording a macro, as fumei suggested?

mikenorgate
11-05-2010, 09:51 AM
Have you tried recording a macro, as fumei suggested?

yes, this works for the watermark but not for the headers

fumei
11-05-2010, 09:58 AM
"because the Style HeaderImage is applied..."

Obviously if you do not have a Style named HeaderImage, then:

"Item with specified name does not exist"

will be the error. It does not exist. Solution? Make it exist. Make a new style, formatted the way you want, and name it whatever you want. I made a Style I named HeaderImage (centered, bordered, with a SpaceFollowing of 10 pts) and used that.

Sebastian H
11-05-2010, 12:00 PM
Or, or course, you can just use a style you already have, and format that accordingly.

fumei
11-05-2010, 12:50 PM
Careful! Technically true. However, if you are talking about a manual change in format for a specific case, this is 100% against the reason for styles.

If you format a style you already have, then THAT format applies in all existing uses of that Style. If this is what you want, then fine. But again, to manually format an existing style to use in a special case is contrary to the use of Styles.

Styles should always, and ONLY, be explicit.

Say you have (as I do) a style (ImageHolder). It is centered.

Now suppose I have a case where there are, oh, 15 images in the document. I want three of them to not be centered, but left-aligned.

Do I manually select those three and make them left-aligned.

NEVER!

Sebastian H
11-05-2010, 01:38 PM
Oh, it seems you took my statement "format that accordingly" to mean to just modify individual paragraphs with that style. That's not what I meant. I meant doing this and then selecting "update to match the selection". Or alternatively, modify the style itself. What I was trying to say was that if Mike always wants to use the same heading style then he can just stick with the default style "Header", and doesn't have to add a new style.

Mike: Please don't let that side conversation about styles distract you from your original question. I still don't quite understand why you need VBA in the first place. As fumei (Gerry) wrote,
Headers, by their nature, are on each page. So, if you insert a picture in a header, it will show on every page. You only need to to that once. Why do you need a macro for that?