Selecting Bookmark objects

S

Steve C

I created a basic seating chart for our company employees by placing a layout
of our office in the background (header) of a Word document. Using the
Drawing toolbar, I used a rounded rectangle object (with no fill or line)
over each employee's location (not in the header, but the actual document).
This allowed me to create a bookmark for each rounded rectangle object for
each employee location. My goal was to create a small form that allowed a
user to select an employee name, then have my code find the corresponding
bookmark object and color it yellow to highlight where they sit.

I recorded a macro to get me started with the code; I chose Insert >
Bookmark, selected an existing bookmark name (jsmith) and clicked Go To. It
successfully found his rounded rectangle and selected it. After closing the
Bookmark dialog box (and still recording), I clicked the Fill Color button to
color it yellow. I then stopped recording and ended up with the following
code:

Sub SelectBookmark()

Selection.GoTo What:=wdGoToBookmark, Name:="jsmith"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 0) 'make Fill
Color = yellow
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
End Sub

When I de-select his oval and try running the macro again, I am getting the
following error:

Run-time error '424'
Object required

What is causing the code not to run successfully? Thanks for your help!
 
G

Greg Maxey

Steve,

You don't really need to select or GoTo the bookmark to color it. Try:

Sub FillBookmarShapeRange()
Dim oStr As String
Dim oBM As Word.Bookmarks
oStr = InputBox("Enter the bookmark name.")
Set oBM = ActiveDocument.Bookmarks
With oBM(oStr).Range.ShapeRange.Fill
.ForeColor.RGB = RGB(255, 255, 0) 'make Fill Color = Yellow
.Visible = msoTrue
.Solid
End With
End Sub
 
J

Jay Freedman

Hi Steve,

The macro recorder has many faults (see
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm), and
it's at its worst when dealing with drawing objects.

The error message you saw is what happens when the Selection (the area
included in the bookmark) doesn't contain any ShapeRange object. This
gets complicated because a floating shape is in the drawing layer,
while its "anchor" is in the text layer. If the bookmark contains the
anchor of a shape, then the corresponding ShapeRange is considered to
be in the bookmark, regardless of where the shape itself appears. But
that connection is fragile, and the recorded macro has no
error-handling.

Instead of trying to select the bookmark, your code can access the
shape directly through the bookmark object (assuming the bookmark
contains the shape). The macro should take precautions against a
nonexistent bookmark as well as a bookmark that doesn't contain a
shape. It could look like this:

Sub SelectBookmark()
Dim BkName As String

On Error GoTo ErrHdl

BkName = InputBox("Enter bookmark name")
If ActiveDocument.Bookmarks.Exists(BkName) Then
With ActiveDocument.Bookmarks(BkName).Range.ShapeRange
.Fill.ForeColor = RGB(255, 255, 0) ' yellow
.Fill.Visible = msoTrue
End With
Else
MsgBox "The bookmark " & BkName & " doesn't exist."
End If

Exit Sub

ErrHdl:
If Err.Number = 424 Then
MsgBox "The bookmark " & BkName & " doesn't contain a shape."
Else
MsgBox Err.Number & vbCr & Err.Description
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.
 

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

Similar Threads


Top