Adding a watermark using VBA

R

Robin

I have a document that has a header in it. I want to add a watermark from my
building blocks into that header, without erasing or replacing the existing
header. This means I would show the header and the watermark simultaneously.
So far I have managed to replace the header with the watermark, but I don't
want that, I want both to show.

thanks
Robin
 
J

Jay Freedman

I have a document that has a header in it. I want to add a watermark from my
building blocks into that header, without erasing or replacing the existing
header. This means I would show the header and the watermark simultaneously.
So far I have managed to replace the header with the watermark, but I don't
want that, I want both to show.

thanks
Robin

The "trick" is to declare a Range object, set it to the header's
range, *and collapse it to the start or end*. If you use the entire
range of the header, then the existing text will be removed.

This works:

Sub InsertWatermark()
Dim oRg As Range
Dim oTempl As Template, tempTempl As Template
Dim oBBE As BuildingBlock, tempBBE As BuildingBlock
Dim idx As Long
Dim BBEname As String

BBEname = "CONFIDENTIAL 1" ' name of watermark to insert

Templates.LoadBuildingBlocks

Set oRg = _
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
oRg.Collapse wdCollapseStart ' <== MUST DO THIS

For Each tempTempl In Templates
If InStr(LCase(tempTempl.Name), "building blocks.dotx") Then
Set oTempl = tempTempl
Exit For
End If
Next

If oTempl Is Nothing Then
MsgBox "Building Blocks.dotx not found"
Exit Sub
End If

For idx = 1 To oTempl.BuildingBlockEntries.Count
Set tempBBE = oTempl.BuildingBlockEntries(idx)
If InStr(LCase(tempBBE.Name), LCase(BBEname)) Then
Set oBBE = tempBBE
Exit For
End If
Next

If oBBE Is Nothing Then
MsgBox BBEname & " not found"
Exit Sub
End If

oBBE.Insert where:=oRg, RichText:=True
End Sub
 
R

Robin

Jay thanks for the good info. I'm still battling along here.

What you suggest adds a watermark to a header without removing the existing
header. If I try the opposite, for example I have a header that is a building
block (a collection of linked images defined as a header building block), and
I want to apply it in a section where there is a watermark (a single linked
image defined as a watermark building block) in that section's header - how
would I do that? Do I use the same code?

From my experience the watermark and everything else gets erased if I use
the WordBasic.RemoveHeader method. I can see that even though the watermark
is a watermark building block, it has a MSOShapeType of 11, as do the other
images in the header I am trying to apply. So does VBA even differentiate
between watermark and header building blocks or the content therein?

Thanks
Robin
 
J

Jay Freedman

Jay thanks for the good info. I'm still battling along here.

What you suggest adds a watermark to a header without removing the existing
header. If I try the opposite, for example I have a header that is a building
block (a collection of linked images defined as a header building block), and
I want to apply it in a section where there is a watermark (a single linked
image defined as a watermark building block) in that section's header - how
would I do that? Do I use the same code?

From my experience the watermark and everything else gets erased if I use
the WordBasic.RemoveHeader method. I can see that even though the watermark
is a watermark building block, it has a MSOShapeType of 11, as do the other
images in the header I am trying to apply. So does VBA even differentiate
between watermark and header building blocks or the content therein?

Thanks
Robin

Yes, you can use the same code. The macro I posted doesn't look at the
gallery the building block belongs to; it just uses the first building
block whose name matches the BBEname variable. As long as there's only
one building block with that name, the macro is fine.

There can actually be multiple building blocks with the same name, as
long as each one is has a different type/category pair. You can select
a specific building block to insert by type and category. The
principle of collapsing the range in which to insert it still applies.
For example, in my macro, instead of looping through the entire
BuildingBlockEntries collection looking for the name, you could use

Set oBBE = oTempl.BuildingBlockTypes(wdTypeWatermarks) _
.Categories("Confidential").BuildingBlocks(BBEname)

There are a lot more details in the VBA help topic "Working with
Building Blocks".

The WordBasic.RemoveHeader method is a very blunt tool. I'm not
surprised that it nukes everything in the header.

Once a graphic or other object is inserted into the document, VBA
doesn't "know" that it was a building block before it was inserted.
It's just a shape of a particular type (linked picture, text effect
[WordArt], etc.).
 

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