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 / April 2005

Tip: Looking for answers? Try searching our database.

VB Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MAB - 05 Apr 2005 16:29 GMT
When I try to run a Word macro named UpdateTOC, I receive a "Run time error
'5678': Word cannot find the requested bookmark." message.

When I click the "debug" button, I'm taken to this line within the code:

"Selection.GoTo What:=wdGoToBookmark, Name:="toc""

This is the rest of the code:

```````````````````
Sub UpdateTOC()

   Selection.HomeKey Unit:=wdStory
   Selection.GoTo What:=wdGoToBookmark, Name:="toc"
   With ActiveDocument.Bookmarks
       .DefaultSorting = wdSortByName
       .ShowHidden = False
   End With
   
   ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
   Selection.EndKey Unit:=wdLine, Extend:=wdExtend
   Selection.Delete Unit:=wdCharacter, Count:=1
   
   ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
   With ActiveDocument
       .TablesOfContents.Add Range:=Selection.Range,
RightAlignPageNumbers:= _
           True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
           LowerHeadingLevel:=4, IncludePageNumbers:=True, AddedStyles:=""
       .TablesOfContents(1).TabLeader = wdTabLeaderDots
   End With
   
   Selection.HomeKey Unit:=wdStory
   Selection.GoTo What:=wdGoToBookmark, Name:="tocend"
   With ActiveDocument.Bookmarks
       .DefaultSorting = wdSortByName
       .ShowHidden = False
   End With
   Selection.MoveLeft Unit:=wdCharacter, Count:=1
   Selection.TypeParagraph
   
End Sub
`````````````
Can anyone provide any insight in two what is going on or attempting to
happen?  I have no experience in VB coding, so laymans terms may be necessary
to explain it.  ;-)

Thanks.
Chuck - 05 Apr 2005 18:19 GMT
The short answer is that the bookmark "toc" is missing from the document, so
your macro can't find it and that causes an error.  You need to make sure the
toc bookmark is where it should be before running the macro.  Or else change
the line
 Selection.GoTo What:=wdGoToBookmark, Name:="toc"
to
 If activedocument.bookmarks.exists("toc") Then
   Selection.GoTo What:=wdGoToBookmark, Name:="toc"
 Else
   Msgbox "toc bookmark missing - cannot continue"
   Exit Sub 'this ends the sub without causing an error
 End If

That being said your code is way more complicated than it needs to be.  If
your table of contents is properly set up you don't need to redefine it every
time you're updating the values.  The following code will update your TOC
just fine:

   Dim i As Long
   
   With ActiveDocument
     If .TablesOfContents.Count > 0 Then
         For i = 1 To .TablesOfContents.Count
             .TablesOfContents(i).Update
         Next i
     End If
   End With

> When I try to run a Word macro named UpdateTOC, I receive a "Run time error
> '5678': Word cannot find the requested bookmark." message.
[quoted text clipped - 44 lines]
>
> Thanks.
Me - 05 Apr 2005 20:11 GMT
Thanks Chuck!

> The short answer is that the bookmark "toc" is missing from the document, so
> your macro can't find it and that causes an error.  You need to make sure the
[quoted text clipped - 72 lines]
> >
> > Thanks.
 
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.