Watermarks from Addin-code

P

Peter Karlström

Hi

I'm working in a project involving document management thru a VB6
COM Addin to Word XP and Word 2003.

The customer want to be able to add and change the active documents
watermark in a specially designed dialog.
The Watermark is textbased (not images) and the goal is to let the user
change anf maintain the watermark in the document.

My problem is that I used the same code that was recorded with the
macro-recoring utility in Word.
The result is that the Watermark don't appear on all pages after the user
has continued working in the document and for instance added some pages.

What did I do wrong?
Shoul I use a different approach when using this i a COMAddin?

Thanks in advance

Regards
 
S

sbezna

I recommend using DocPoint. It is a simple, robust, and powerful
document management system. It has an addin to all versions of
Microsoft Word, and other Office applications.

See www.docpoint.biz

Hope this helps.
 
P

Peter Karlström

Hi

Sorry, but we (and the customer) have spent about a year in this project and
the COM Addin is so much more than just another document management system.
This one is taylored for the customers needs with about 40 (today) designed
functions.
They are not likely to switch system over night.

Thanks anyway
--
Peter Karlström
Midrange AB
Sweden


sbezna said:
I recommend using DocPoint. It is a simple, robust, and powerful
document management system. It has an addin to all versions of
Microsoft Word, and other Office applications.

See www.docpoint.biz

Hope this helps.
 
C

Cindy M.

Hi Peter,
I'm working in a project involving document management thru a VB6
COM Addin to Word XP and Word 2003.

The customer want to be able to add and change the active documents
watermark in a specially designed dialog.
The Watermark is textbased (not images) and the goal is to let the user
change anf maintain the watermark in the document.

My problem is that I used the same code that was recorded with the
macro-recoring utility in Word.
The result is that the Watermark don't appear on all pages after the user
has continued working in the document and for instance added some pages.

What did I do wrong?
Shoul I use a different approach when using this i a COMAddin?
Really impossible to say without seeing how your code creates and positions
the add-in, and in what Range. Right-off hand, though, I'd guess it's not
being put in the header/footer layer of the document.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
P

Peter Karlström

Hi Cindy

I'll show you the code below.
Some commentaries: ActiveDoc ist the name of the active document
wrdApp is the Word-Object in which the COM Addin operates.

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

'Get selected watermark text from dialog
Select Case selWatermark
Case 0
WMText = lblWatermark.Caption
Case 1
WMText = lstWatermark.Text
Case 2
WMText = txtWatermark.Text
Case 3
GoTo noWM
End Select

wrdApp.ActiveDocument.Sections(1).Range.Select
wrdApp.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

wrdApp.Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject1,
WMText, "Times New Roman", 66, False, False, 0, 0).Select
wrdApp.Selection.ShapeRange.Name = "PowerPlusWaterMarkObject1"
wrdApp.Selection.ShapeRange.TextEffect.NormalizedHeight = False
wrdApp.Selection.ShapeRange.Line.Visible = False
wrdApp.Selection.ShapeRange.Fill.Visible = True
wrdApp.Selection.ShapeRange.Fill.Solid
wrdApp.Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 0, 0)
wrdApp.Selection.ShapeRange.Fill.Transparency = 0.5
wrdApp.Selection.ShapeRange.Rotation = 315
wrdApp.Selection.ShapeRange.LockAspectRatio = True
wrdApp.Selection.ShapeRange.Height = CentimetersToPoints(2.65)
wrdApp.Selection.ShapeRange.Width = CentimetersToPoints(13.33)
wrdApp.Selection.ShapeRange.WrapFormat.AllowOverlap = True
wrdApp.Selection.ShapeRange.WrapFormat.Side = wdWrapNone
wrdApp.Selection.ShapeRange.WrapFormat.Type = 3
wrdApp.Selection.ShapeRange.RelativeHorizontalPosition =
wdRelativeVerticalPositionMargin
wrdApp.Selection.ShapeRange.RelativeVerticalPosition =
wdRelativeVerticalPositionMargin
'wrdApp.Selection.ShapeRange.Left = wdShapeCenter
'wrdApp.Selection.ShapeRange.Top = wdShapeCenter
wrdApp.Selection.ShapeRange.Left = CentimetersToPoints(2)
wrdApp.Selection.ShapeRange.Top = CentimetersToPoints(13.33)
wrdApp.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

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

+ end code +++++++++++++++++++++++++++++++++++++++++++++

This is all the code running when adding a watermark to the active document.

Even if the document consists of 3 sections, when I record a macro which
basically
will return the above code, there is only a Sections(1).Range.Select in the
code, and the watermark is placed in every section.

Regards
 
C

Cindy M.

Hi Peter,
This is all the code running when adding a watermark to the active document.

