Cannot get a watermark to insert...

R

Robin

Hi,

I have "recorded" the steps I use to insert a watermark into section 2 of my
document. I get to section to by navigating to the last page and then moving
one page up. The watermark gets inserted as expected. I then delete the
watermark. Now I try to reinsert the watermark using the recorded steps
(macro). This is a standard Word watermark.

Sub Macro5()
Selection.EndKey Unit:=wdStory
Selection.MoveUp Unit:=wdScreen, Count:=1
ActiveDocument.AttachedTemplate.BuildingBlockEntries("SAMPLE 2").Insert _
Where:=Selection.Range, RichText:=True
End Sub

This fails and says "The requested member of the collection does not exist",
but SAMPLE 2 is there in the watermark quick list?

Help...

Robin
 
T

tgiallo

Robin - what version are you running? I won'der if the table reference
is missing
The "requested member of the collection does not exist" error occurs
when you try to access an object that does not exist. For example, the
following instruction may post an error if the active document does not
contain at least one table.

Sub SelectTable()
ActiveDocument.Tables(1).Select
End Sub

To avoid this error when accessing a member of a collection, ensure
that the member exists prior to accessing the collection member. If you
are accessing the member by index number, you can use the Count property
of the collection to determine if the member exists. The following
example selects the first table, if there is at least one table in the
active document.

Sub SelectFirstTable()
If ActiveDocument.Tables.Count > 0 Then
ActiveDocument.Tables(1).Select
Else
MsgBox "Document doesn't contain a table"
End If
End Sub

If you are accessing a collection member by name, you can loop on the
elements in a collection using a For Each...Next loop to determine if
the named member is part of the collection. For example, the following
deletes the AutoCorrect entry named "acheive" if it is part of the
AutoCorrectEntries collection. For more information, see Looping Through
a Collection.

Sub DeleteAutoTextEntry()
Dim aceEntry As AutoCorrectEntry
For Each aceEntry In AutoCorrect.Entries
If aceEntry.Name = "acheive" Then aceEntry.Delete
Next aceEntry
End Sub
 
J

Jay Freedman

The reason it doesn't work as advertised is that the macro recorder is
kind of stupid about certain things (see
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm for a
discussion of some of the other things).

In this case, the problem is that the "SAMPLE 2" building block entry
is stored in the Building Blocks.dotx template, not in the document's
attached template (which is probably Normal.dotm). You can see this by
looking in the Building Blocks Organizer.

To make things a little more interesting, Word doesn't load the
Building Blocks.dotx template into the Templates collection until the
user clicks the Insert > Quick Parts button or another button (such as
the Cover Page button or the Watermark button) that needs to show a
gallery of building blocks. The following macro uses the
LoadBuildingBlocks method to force the template to be loaded if it
isn't in the Templates collection yet.

Sub x()
Dim BBtemplate As Template, tmpTemplate As Template

' force loading of Building Blocks.dotx if it isn't present
Templates.LoadBuildingBlocks

' get Building Blocks as a template object
For Each tmpTemplate In Templates
If InStr(LCase(tmpTemplate.Name), "building blocks") Then
Set BBtemplate = tmpTemplate
Exit For
End If
Next

If Not BBtemplate Is Nothing Then
' now you can insert an entry from Building Blocks.dotx
Selection.EndKey Unit:=wdStory
Selection.MoveUp Unit:=wdScreen, Count:=1
BBtemplate.BuildingBlockEntries("SAMPLE 2").Insert _
Where:=Selection.Range, RichText:=True
End If
End Sub

The rest of your recorded code is suspicious, too. For one thing,
moving the Selection by Unit:=wdScreen isn't guaranteed to move it to
the page before the last page -- unless the last page contains less
than a screenful of text. And what constitutes a screenful depends on
the screen resolution of the particular computer and the zoom factor
of the current Word window, neither of which you've tried to control.

Another thing: unless you explicitly put the cursor (the Selection)
into a header pane, the "SAMPLE 2" building block is going to be
inserted as a floating Shape object in the main text, so it won't
appear on any other pages. This is a difference between the behavior
of building blocks inserted through the ribbon and those inserted by
code -- the Note in the VBA help topic "Working with Building Blocks"
says:

~~~
When you insert a building block by using the Ribbon, Word
automatically determines certain things about the building block, such
as where to insert it; however, when you insert a buildng block
through the object model, none of this built-in intelligence
automatically happens. For example, when you insert a header building
block by using the Ribbon, Word automatically determines to replace
the existing header. When inserting the same header building block by
using the object model, you need to explicitly specify where to place
the building block text.
~~~

Instead of moving the Selection, you would be better off determining
which of the document's sections should have the watermark, and then
inserting the building block into the correct header -- maybe
something like this:

Sub x2()
Dim BBtemplate As Template, tmpTemplate As Template
Dim numSecs As Long
Dim WMsec As Section, WMsecLast As Section

' force loading of Building Blocks.dotx if it isn't present
Templates.LoadBuildingBlocks

' get Building Blocks as a template object
For Each tmpTemplate In Templates
If InStr(LCase(tmpTemplate.Name), "building blocks") Then
Set BBtemplate = tmpTemplate
Exit For
End If
Next

If Not BBtemplate Is Nothing Then
' get next-to-last section
numSecs = ActiveDocument.Sections.Count
If numSecs > 1 Then
Set WMsec = ActiveDocument.Sections(numSecs - 1)
Else
Set WMsec = ActiveDocument.Sections.Last
End If
Set WMsecLast = ActiveDocument.Sections.Last

' turn off Same As Previous in both sections
WMsec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
WMsecLast.Headers(wdHeaderFooterPrimary).LinkToPrevious = _
False

' now you can insert an entry from Building Blocks.dotx
BBtemplate.BuildingBlockEntries("SAMPLE 2").Insert _
Where:=WMsec.Headers(wdHeaderFooterPrimary).Range, _
RichText:=True
End If
End Sub


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
R

Robin

Hi Jay and Tgiallo -

Thanks for the information, that has clarified some things.

What I am doing here is developing a template that has three sections. Each
section has a different header and footer. By replacing headers and footers
as well as watermarks in the various sections I can use the template for 64
different types of documents.

This is quite complicated because on the cover page (section 1) I have a
header that can include up to four four watermarks at once, these being
layered in a specific order, and besides that there is also a building block
header with graphics.

In section 2 the header has a field with the document name and a horizontal
line. This also has a watermark applied.

In section 3 I have a watermark, graphics and linked text boxes.

All graphics (images) are linked, including those in the watermarks - we do
not use WordArt. All watermarks, headers, footers etc. are building blocks
stored in the template.

The code to insert and remove these various elements uses the
BuildingBlockEntries Insert method. A problem is that the setions are
continuous, so even if I use script to find and navigate to section 2, it
inserts the header and watermarks for section 2 on page 1, because that's
where the section 2 starts (half way on the first page). Now I have forced
navigation to page 2 (via the page number) as we always only have a single
cover page - at least this way I can get the header placed in the correct
section.

What complicates even more is that watermarks can be stacked and headers
cannot. Then again, I have found that headers with WordArt can be stacked,
but not if they use graphics (linked files) - in the latter case the header
is simply replaced in totality.

At least the macros are working, though I hate having things working that I
can't be 100% certain of why they are working the way they do!

rgrds
Robin
 

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