TOC name/value matchup

C

Csaba Gabor

If I want to insert a page reference, then the drop down shows me the
bookmarks I have named, it also shows me table of contents (TOC)
entries in the form of: _Toc141442210. This seems less than helpful.

So what I'd like to do is run a (vb)script that will match up those
ugly names to the text of the TOC entry. If myField is a vbscript/vba
field reference, then

myField.type: a numeric field type (88 => hyperlink, 37 => pageref, 9
=> TOC entry)
myField.code examples
Type 88: HYPERLINK \l "_Toc141442256"
Type 37: PAGEREF _Toc141442256 \h
Type 9: TC "Introduction " \f C \l "1"


How do I make the matchup from type 9 (TOCEntry) to the corresponding
pageref/hyperlink so that later I can insert my own page reference to
what the TOC Entry points at. In other words, I want to avoid creating
a separate bookmark, when there is already a TOC Entry pointing to a
place of interest.

Thanks,
Csaba Gabor from Vienna
PS. This is on Word 2003 under WinXP
 
J

Jezebel

You can use Doc.Bookmarks("_Toc141442256").Range to retrieve whatever the
TOC bookmark points to; then check whether that range includes a TC field.
 
C

Csaba Gabor

Jezebel said:
You can use Doc.Bookmarks("_Toc141442256").Range to retrieve whatever the
TOC bookmark points to; then check whether that range includes a TC field.

Jezebel,

thank you, Thank You, THANK YOU. I don't dabble in Word that much,
but I had thought to iterate through all the Bookmarks with a
For Each bm in doc.Bookmarks
but the _Toc guys did not show up. This is sort of in line with what I
get when I try to do an Insert \ Reference \ Cross-reference ...
sometimes those _Toc guys show up and sometimes they don't.

Thanks again,
Csaba

PHP code to return associative array of bookmarks:

function getBookmarks($doc) { // doc is a word document COM object
$abm = array(); // array of bookmarks
foreach ($doc->Fields as $ref) // TOC
if (preg_match("/_Toc\d+/",$ref->code,$aM))
$abm[$aM[0]] = $doc->Bookmarks($aM[0])->range->text;
foreach ($doc->Bookmarks as $bm) $abm[$bm->Name] = ""; // standard
return $abm; }
 
D

Dave Lett

Hi Csaba,

"sometimes those _Toc guys show up and sometimes they don't"

This is most likely related to the Hidden bookmarks check box that is on the
Bookmark dialog box. Also, if you don't have a TOC and TC entries, then Word
won't generate any hidden bookmarks for the TOC.

HTH,
Dave

Csaba Gabor said:
Jezebel said:
You can use Doc.Bookmarks("_Toc141442256").Range to retrieve whatever the
TOC bookmark points to; then check whether that range includes a TC
field.

Jezebel,

thank you, Thank You, THANK YOU. I don't dabble in Word that much,
but I had thought to iterate through all the Bookmarks with a
For Each bm in doc.Bookmarks
but the _Toc guys did not show up. This is sort of in line with what I
get when I try to do an Insert \ Reference \ Cross-reference ...
sometimes those _Toc guys show up and sometimes they don't.

Thanks again,
Csaba

PHP code to return associative array of bookmarks:

function getBookmarks($doc) { // doc is a word document COM object
$abm = array(); // array of bookmarks
foreach ($doc->Fields as $ref) // TOC
if (preg_match("/_Toc\d+/",$ref->code,$aM))
$abm[$aM[0]] = $doc->Bookmarks($aM[0])->range->text;
foreach ($doc->Bookmarks as $bm) $abm[$bm->Name] = ""; // standard
return $abm; }
 

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