Discussion:
Watermarks on EVERY page
(too old to reply)
Peter Karlström
2007-10-12 07:39:01 UTC
Permalink
Hi

I have a problem with watermarks in documents.
I have developed a COM-Addin for Word XP/2003 with a lot of functionality.
One of them is a customized watermark in the documents.
The user opens a dialogue where they choose one of 3 types of text for the
watermark, or to have no watermark.

When they click OK the watermark is created on page 2 and forward.
Nothing attaches to the first page.
In the code below I have tested both wdHeaderFooterFirstPage and
wdHeaderFooterPrimary with the same result. If I use both I get doubled
watermark on page 2 and forward.

What have I made wrong, or is this a bug?

Thanks in advance

+++++++++ START CODE +++++++++++++++
Private Sub cmdOK_Click()

Dim WMText As String 'Text for watermark
Dim AcDoc As Word.Document 'Active document
Dim sec As Word.Section 'Section
Dim hfPri As Word.HeaderFooter 'Primary header/footer
Dim hfFrst As Word.HeaderFooter 'First header/footer
Dim shp As Word.Shape 'Watermark shape
Dim WMCount As Integer 'Nbr of watermarks
Dim cm As ADODB.Command 'ADODB Command
Dim rs As ADODB.Recordset 'ADODB Recordset


'Mark position in document
wrdApp.Windows(ActiveDoc).Document.Bookmarks.Add ("tmpAuto")

'Delete any existing watermark
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
For Each sec In AcDoc.Sections
Set hfPri = sec.Headers(wdHeaderFooterPrimary)
For Each shp In hfPri.Shapes
If Left(shp.Name, 16) = "SKBMallWatermark" Then
shp.Delete
End If
Next
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
For Each shp In hfFrst.Shapes
If Left(shp.Name, 16) = "SKBMallWatermark" Then
shp.Delete
End If
Next
Next

Set AcDoc = Nothing
Set hfPri = Nothing
Set hfFrst = Nothing

'Get user choice of watermark
Select Case selWatermark
Case 0 'Security class
WMText = lblWatermark.Caption

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "0"

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = WMText
Case 1 'Database text
If lstWatermark.SelCount = 0 Then
MsgBox "You must choose a text for watermark in the list.",
vbOKOnly, App.ProductName & "Ver: " & App.Major & "." & App.Minor & "." &
App.Revision
Exit Sub
End If
'Initiera databaskoppling
Set cm = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")
cm.CommandType = 1
Set cm.ActiveConnection = cn

'Get watermark from database
cm.CommandText = "SELECT * from tblWWatermark Where fldID = " &
lstWatermark.ItemData(lstWatermark.ListIndex)
rs.Open cm, , adOpenDynamic, adLockReadOnly
If Not rs.BOF And Not rs.EOF Then
Select Case
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_Språk")
Case "0"
WMText = Trim(rs("fldHeadingSve"))
Case "1"
WMText = Trim(rs("fldHeadingEng"))
End Select
Else
WMText = lstWatermark.Text
End If
rs.Close

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "1"

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = WMText
Set cm = Nothing
Set rs = Nothing
Case 2 'Free text
If Trim(txtWatermark.Text) = "" Then
MsgBox "You must write a text for watermark!", vbOKOnly,
App.ProductName & "Ver: " & App.Major & "." & App.Minor & "." & App.Revision
txtWatermark.SetFocus
Exit Sub
End If
WMText = txtWatermark.Text

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "2"

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = WMText
Case 3 'No watermark

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "3"

wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = ""
GoTo noWM
End Select

'Add watermark to all parts of the document
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 1
For Each sec In AcDoc.Sections
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
Set shp = hfFrst.Shapes.AddTextEffect(PowerPlusWaterMarkObject1,
WMText, "Times New Roman", 66, False, False, 0, 0)
With shp
.Name = "SKBMallWatermark" & CStr(WMCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.Height = CentimetersToPoints(2.65)
.Width = CentimetersToPoints(13.33)
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = CentimetersToPoints(2)
.Top = CentimetersToPoints(13.33)
WMCount = WMCount + 1
End With
Set hfFrst = Nothing
Set shp = Nothing
Next

Set AcDoc = Nothing

noWM:
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Select
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Delete

Unload Me

End Sub
+++++++++ END CODE +++++++++++++++
--
Peter Karlström
Midrange AB
Sweden
Jean-Guy Marcil
2007-10-12 14:11:28 UTC
Permalink
Post by Peter Karlström
Hi
I have a problem with watermarks in documents.
I have developed a COM-Addin for Word XP/2003 with a lot of
functionality.
One of them is a customized watermark in the documents.
The user opens a dialogue where they choose one of 3 types of text for the
watermark, or to have no watermark.
When they click OK the watermark is created on page 2 and forward.
Nothing attaches to the first page.
Not sure I follow, according to your code:

'Add watermark to all parts of the document
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 1
For Each sec In AcDoc.Sections
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)

