Bug with cross-references in VBA?

W

wiwidom

Hallo,

I'd like to insert a cross-reference (reference to: only label and
number) to a self defined caption (Insert->reference->caption) using a
macro. Normally this wouldn't be a problem:

Selection.InsertCaption Label:="Test", TitleAutoText:="InsertCaption1",
_
Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0

Selection.InsertCrossReference ReferenceType:="Test", ReferenceKind:= _
wdOnlyLabelAndNumber, ReferenceItem:="1", InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

But I need a caption with the option "Exclude label from caption".
Now - if I'm using this type of caption (ExcludeLabel:=1) and insert
a cross reference manually, everything is still fine, but if I'm
executing the same command as a macro Word 2003 is inserting nothing
(don't know about other Word versions) ! I even recorded the successful
manual insertion with the recorder (which is creating the same code
lines as above). When I'm executing the macro nothing is inserted.

Does anybody have any idea?

thanks
Dom
 
J

Jean-Guy Marcil

wiwidom was telling us:
wiwidom nous racontait que :
Hallo,

I'd like to insert a cross-reference (reference to: only label and
number) to a self defined caption (Insert->reference->caption) using a
macro. Normally this wouldn't be a problem:

Selection.InsertCaption Label:="Test",
TitleAutoText:="InsertCaption1", _
Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0

Selection.InsertCrossReference ReferenceType:="Test", ReferenceKind:=
_ wdOnlyLabelAndNumber, ReferenceItem:="1", InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

But I need a caption with the option "Exclude label from caption".
Now - if I'm using this type of caption (ExcludeLabel:=1) and insert
a cross reference manually, everything is still fine, but if I'm
executing the same command as a macro Word 2003 is inserting nothing
(don't know about other Word versions) ! I even recorded the
successful manual insertion with the recorder (which is creating the
same code lines as above). When I'm executing the macro nothing is
inserted.

This is because, as you found out, there is often a difference (bug?) in the
way Word works when we use its dialogs and when we try to do the same with
VBA.

In this case, when you insert your caption without a label, and then
manually (with the dialog from Insert > Reference > Cross...) Word manages
to create the appropriate hidden bookmark so that the cross reference works
and displays only the number (since the label is missing). When you do the
same by code, the cross-reference is inserted, but contains nothing as the
hidden bookmark that the .InserCrossReference method created is a single
insertion point just before the number in the caption (instead on
encompassing the number as it does when you use the dialog).

I am afraid I do not have a solution. I thought that you could invoke the
Insert Cross-reference dialog with VBA, but that also presents a whole new
sets of problems. (See
http://support.microsoft.com/default.aspx?scid=kb;en-us;q209668).
Another crazy idea would be to check the bookmark collection before
inserting the cross-reference, then again after. The new bookmark will be
the one the method just added in order to reference the caption. Then,
extend that bookmark to the right so as to include the number that follows.
Finally, update the cross-reference field so that it will display the
number.
But what if you add a second cross-reference to that same caption later on,
elsewhere in the document?

Why don't you want the label displayed?

If it is absolutely necessary to remove the labels, you could insert them
anyway, so that the cross reference code will work, but later, when the
document is finished, remove all the labels from the document using a
Find/Replace.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
W

wiwidom

I'd need to programm a semi-automatic bibliography. My idea was to use
the caption as the actual bibliography like: "1 Title, author" and the
references in the text with just "(1)", that's why I don't want the
labels. If you have any other idea doing that just let me know :)
I could of course insert bookmarks myself, but it would be much easier
and less error prone if WORD would do that internally. I like the idea
of comparing the bookmark list. i will try that when I'm back from
vacation next week.

thanks
Dom
 

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