VB Help

M

MAB

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.
 
C

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
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
 
M

Me

Thanks Chuck!

Chuck said:
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
 

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