the watermark will be added to the first page if it has a first page
header/footer.
Post by Peter Karlström
In the code below I have tested both wdHeaderFooterFirstPage and
wdHeaderFooterPrimary with the same result. If I use both I get doubled
watermark on page 2 and forward.
I do not get hat you mean by:
"If I use both I get doubled watermark (...)"

You mean you use both type of header/footer in the code or it you mean it
doesn't matter which one you use? You get two watermarks shape on top of
each other?
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
***@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
Peter Karlström
2007-10-12 15:14:00 UTC
Permalink
Hi

What I mean is that I have tried switching the headertype from
wdHeaderFooterFirstPage to wdHeaderFooterPrimary with the same result (No
watermark on page 1).
I have also tested to set the watermark on both wdHeaderFooterFirstPage AND
wdHeaderFooterPrimary but then there is two watermarks on every page, but
still no one on page one.

Got a clue?
--
Peter Karlström
Midrange AB
Sweden
Post by Peter Karlström
Post by Peter Karlström
Hi
I have a problem with watermarks in documents.
I have developed a COM-Addin for Word XP/2003 with a lot of
functionality.
One of them is a customized watermark in the documents.
The user opens a dialogue where they choose one of 3 types of text for the
watermark, or to have no watermark.
When they click OK the watermark is created on page 2 and forward.
Nothing attaches to the first page.
'Add watermark to all parts of the document
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 1
For Each sec In AcDoc.Sections
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
the watermark will be added to the first page if it has a first page
header/footer.
Post by Peter Karlström
In the code below I have tested both wdHeaderFooterFirstPage and
wdHeaderFooterPrimary with the same result. If I use both I get doubled
watermark on page 2 and forward.
"If I use both I get doubled watermark (...)"
You mean you use both type of header/footer in the code or it you mean it
doesn't matter which one you use? You get two watermarks shape on top of
each other?
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
Word MVP site: http://www.word.mvps.org
Jonathan West
2007-10-12 14:18:55 UTC
Permalink
I suspect the problem is that you have multiple sections in your document.
Having multiple sections doesn't mean that different sections have different
headers & footers. Whether a section's header & footer is different can be
determined by the header or footer's LinkToPrevious property. If
LinkToPrevious is True, there is no need to insert an additional watermark
in that header or footer, since the one from the previous section will also
display here.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Post by Peter Karlström
Hi
I have a problem with watermarks in documents.
I have developed a COM-Addin for Word XP/2003 with a lot of functionality.
One of them is a customized watermark in the documents.
The user opens a dialogue where they choose one of 3 types of text for the
watermark, or to have no watermark.
When they click OK the watermark is created on page 2 and forward.
Nothing attaches to the first page.
In the code below I have tested both wdHeaderFooterFirstPage and
wdHeaderFooterPrimary with the same result. If I use both I get doubled
watermark on page 2 and forward.
What have I made wrong, or is this a bug?
Thanks in advance
+++++++++ START CODE +++++++++++++++
Private Sub cmdOK_Click()
Dim WMText As String 'Text for watermark
Dim AcDoc As Word.Document 'Active document
Dim sec As Word.Section 'Section
Dim hfPri As Word.HeaderFooter 'Primary header/footer
Dim hfFrst As Word.HeaderFooter 'First header/footer
Dim shp As Word.Shape 'Watermark shape
Dim WMCount As Integer 'Nbr of watermarks
Dim cm As ADODB.Command 'ADODB Command
Dim rs As ADODB.Recordset 'ADODB Recordset
'Mark position in document
wrdApp.Windows(ActiveDoc).Document.Bookmarks.Add ("tmpAuto")
'Delete any existing watermark
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
For Each sec In AcDoc.Sections
Set hfPri = sec.Headers(wdHeaderFooterPrimary)
For Each shp In hfPri.Shapes
If Left(shp.Name, 16) = "SKBMallWatermark" Then
shp.Delete
End If
Next
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
For Each shp In hfFrst.Shapes
If Left(shp.Name, 16) = "SKBMallWatermark" Then
shp.Delete
End If
Next
Next
Set AcDoc = Nothing
Set hfPri = Nothing
Set hfFrst = Nothing
'Get user choice of watermark
Select Case selWatermark
Case 0 'Security class
WMText = lblWatermark.Caption
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "0"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = WMText
Case 1 'Database text
If lstWatermark.SelCount = 0 Then
MsgBox "You must choose a text for watermark in the list.",
vbOKOnly, App.ProductName & "Ver: " & App.Major & "." & App.Minor & "." &
App.Revision
Exit Sub
End If
'Initiera databaskoppling
Set cm = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")
cm.CommandType = 1
Set cm.ActiveConnection = cn
'Get watermark from database
cm.CommandText = "SELECT * from tblWWatermark Where fldID = " &
lstWatermark.ItemData(lstWatermark.ListIndex)
rs.Open cm, , adOpenDynamic, adLockReadOnly
If Not rs.BOF And Not rs.EOF Then
Select Case
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_Språk")
Case "0"
WMText = Trim(rs("fldHeadingSve"))
Case "1"
WMText = Trim(rs("fldHeadingEng"))
End Select
Else
WMText = lstWatermark.Text
End If
rs.Close
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "1"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = WMText
Set cm = Nothing
Set rs = Nothing
Case 2 'Free text
If Trim(txtWatermark.Text) = "" Then
MsgBox "You must write a text for watermark!", vbOKOnly,
App.ProductName & "Ver: " & App.Major & "." & App.Minor & "." & App.Revision
txtWatermark.SetFocus
Exit Sub
End If
WMText = txtWatermark.Text
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "2"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = WMText
Case 3 'No watermark
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "3"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = ""
GoTo noWM
End Select
'Add watermark to all parts of the document
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 1
For Each sec In AcDoc.Sections
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
Set shp = hfFrst.Shapes.AddTextEffect(PowerPlusWaterMarkObject1,
WMText, "Times New Roman", 66, False, False, 0, 0)
With shp
.Name = "SKBMallWatermark" & CStr(WMCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.Height = CentimetersToPoints(2.65)
.Width = CentimetersToPoints(13.33)
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = CentimetersToPoints(2)
.Top = CentimetersToPoints(13.33)
WMCount = WMCount + 1
End With
Set hfFrst = Nothing
Set shp = Nothing
Next
Set AcDoc = Nothing
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Select
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Delete
Unload Me
End Sub
+++++++++ END CODE +++++++++++++++
--
Peter Karlström
Midrange AB
Sweden
Peter Karlström
2007-10-12 15:20:01 UTC
Permalink
Hi Jonathan

No, actually on the documents I have tested there is only one single section.
but the documents are created with a "Different first page".

Do you think that the property LinkToPrevious can prevent the watermark on
page 1?

Thanks in advance
--
Peter Karlström
Midrange AB
Sweden
Post by Jonathan West
I suspect the problem is that you have multiple sections in your document.
Having multiple sections doesn't mean that different sections have different
headers & footers. Whether a section's header & footer is different can be
determined by the header or footer's LinkToPrevious property. If
LinkToPrevious is True, there is no need to insert an additional watermark
in that header or footer, since the one from the previous section will also
display here.
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Post by Peter Karlström
Hi
I have a problem with watermarks in documents.
I have developed a COM-Addin for Word XP/2003 with a lot of functionality.
One of them is a customized watermark in the documents.
The user opens a dialogue where they choose one of 3 types of text for the
watermark, or to have no watermark.
When they click OK the watermark is created on page 2 and forward.
Nothing attaches to the first page.
In the code below I have tested both wdHeaderFooterFirstPage and
wdHeaderFooterPrimary with the same result. If I use both I get doubled
watermark on page 2 and forward.
What have I made wrong, or is this a bug?
Thanks in advance
+++++++++ START CODE +++++++++++++++
Private Sub cmdOK_Click()
Dim WMText As String 'Text for watermark
Dim AcDoc As Word.Document 'Active document
Dim sec As Word.Section 'Section
Dim hfPri As Word.HeaderFooter 'Primary header/footer
Dim hfFrst As Word.HeaderFooter 'First header/footer
Dim shp As Word.Shape 'Watermark shape
Dim WMCount As Integer 'Nbr of watermarks
Dim cm As ADODB.Command 'ADODB Command
Dim rs As ADODB.Recordset 'ADODB Recordset
'Mark position in document
wrdApp.Windows(ActiveDoc).Document.Bookmarks.Add ("tmpAuto")
'Delete any existing watermark
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
For Each sec In AcDoc.Sections
Set hfPri = sec.Headers(wdHeaderFooterPrimary)
For Each shp In hfPri.Shapes
If Left(shp.Name, 16) = "SKBMallWatermark" Then
shp.Delete
End If
Next
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
For Each shp In hfFrst.Shapes
If Left(shp.Name, 16) = "SKBMallWatermark" Then
shp.Delete
End If
Next
Next
Set AcDoc = Nothing
Set hfPri = Nothing
Set hfFrst = Nothing
'Get user choice of watermark
Select Case selWatermark
Case 0 'Security class
WMText = lblWatermark.Caption
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "0"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText")
= WMText
Case 1 'Database text
If lstWatermark.SelCount = 0 Then
MsgBox "You must choose a text for watermark in the list.",
vbOKOnly, App.ProductName & "Ver: " & App.Major & "." & App.Minor & "." &
App.Revision
Exit Sub
End If
'Initiera databaskoppling
Set cm = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")
cm.CommandType = 1
Set cm.ActiveConnection = cn
'Get watermark from database
cm.CommandText = "SELECT * from tblWWatermark Where fldID = " &
lstWatermark.ItemData(lstWatermark.ListIndex)
rs.Open cm, , adOpenDynamic, adLockReadOnly
If Not rs.BOF And Not rs.EOF Then
Select Case
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_Språk")
Case "0"
WMText = Trim(rs("fldHeadingSve"))
Case "1"
WMText = Trim(rs("fldHeadingEng"))
End Select
Else
WMText = lstWatermark.Text
End If
rs.Close
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "1"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText")
= WMText
Set cm = Nothing
Set rs = Nothing
Case 2 'Free text
If Trim(txtWatermark.Text) = "" Then
MsgBox "You must write a text for watermark!", vbOKOnly,
App.ProductName & "Ver: " & App.Major & "." & App.Minor & "." & App.Revision
txtWatermark.SetFocus
Exit Sub
End If
WMText = txtWatermark.Text
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "2"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText")
= WMText
Case 3 'No watermark
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkType") = "3"
wrdApp.Windows(ActiveDoc).Document.CustomDocumentProperties("su_WatermarkText") = ""
GoTo noWM
End Select
'Add watermark to all parts of the document
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 1
For Each sec In AcDoc.Sections
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
Set shp = hfFrst.Shapes.AddTextEffect(PowerPlusWaterMarkObject1,
WMText, "Times New Roman", 66, False, False, 0, 0)
With shp
.Name = "SKBMallWatermark" & CStr(WMCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.Height = CentimetersToPoints(2.65)
.Width = CentimetersToPoints(13.33)
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = CentimetersToPoints(2)
.Top = CentimetersToPoints(13.33)
WMCount = WMCount + 1
End With
Set hfFrst = Nothing
Set shp = Nothing
Next
Set AcDoc = Nothing
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Select
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Delete
Unload Me
End Sub
+++++++++ END CODE +++++++++++++++
--
Peter Karlström
Midrange AB
Sweden
Jonathan West
2007-10-12 15:44:32 UTC
Permalink
Post by Peter Karlström
Hi Jonathan
No, actually on the documents I have tested there is only one single section.
but the documents are created with a "Different first page".
Do you think that the property LinkToPrevious can prevent the watermark on
page 1?
Ah, I've taken a look and I think I see the real problem. In the
AddTextEffect method, you have neglected to specify an Anchor (i.e. the
Range of the paragraph the shape is to be anchored to). In the absence of
one, the anchor is the Selection - even if it is not in the same heading as
the heading whose Shapes collection you are adding to.

It wasn't immediately obvious as you didn't use named arguments. For any
complex method with multiple arguments, I would recommend you always name
the arguments instead of relying on order. Although it makes the code more
verbose, it also makes it much more readable!
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Peter Karlström
2007-10-12 18:59:00 UTC
Permalink
THANK YOU Jonathan

You are one sharp eyed MVP, I must say.

I now have a working Add-in which places watermarks on every page in
the document.
But I had to do it the "old way" for pages 2 and forward, i.e. without the
Ancor
part.

Thanks a million for your cunny help.
--
Peter Karlström
Midrange AB
Sweden
Post by Jonathan West
Post by Peter Karlström
Hi Jonathan
No, actually on the documents I have tested there is only one single section.
but the documents are created with a "Different first page".
Do you think that the property LinkToPrevious can prevent the watermark on
page 1?
Ah, I've taken a look and I think I see the real problem. In the
AddTextEffect method, you have neglected to specify an Anchor (i.e. the
Range of the paragraph the shape is to be anchored to). In the absence of
one, the anchor is the Selection - even if it is not in the same heading as
the heading whose Shapes collection you are adding to.
It wasn't immediately obvious as you didn't use named arguments. For any
complex method with multiple arguments, I would recommend you always name
the arguments instead of relying on order. Although it makes the code more
verbose, it also makes it much more readable!
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Michael Mogensen
2008-05-23 09:48:01 UTC
Permalink
Hi Peter,

I have the same problem here and I'm not 100% sure about the anchor-thingy.
Can I ask you to post the final add/remove WM-VBA in this thread so I can use
it also.

Thanx' in advance,

Kind regards,
Michael Mogensen, dk.
Post by Peter Karlström
THANK YOU Jonathan
You are one sharp eyed MVP, I must say.
I now have a working Add-in which places watermarks on every page in
the document.
But I had to do it the "old way" for pages 2 and forward, i.e. without the
Ancor
part.
Thanks a million for your cunny help.
--
Peter Karlström
Midrange AB
Sweden
Post by Jonathan West
Post by Peter Karlström
Hi Jonathan
No, actually on the documents I have tested there is only one single section.
but the documents are created with a "Different first page".
Do you think that the property LinkToPrevious can prevent the watermark on
page 1?
Ah, I've taken a look and I think I see the real problem. In the
AddTextEffect method, you have neglected to specify an Anchor (i.e. the
Range of the paragraph the shape is to be anchored to). In the absence of
one, the anchor is the Selection - even if it is not in the same heading as
the heading whose Shapes collection you are adding to.
It wasn't immediately obvious as you didn't use named arguments. For any
complex method with multiple arguments, I would recommend you always name
the arguments instead of relying on order. Although it makes the code more
verbose, it also makes it much more readable!
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Peter Karlström
2008-05-23 11:38:01 UTC
Permalink
Hi Michael

Sure thing. Below is the code that generates a watermark on page 1 and
forward.
Notice that the ancor part is not used in HeaderFooterFirstPage, only in
HeaderFooterPrimary.

Variables used in the code is:
wrdApp = Word.Application object
ActiveDoc = string name of active document (wrdApp.ActiveDocument.Name)
WMText = "Text to be displayed in watermark"


+++++ Code start +++++
'Add watermark to HeaderFooterPrimary
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 0
For Each sec In AcDoc.Sections
WMCount = WMCount + 1
Set hfPri = sec.Headers(wdHeaderFooterPrimary)
Set shpPri = hfPri.Shapes.AddTextEffect(PowerPlusWaterMarkObject1,
WMText, "Times New Roman", 66, False, False, 0, 0, hfPri.Range)
With shpPri
.Name = "obj_Watermark" & CStr(WMCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.Height = wrdApp.CentimetersToPoints(2.65)
.Width = wrdApp.CentimetersToPoints(13.33)
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wrdApp.CentimetersToPoints(2)
.Top = wrdApp.CentimetersToPoints(13.33)
End With
Set hfPri = Nothing
Set shpPri = Nothing

'Add watermark to HeaderFooterFirstPage
WMCount = WMCount + 1
Set hfFrst = sec.Headers(wdHeaderFooterFirstPage)
Set shpFrst = hfFrst.Shapes.AddTextEffect(PowerPlusWaterMarkObject1,
WMText, "Times New Roman", 66, False, False, 0, 0)
With shpFrst
.Name = "obj_Watermark" & CStr(WMCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.Height = wrdApp.CentimetersToPoints(2.65)
.Width = wrdApp.CentimetersToPoints(13.33)
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Left = wrdApp.CentimetersToPoints(2)
.Top = wrdApp.CentimetersToPoints(13.33)
End With
Set hfFrst = Nothing
Set shpFrst = Nothing
Next

Set AcDoc = Nothing
+++++ Code end +++++

Regards
--
Peter Karlström
Midrange AB
Sweden
Post by Michael Mogensen
Hi Peter,
I have the same problem here and I'm not 100% sure about the anchor-thingy.
Can I ask you to post the final add/remove WM-VBA in this thread so I can use
it also.
Thanx' in advance,
Kind regards,
Michael Mogensen, dk.
Post by Peter Karlström
THANK YOU Jonathan
You are one sharp eyed MVP, I must say.
I now have a working Add-in which places watermarks on every page in
the document.
But I had to do it the "old way" for pages 2 and forward, i.e. without the
Ancor
part.
Thanks a million for your cunny help.
--
Peter Karlström
Midrange AB
Sweden
Post by Jonathan West
Post by Peter Karlström
Hi Jonathan
No, actually on the documents I have tested there is only one single section.
but the documents are created with a "Different first page".
Do you think that the property LinkToPrevious can prevent the watermark on
page 1?
Ah, I've taken a look and I think I see the real problem. In the
AddTextEffect method, you have neglected to specify an Anchor (i.e. the
Range of the paragraph the shape is to be anchored to). In the absence of
one, the anchor is the Selection - even if it is not in the same heading as
the heading whose Shapes collection you are adding to.
It wasn't immediately obvious as you didn't use named arguments. For any
complex method with multiple arguments, I would recommend you always name
the arguments instead of relying on order. Although it makes the code more
verbose, it also makes it much more readable!
--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Michael Mogensen
2008-05-23 12:57:00 UTC
Permalink
Hi Peter,

Thanx' first of all. Think the point is to use wdHeaderFooterPrimary AND
wdHeaderFooterFirstPage and repeat - that I do - like you do.

Though I can *only* get this working iif I have:

"OddEven = False"
"DifferentFirst = False"

Not with other combinations, that is:

"OddEven = True"
"DifferentFirst = False"

"OddEven = False"
"DifferentFirst = True"

"OddEven = True"
"DifferentFirst = True"

Still I don't understand the anchor thing the other talked about. As you can
see I iterate all sections like you...

(Word 2003 SP3)

VBA:

Private Function AddWatermark(strText As String)

Dim MyDocument As Word.Document
Set MyDocument = ActiveDocument

Dim MySection As Word.Section
Dim MyHeaderFooter As Word.HeaderFooter
Dim MyShape As Word.Shape

Dim iPaperWidthInPoints As Integer
iPaperWidthInPoints = ActiveDocument.PageSetup.PageWidth -
ActiveDocument.PageSetup.LeftMargin - ActiveDocument.PageSetup.RightMargin
Dim iPaperHeightInPoints As Integer
iPaperHeightInPoints = ActiveDocument.PageSetup.PageHeight -
ActiveDocument.PageSetup.TopMargin - ActiveDocument.PageSetup.BottomMargin

Dim vWhatHeaderFooter As Variant
Dim vWhatHeaderFooterArr(1) As Variant
vWhatHeaderFooterArr(0) = wdHeaderFooterPrimary
vWhatHeaderFooterArr(1) = wdHeaderFooterFirstPage
'vWhatHeaderFooterArr(2) = wdHeaderFooterEvenPages

Dim iWatermarkCount As Integer
iWatermarkCount = 0

For Each MySection In MyDocument.Sections
For Each vWhatHeaderFooter In vWhatHeaderFooterArr
Set MyHeaderFooter = MySection.Headers(vWhatHeaderFooter)
Set MyShape = MyHeaderFooter.Shapes.AddTextEffect( _
msoTextEffect1, strText, "Times New Roman", 66, False,
False, 0, 0)
With MyShape
.Name = "MyWatermark" + CStr(iWatermarkCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Width = CentimetersToPoints(13.33)
.Height = CentimetersToPoints(2.65)
.Left = (iPaperWidthInPoints - .Width) / 2
.Top = (iPaperHeightInPoints - .Height) / 2
End With
iWatermarkCount = iWatermarkCount + 1
Next
Next

End Function

Private Function DeleteWatermark()

Dim MyDocument As Word.Document
Set MyDocument = ActiveDocument

Dim MySection As Word.Section
Dim MyHeaderFooter As Word.HeaderFooter
Dim MyShape As Word.Shape

Dim vWhatHeaderFooter As Variant
Dim vWhatHeaderFooterArr(1) As Variant
vWhatHeaderFooterArr(0) = wdHeaderFooterPrimary
vWhatHeaderFooterArr(1) = wdHeaderFooterFirstPage
'vWhatHeaderFooterArr(2) = wdHeaderFooterEvenPages

Dim iWatermarkCount As Integer
iWatermarkCount = 0

For Each MySection In MyDocument.Sections
For Each vWhatHeaderFooter In vWhatHeaderFooterArr
Set MyHeaderFooter = MySection.Headers(g_iWhatHeaderFooter)
For Each MyShape In MyHeaderFooter.Shapes
With MyShape
If (.Name = "MyWatermark" + CStr(iWatermarkCount)) Then
.Delete
End If
End With
Next
iWatermarkCount = iWatermarkCount + 1
Next
Next

End Function


Kine regards,
Michael Mogensen.
Peter Karlström
2008-05-23 14:33:01 UTC
Permalink
Hi Michael

I see now that you have missed the ancor parameter. On the line
Set MyShape = MyHeaderFooter.Shapes.AddTextEffect(msoTextEffect1, _ strText,
"Times New Roman", 66, False, False, 0, 0)
you have the ancor parameter last, like this:
......."Times New Roman", 66, False, False, 0, 0, Ancor.Parameter)

Look at my code and you will see how to use it.

Regards
--
Peter Karlström
Midrange AB
Sweden
Post by Michael Mogensen
Hi Peter,
Thanx' first of all. Think the point is to use wdHeaderFooterPrimary AND
wdHeaderFooterFirstPage and repeat - that I do - like you do.
"OddEven = False"
"DifferentFirst = False"
"OddEven = True"
"DifferentFirst = False"
"OddEven = False"
"DifferentFirst = True"
"OddEven = True"
"DifferentFirst = True"
Still I don't understand the anchor thing the other talked about. As you can
see I iterate all sections like you...
(Word 2003 SP3)
Private Function AddWatermark(strText As String)
Dim MyDocument As Word.Document
Set MyDocument = ActiveDocument
Dim MySection As Word.Section
Dim MyHeaderFooter As Word.HeaderFooter
Dim MyShape As Word.Shape
Dim iPaperWidthInPoints As Integer
iPaperWidthInPoints = ActiveDocument.PageSetup.PageWidth -
ActiveDocument.PageSetup.LeftMargin - ActiveDocument.PageSetup.RightMargin
Dim iPaperHeightInPoints As Integer
iPaperHeightInPoints = ActiveDocument.PageSetup.PageHeight -
ActiveDocument.PageSetup.TopMargin - ActiveDocument.PageSetup.BottomMargin
Dim vWhatHeaderFooter As Variant
Dim vWhatHeaderFooterArr(1) As Variant
vWhatHeaderFooterArr(0) = wdHeaderFooterPrimary
vWhatHeaderFooterArr(1) = wdHeaderFooterFirstPage
'vWhatHeaderFooterArr(2) = wdHeaderFooterEvenPages
Dim iWatermarkCount As Integer
iWatermarkCount = 0
For Each MySection In MyDocument.Sections
For Each vWhatHeaderFooter In vWhatHeaderFooterArr
Set MyHeaderFooter = MySection.Headers(vWhatHeaderFooter)
Set MyShape = MyHeaderFooter.Shapes.AddTextEffect( _
msoTextEffect1, strText, "Times New Roman", 66, False,
False, 0, 0)
With MyShape
.Name = "MyWatermark" + CStr(iWatermarkCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Width = CentimetersToPoints(13.33)
.Height = CentimetersToPoints(2.65)
.Left = (iPaperWidthInPoints - .Width) / 2
.Top = (iPaperHeightInPoints - .Height) / 2
End With
iWatermarkCount = iWatermarkCount + 1
Next
Next
End Function
Private Function DeleteWatermark()
Dim MyDocument As Word.Document
Set MyDocument = ActiveDocument
Dim MySection As Word.Section
Dim MyHeaderFooter As Word.HeaderFooter
Dim MyShape As Word.Shape
Dim vWhatHeaderFooter As Variant
Dim vWhatHeaderFooterArr(1) As Variant
vWhatHeaderFooterArr(0) = wdHeaderFooterPrimary
vWhatHeaderFooterArr(1) = wdHeaderFooterFirstPage
'vWhatHeaderFooterArr(2) = wdHeaderFooterEvenPages
Dim iWatermarkCount As Integer
iWatermarkCount = 0
For Each MySection In MyDocument.Sections
For Each vWhatHeaderFooter In vWhatHeaderFooterArr
Set MyHeaderFooter = MySection.Headers(g_iWhatHeaderFooter)
For Each MyShape In MyHeaderFooter.Shapes
With MyShape
If (.Name = "MyWatermark" + CStr(iWatermarkCount)) Then
.Delete
End If
End With
Next
iWatermarkCount = iWatermarkCount + 1
Next
Next
End Function
Kine regards,
Michael Mogensen.
Michael Mogensen
2008-05-26 09:39:01 UTC
Permalink
Hi Peter,

Thanx' for your kind replies and for your patience with me.

My conclusion after trying your code out though - even without any changes -
is that *I* can't get it to work :-(

I changed the anchor thing and I even copy/pasted your code. After that
changes to the header-style still affects the way watermarks are displayed.
Therefore I'm forced to go for a less-good solution - namely to work with
wordart to mimic a wm.

Thanx' again.


Kind regards,
Michael M. dk.
Post by Peter Karlström
Hi Michael
I see now that you have missed the ancor parameter. On the line
Set MyShape = MyHeaderFooter.Shapes.AddTextEffect(msoTextEffect1, _ strText,
"Times New Roman", 66, False, False, 0, 0)
......."Times New Roman", 66, False, False, 0, 0, Ancor.Parameter)
Look at my code and you will see how to use it.
Regards
--
Peter Karlström
Midrange AB
Sweden
Post by Michael Mogensen
Hi Peter,
Thanx' first of all. Think the point is to use wdHeaderFooterPrimary AND
wdHeaderFooterFirstPage and repeat - that I do - like you do.
"OddEven = False"
"DifferentFirst = False"
"OddEven = True"
"DifferentFirst = False"
"OddEven = False"
"DifferentFirst = True"
"OddEven = True"
"DifferentFirst = True"
Still I don't understand the anchor thing the other talked about. As you can
see I iterate all sections like you...
(Word 2003 SP3)
Private Function AddWatermark(strText As String)
Dim MyDocument As Word.Document
Set MyDocument = ActiveDocument
Dim MySection As Word.Section
Dim MyHeaderFooter As Word.HeaderFooter
Dim MyShape As Word.Shape
Dim iPaperWidthInPoints As Integer
iPaperWidthInPoints = ActiveDocument.PageSetup.PageWidth -
ActiveDocument.PageSetup.LeftMargin - ActiveDocument.PageSetup.RightMargin
Dim iPaperHeightInPoints As Integer
iPaperHeightInPoints = ActiveDocument.PageSetup.PageHeight -
ActiveDocument.PageSetup.TopMargin - ActiveDocument.PageSetup.BottomMargin
Dim vWhatHeaderFooter As Variant
Dim vWhatHeaderFooterArr(1) As Variant
vWhatHeaderFooterArr(0) = wdHeaderFooterPrimary
vWhatHeaderFooterArr(1) = wdHeaderFooterFirstPage
'vWhatHeaderFooterArr(2) = wdHeaderFooterEvenPages
Dim iWatermarkCount As Integer
iWatermarkCount = 0
For Each MySection In MyDocument.Sections
For Each vWhatHeaderFooter In vWhatHeaderFooterArr
Set MyHeaderFooter = MySection.Headers(vWhatHeaderFooter)
Set MyShape = MyHeaderFooter.Shapes.AddTextEffect( _
msoTextEffect1, strText, "Times New Roman", 66, False,
False, 0, 0)
With MyShape
.Name = "MyWatermark" + CStr(iWatermarkCount)
.TextEffect.NormalizedHeight = False
.Line.Visible = False
.Fill.Visible = True
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 0, 0)
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = True
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapNone
.WrapFormat.Type = 3
.RelativeHorizontalPosition = wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
.Width = CentimetersToPoints(13.33)
.Height = CentimetersToPoints(2.65)
.Left = (iPaperWidthInPoints - .Width) / 2
.Top = (iPaperHeightInPoints - .Height) / 2
End With
iWatermarkCount = iWatermarkCount + 1
Next
Next
End Function
Private Function DeleteWatermark()
Dim MyDocument As Word.Document
Set MyDocument = ActiveDocument
Dim MySection As Word.Section
Dim MyHeaderFooter As Word.HeaderFooter
Dim MyShape As Word.Shape
Dim vWhatHeaderFooter As Variant
Dim vWhatHeaderFooterArr(1) As Variant
vWhatHeaderFooterArr(0) = wdHeaderFooterPrimary
vWhatHeaderFooterArr(1) = wdHeaderFooterFirstPage
'vWhatHeaderFooterArr(2) = wdHeaderFooterEvenPages
Dim iWatermarkCount As Integer
iWatermarkCount = 0
For Each MySection In MyDocument.Sections
For Each vWhatHeaderFooter In vWhatHeaderFooterArr
Set MyHeaderFooter = MySection.Headers(g_iWhatHeaderFooter)
For Each MyShape In MyHeaderFooter.Shapes
With MyShape
If (.Name = "MyWatermark" + CStr(iWatermarkCount)) Then
.Delete
End If
End With
Next
iWatermarkCount = iWatermarkCount + 1
Next
Next
End Function
Kine regards,
Michael Mogensen.
Loading...