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.