removing bookmarks in a TOC

M

mtl

I'd like to be able to copy the TOC of several documents
and put them into a single document. At the moment if I do
that, I get a message'Error! Bookmark not defined'. Does
anyone know how I can get round this?

Many thanks,
mtl
 
P

Peter Hewett

Hi mtl

Funny, A client of mine had a document with a TOC in that would open ok on
some machines but not on others. The machines that did not work exhibited
the "Error! Bookmark not defined" problem. So I put this peice of code
together for them:

Public Sub CleanupTOCBookmarks()
Const cTOCBMPrefix As String = "_Toc"

Dim bmItem As Word.Bookmark
Dim fldItem As Word.Field

' Delete all TOC bookmarks
ActiveDocument.Bookmarks.ShowHidden = True
Debug.Print "Before: " & ActiveDocument.Bookmarks.Count
For Each bmItem In ActiveDocument.Bookmarks

' Hidden TOC bookmarks start with "_Toc"
If Left$(bmItem.Name, 4) = cTOCBMPrefix Then
bmItem.Delete
End If
Next

For Each fldItem In ActiveDocument.Fields

' We only want to update TOC fields
If fldItem.Type = wdFieldTOC Then

' Rebuild entire table - For some reason needs doing twice
fldItem.Update
fldItem.Update
End If
Next
Debug.Print "After: " & ActiveDocument.Bookmarks.Count
End Sub

Save the document after running this macro.

HTH + Cheers - Peter
 
P

Peter Hewett

Hi mtl

I'm not sure whether I read you post correctly. Are you trying to put just
the TOC of the other document in your document, but not the non-TOC text of
the other document?

Cheers - Peter
 
S

Suzanne S. Barnhill

Not really an issue. If the document you paste it into has headings at the
same levels or styles of the same names as those used by the TOC, then
pressing F9 and choosing to update the entire TOC will take care of the
problem. If it's an empty document, then naturally you will see that message
until the doc contains some headings.

If what you want is to paste just the text of another doc's TOC into a
different doc (to reflect the contents of the original doc), then you need
to lock (Ctrl+F11) or unlink (Ctrl+Shift+F9) the field either before
insertion or immediately afterward.
 

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