PDA

View Full Version : Insert and remove watermarks



peterpan66
11-23-2006, 01:33 AM
Hi

I have been trying to use the great "Insert/Remove Watermark" by Lucas.

It works well with documents with 1 or more pages if You have the same header on all pages.

I have a template that is using different first page header.
When I insert the watermark now it works ok with only 1 page in the document.
With 2 or more pages in the document the watermark is only inserted on the page 2-3-4 .... not on the first (different) page.

Can anybody tell what to change in the code to make it work on all pages?

Here is the code used for inserting the watermark:



Sub InsertWaterMark()
Dim strWMName As String

On Error GoTo ErrHandler
'selects all the sheets
ActiveDocument.Sections(1).Range.Select
strWMName = ActiveDocument.Sections(1).Index
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
'Change the text for your watermark here
Selection.HeaderFooter.Shapes.AddTextEffect(msoTextEffect1, _
"DRAFT", "Arial", 1, False, False, 0, 0).Select
With Selection.ShapeRange

.Name = strWMName
.TextEffect.NormalizedHeight = False
.Line.Visible = False

With .Fill

.Visible = True
.Solid
.ForeColor.RGB = Gray
.Transparency = 0.5
End With

.Rotation = 315
.LockAspectRatio = True
.Height = InchesToPoints(2.42)
.Width = InchesToPoints(6.04)

With .WrapFormat
.AllowOverlap = True
.Side = wdWrapNone
.Type = 3

End With

.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin

'If using Word 2000 you may need to comment the 2
'lines above and uncomment the 2 below.

' .RelativeHorizontalPosition = wdRelativeVerticalPositionPage
' .RelativeVerticalPosition = wdRelativeVerticalPositionPage

.Left = wdShapeCenter
.Top = wdShapeCenter
End With

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Exit Sub

ErrHandler:
MsgBox "An error occured trying to insert the watermark." & Chr(13) & _
"Error Number: " & Err.Number & Chr(13) & _
"Decription: " & Err.Description, vbOKOnly + vbCritical, "Error"


End Sub


Enclosed is a zip file containing the documenttemplate incl. the code for Insert/remove watermark.

Bilby
11-23-2006, 04:20 PM
Greetings,

First up the watermarks go into headers which belong to sections & not to the page themselves so you need something like;

For each section in ActiveDocument.Sections
For each header in section.header



Next section

Bilby
11-23-2006, 04:25 PM
Greetings,

First up the watermarks go into headers which belong to sections & not to the page themselves so you need something like;

For each section in ActiveDocument.Sections
For each header in section.header

THIS IS WHERE MY WATERMARK IS CREATED

next header
Next section

So convert your Watermark maker into a procedure that can be called
by the above loop and you should get your watermark into every
header irrespective of page setup and number of sections.

peterpan66
11-24-2006, 12:07 AM
Thanks ....

It sounds very good ..... :think:
and now I just wish that I was a code-wizard .... and I am not .. I am a beginner (as of yesterday)

1. How do I convert the code I have now to a module?

2. How do I call the modul after convertion?

Bilby
11-27-2006, 01:50 PM
Firstly, try not to use Normal.dot as its dangerous. Create a new template and save it in Words startup directory so its available on startup.This will allow you to create a toolbar and add a commandbutton to trigger the macro.

Secondly, Watermarks are an interesting (and frustrating) introduction to VBA. Your in for a interesting time. I've attached your dot (renamed).

The code as presented was a module and could be called simply by using its name.

I've modified it to create a watermark from any string so both the following are valid;

sMark = "draft"
InsertWaterMark sMark or InsertWaterMark "draft"

Good Luck

Bilby
11-27-2006, 01:55 PM
Sorry I forgot the attachment.

peterpan66
11-28-2006, 08:50 AM
Hi and thanks ...

but I still have the same problem.
No watermark on page one if the document has 2,3,4 or more pages?

To replicate:
Open document
just insert a few pagebreaks.
Run the code and you have got watermarks on all pages except page 1.

Am I missing something here?

fumei
11-28-2006, 09:04 AM
You absolutely must grasp how Word handles Sections, and headers/footers.

1. You have Different first page in Page Setup.

This means...Different first page. Which means the headers and footers are different for the other pages, right?

You add page breaks:
To replicate:
Open document
just insert a few pagebreaks.
Run the code and you have got watermarks on all pages except page 1.So after you add pages, where is the Selection? it is, probably, on the last page.

So...ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader does just that. It actions the current page. Which is NOT the first page, so...as the first page IS different, it does not get actioned.

Do not use ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader. This is a View. It uses the Selection.

Use what Bilby suggested. A header/footer object. here is some of your code: 'selects all the sheets
ActiveDocument.Sections(1).Range.Select
strWMName = ActiveDocument.Sections(1).Index
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

First of all...they are not sheets.
Second of all, yes, you ARE selecting the range of the Section, BUT SeekCurrentPageHeader does just that...the current page.

And, "Selection.HeaderFooter.Shapes.AddTextEffect" actions ONLY the header/footer for the page the Selection is on.