InsertCrossReference On a Heading with No Text

Joined
Nov 20, 2014
Messages
3
Reaction score
0
In WORD 2010, Selection.InsertCrossReference fails for a Heading with no text (i.e., heading number but no text after it). It works with tables and figures with no text after the number. In the following example Heading 2.0 fails (4198), but Figure 2-2 does not fail. The Insert Cross-reference menu option is successful for both instances. However, if a macro is recorded to cross-reference to a heading and that code fails. Is there a solution?
Thanks,
John

1.0 Introduction
2.0
2.1 Approach
Figure 2-1: Diagram
Figure 2-2

strListTitle ="Heading"
'strListTitle = "Table"
'strListTitle = "Appendix_Table"
'strListTitle = "Figure"
'strListTitle = "Appendix_Figure"

RefKind = wdOnlyLabelAndNumber'
RefKind = wdNumberNoContextSelectedItem =2

Selection.InsertCrossReference ReferenceType:=strListTitle, _
ReferenceKind:= RefKind, _
ReferenceItem:=CStr(SelectedItem), _
InsertAsHyperlink:=True, _
IncludePosition:=False, _
SeparateNumbers:=False, _
SeparatorString:=" "
 
Joined
Nov 20, 2014
Messages
3
Reaction score
0
After further investigation, it appears that the WORD Insert Cross-Reference dialog box actually inserts a cross-reference to a Heading as if it is a cross-reference to a Numbered item, regardless of whether or not there is text following the heading number. In this instance, if the text is deleted (but not the number) or replaced the cross-reference remains valid.

Recorded macros of using the dialog box to insert cross-references to heading with and without text always show ReferenceType:="Heading". Without heading text this recorded macro fails. With heading text the hidden bookmark listed in the field code when using the recorded macro is not the same as the one listed in the field code when using the dialog box. In this instance (i.e., recorded macro with heading text present) the field code returns an error when updated after subsequently deleting or replacing the heading text, which did not occur as discussed previously.

Therefore, the code as recorded will never generate identical cross-reference field codes for Headings that are generated when using the dialog box.

John
 
Joined
Nov 20, 2014
Messages
3
Reaction score
0
My solution for Headings was:
  1. Read (GetCrossReferenceItems) both Headings and Numbered items
  2. Iterate through the Headings and find the corresponding Numbered item to match.
    1. Potential Bug: Need to see if there is a Numbered item format that would match a Heading format and give a false match.
  3. Allow the user to select from the Headings list.
  4. Translate the selected Heading index to a Numbered item index.
  5. Add the cross-reference to the selected Heading as a cross-reference to a Numbered item.
The hidden bookmark generated by my macro was identical to the ones created when using the WORD insert cross-reference for both Heading and Numbered item.

John
 

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