Need help getting entries from ToC

A

Andreas Baus

I have some difficulties trying to extract entries form a Table of
Contents in a Word document. What I want is, basically, to retrieve
for each entry in a ToC the name (i.e. the text) of the entry and the
point it's referring to (i.e. the name of the hidden bookmark). My
first idea was to use the Paragraphs collection on the range of the
ToC; but then I'm faced with the problem that, while it works quite
well for all Paragraphs except the very first and very last, for both
of the latter the Fields collection contains *all* the fields of the
entire ToC, leading to a number of special case branches, which rely
on a number of assumptions I made about the structure of the data in
the collections in question. Consequently, I'm not 100% sure my
solution is going to be water-proof in all cases; all in all I'm
wondering (or rather hoping) that there is a simpler, more elegant and
most of all more reliable method for accomplishing what I'm trying to
do. If anyone here has any idea's, I'd be eager to hear them. Thanks.
 
D

Dave Lett

Hi Andreas,

If you want to get the titles of all headings in a document, then you can
use something like the following:

Dim myHeadings
Dim iCount As Integer
myHeadings = _
ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)
For iCount = 1 To UBound(myHeadings)
Debug.Print myHeadings(iCount)
Next iCount

Each heading doesn't really have a "hidden bookmark". Word has a reference
location for each heading that you can view as a hidden bookmark when you
insert, for example, a cross reference. If you want to insert a cross
reference or something like that, then have a look at the VBA help topic
"GetCrossReferenceItems Method Example"

HTH
 

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