Even if the document consists of 3 sections, when I record a macro which
basically
will return the above code, there is only a Sections(1).Range.Select in the
code, and the watermark is placed in every section.
The following is the "root of all evil":

wrdApp.ActiveDocument.Sections(1).Range.Select
wrdApp.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

The macro recorder works with the Selection object because it's mimicking what
there user does. But the developer should never work with Selection unless
it's absolutely necessary. You can never be sure what the Selection really is
(what if a user clicks on the document while your code is running?). So it's
often necessary to modify code generated by the macro recorder. You need to
learn how to use Word's objects and the object model directly.

SeekView almost never takes you into the correct header/footer area. If you
want to loop through all sections, then you should proceed more like this:

Dim ActiveDoc as Word.Document
Dim sec as Word.Section
Dim hf as Word.HeaderFooter
Dim shp as Word.Shape

Set ActiveDoc = wrdApp.Windows(ActiveDoc).Document
For each sec in ActiveDoc.Sections
Set hf = sec.Headers(wdHeaderFooterPrimary)
Set shp = hf.Shapes.AddTextEffect('params here)
With shp
'Do stuff here
End With
Next

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
P

Peter Karlström

Hi Cindy

Thanks a million för your response and your cunny support.
This works like a charm.

Regards
 
P

Peter Karlström

Hi again Cindy

Sorry to bother you about this since it seemed to work fine.

I have a problem getting the watermark to stick on page 1.
Below is the complete code I use.
I have tried changing sec.Headers to wdHeaderFooterFirstPage but the effect
is the same.
The page-setup on the documents I have tested has "A different first page"
for Heading/Fotter. Is it here I have a problem?


++ code start +++++
Dim WMText As String 'Text for watermark
Dim AcDoc As Word.Document 'Active document
Dim sec As Word.Section 'Section object
Dim hf As Word.HeaderFooter 'Header/Footer object
Dim shp As Word.Shape 'Watermark object
Dim WMCount As Integer 'Watermark Count

'Turn off document-events
UpdatingDoc = True

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

'Delete active watermarks if they exist
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
For Each sec In AcDoc.Sections
Set hf = sec.Headers(wdHeaderFooterPrimary)
For Each shp In hf.Shapes
If Left(shp.Name, 16) = "SKBMallWatermark" Then
shp.Delete
End If
Next
Next

'Get choosen watermark text
Select Case selWatermark
Case 0
WMText = lblWatermark.Caption
Case 1
WMText = lstWatermark.Text
Case 2
WMText = txtWatermark.Text
Case 3
GoTo noWM
End Select

'Add wetermark to all parts of the document
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 1
For Each sec In AcDoc.Sections
Set hf = sec.Headers(wdHeaderFooterPrimary)
Set shp = hf.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
Next

noWM:
'Get back to users position in document and delete temp bookmark
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Select
wrdApp.Windows(ActiveDoc).Document.Bookmarks("tmpAuto").Delete

'Turn on document-events
UpdatingDoc = False

'Unload dialog
Unload Me
+ code end ++++++++++

Thanks in advance

Regards
 
C

Cindy M.

Hi =?Utf-8?B?UGV0ZXIgS2FybHN0csO2bQ==?=,
I have a problem getting the watermark to stick on page 1.
Below is the complete code I use.
I have tried changing sec.Headers to wdHeaderFooterFirstPage but the effect
is the same.
The page-setup on the documents I have tested has "A different first page"
for Heading/Fotter. Is it here I have a problem?
Show me the code you've tested that addresses both wdHeaderFooterFirstPage as
well as wdHeaderFooterPrimary, please?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
P

Peter Karlström

Hi Cindy

Here is the whole Sub that is running:

+++ start code >>>>>
Dim WMText As String 'Text for watermark
Dim AcDoc As Word.Document 'Active document
Dim sec As Word.Section 'Section
Dim hf As Word.HeaderFooter 'Header/footer
Dim shp As Word.Shape 'Watermark
Dim WMCount As Integer 'Nbr of sections with watermark

'Turn of document events
UpdatingDoc = True

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

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

'Get new watermark setting
Select Case selWatermark
Case 0
WMText = lblWatermark.Caption
Case 1
WMText = lstWatermark.Text
Case 2
WMText = txtWatermark.Text
Case 3
GoTo noWM
End Select

'Add watermark to document
Set AcDoc = wrdApp.Windows(ActiveDoc).Document
WMCount = 1
For Each sec In AcDoc.Sections
Set hf = sec.Headers(wdHeaderFooterPrimary)
Set shp = hf.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 hf = sec.Headers(wdHeaderFooterFirstPage)
Set shp = hf.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
Next

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

UpdatingDoc = False

Unload Me

<<< End code +++++


Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top