Dynamic Field codes

C

Csaba Gabor

Earlier today, Jezebel was kind enough to help me out in determining
the text associated with each of the TOC Entry Ids (see
http://groups.google.com/group/microsoft.public.word.vba.general/browse_frm/thread/82b2e7becb6a27a0/)

Therefore, I can have a Cross Reference point to what a given TOC
(table of contents) entry points to. There is a problem, however, in
that if I update the TOC then the TOC Entry Ids are completely
recalculated and become invalid. That is to say, all Cross References
where I had given a TOC Entry Id would be invalid. No wonder they are
not normally shown under Insert \ Reference \ Cross-reference.

Still, what I want to do is reasonable, so I'm wondering if there isn't
a way I can somehow attach vba code to a Pageref (for example, also
affixing the text of the TOC Entry, which is far less likely to change)
so that when I do update field codes, it will be clever enough to match
up the desired text with the TOC entry whose text currently matches it?

Another way to say this: When I enter a TOC Entry field, Word knows
good and well where it is and what it is. I should be able to have my
PAGEREF (showing page number) field point to that TOC Entry field just
as well as the TOC itself points to the TOC Entry fields. How do I get
there from here?

Thanks for any tips,
Csaba Gabor from Vienna
 
D

Dave Lett

Hi,

You could bookmark the TOC Entry field and then insert a cross reference to
the bookmark.

HTH,
Dave
 
C

Csaba Gabor

Correct me if I am wrong. What you are suggesting is that each time I
insert a TOC entry (well, one that I want to cross reference), I also
insert a regular bookmark at the same time, since there's no easy way
to refer to the TOC entry directly. That will work, of course, but it
just bugs the heck out of me to have to put in duplicate entries - that
was what started this quest off in the first place.

Actually, if you can tell me how to insert a comment switch into a
field, that would satisfy me. Then I would just have a little routine
go through all the pagerefs and where it sees the comment, would look
up the TOC Entry with the text of the comment and tada I would have my
pageref.

Thanks,
Csaba
 
D

Dave Lett

Hi,

Yes, that's what I'm suggesting. However, you could create a routine to
insert your { TC } field and bookmark that range simultaneously, as in the
following:

Dim sResponse As String
Dim oRng As Range
Set oRng = Selection.Range

sResponse = InputBox(Prompt:="What TOC Entry do you want to include?",
Title:="Insert TOC Entry")

oRng.Text = "TC " & """" & sResponse & """"

oRng.Fields.Add _
Range:=oRng, _
Text:=oRng.Text, _
PreserveFormatting:=False

oRng.End = oRng.End + Len(sResponse) + 5

ActiveDocument.Bookmarks.Add _
Name:=Replace(sResponse, " ", "_"), _
Range:=oRng

This routine works well enough but not perfectly. For example, if you insert
two TC fields that have the same material, then the existing bookmark of
with the same name will be replaced and you will break reattach your cross
references to the range of the new material.

HTH,
Dave
 
P

Peter Jamieson

Actually, if you can tell me how to insert a comment switch into a
field, that would satisfy me.

Try e.g.

{ PAGEREF x { SET rem my comment text } }

Peter Jamieson
 
C

Csaba Gabor

Your comment was exceptionally helpful. I'm not sure that I've done
exactly what you intended, but my routine is now working as expected
without it or Word complaining. Here's what I've done.

Everyplace where I want to have a Cross-reference that points to a TOC
entry, I do an Insert \ Reference \ Cross-reference selecting Bookmark,
page Number, and choosing any old bookmark, it doesn't matter which.
If I now do Alt+F9 to see the reference, I see:
{PAGEREF bookmarkName \h}. I now put the cursor after bookmarkName and
type something like:
{TOCE All about squids} where All about squids is the TOC entry that I
want to refer to. At this point I have:
{PAGEREF bookmarkName {TOCE All about squids} \h}

Now the outer pair of curly braces indicate that the entire above line
is a field. However, since I typed the inner pair of curly braces,
that {TOCE ...} is not a field, and it seems that Word ignores it. In
other words, for all intents and purposes it's a comment (which is
exactly what I was asking for, thanks). I put TOCE as an indicator to
me that it's meant to be used to ferret out the corresponding TOC Entry
that has the remaining part as text.

Here's how that goes.
Part 1/2:
doc.Bookmarks.ShowHidden = true, remembering to retain the previous
setting.
Now I iterate through all the doc.Bookmarks, setting an associative
array:
trim(bookmark.range.text) => bookmark.Name

Part 2/2:
I iterate through each pageref in doc.Fields
If pageref.type!=37 then continue (37 is the PAGEREF type)
code="" & pageref.code
Now see if code has an embedded {TOCE entryText}
If not, continue (looping)
If so, see if the earlier associative array has an entryText entry.
If so, update the pageref's code with that bookmark name,
Then update the pageref.
If not, accumulate the error to be reported as a dangling reference.


Thanks again for the useful comments.
Csaba
 
P

Peter Jamieson

Even simpler! Nice.

Peter Jamieson

Csaba Gabor said:
Your comment was exceptionally helpful. I'm not sure that I've done
exactly what you intended, but my routine is now working as expected
without it or Word complaining. Here's what I've done.

Everyplace where I want to have a Cross-reference that points to a TOC
entry, I do an Insert \ Reference \ Cross-reference selecting Bookmark,
page Number, and choosing any old bookmark, it doesn't matter which.
If I now do Alt+F9 to see the reference, I see:
{PAGEREF bookmarkName \h}. I now put the cursor after bookmarkName and
type something like:
{TOCE All about squids} where All about squids is the TOC entry that I
want to refer to. At this point I have:
{PAGEREF bookmarkName {TOCE All about squids} \h}

Now the outer pair of curly braces indicate that the entire above line
is a field. However, since I typed the inner pair of curly braces,
that {TOCE ...} is not a field, and it seems that Word ignores it. In
other words, for all intents and purposes it's a comment (which is
exactly what I was asking for, thanks). I put TOCE as an indicator to
me that it's meant to be used to ferret out the corresponding TOC Entry
that has the remaining part as text.

Here's how that goes.
Part 1/2:
doc.Bookmarks.ShowHidden = true, remembering to retain the previous
setting.
Now I iterate through all the doc.Bookmarks, setting an associative
array:
trim(bookmark.range.text) => bookmark.Name

Part 2/2:
I iterate through each pageref in doc.Fields
If pageref.type!=37 then continue (37 is the PAGEREF type)
code="" & pageref.code
Now see if code has an embedded {TOCE entryText}
If not, continue (looping)
If so, see if the earlier associative array has an entryText entry.
If so, update the pageref's code with that bookmark name,
Then update the pageref.
If not, accumulate the error to be reported as a dangling reference.


Thanks again for the useful comments.
Csaba
 

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