Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Selecting Bookmark objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve C - 19 Feb 2007 01:40 GMT
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!
Signature

Steve C

Greg Maxey - 19 Feb 2007 02:16 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

>I created a basic seating chart for our company employees by placing a
>layout
[quoted text clipped - 38 lines]
>
> What is causing the code not to run successfully?  Thanks for your help!
Jay Freedman - 19 Feb 2007 02:23 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>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
[quoted text clipped - 32 lines]
>
>What is causing the code not to run successfully?  Thanks for your help!
Steve C - 19 Feb 2007 18:53 GMT
Greg & Jay:

Thank you both for getting me over the hump.  It is much appreciated!
Signature

Steve C

> Hi Steve,
>
[quoted text clipped - 85 lines]
> >
> >What is causing the code not to run successfully?  Thanks for your help!
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.