Word 2010: Inserting a heading number and text as a cross reference

This may seem a silly question but when you insert a cross-reference to a heading that has been formatted to show a number how can you set Word 2010 to insert both the heading number AND the heading text ?

So if I have a document with headings such as:

1 Contents
2 Overview
 2.1 History
 2.2 Development
3 Code

How can I insert a cross-reference that will insert text such as '2.1 History'

I used to do this no problem in Word 2003 and 2007 but for some reason I don't seem to be able to find how this is done in Word 2010

A video showing how (or a link to a YouTube video) would be fantastic.

Many thanks.
LVL 3
AL_XResearchAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AL_XResearchAuthor Commented:
To clarify:

If you open the 'Cross-reference' dialog you can select 'Numbered item' for the 'Reference type' and then  for the 'Insert reference to' select first 'Paragraph Number' and then 'Paragraph text' but that takes two steps. In older versions of Word I was able to insert both of these items in a single action.
DrTribosCommented:
I think you might be using a numbered list instead of a  heading style... I think  (but will check ) that if you use heading styles you'll see the result your expecting
DrTribosCommented:
Nope. You're right. They changed the way it works.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

DrTribosCommented:
I speculate that they changed this due to problems with numbering in word. Go with the 2 step process... you have more control that way anyhow 😆
AL_XResearchAuthor Commented:
Thanks DrTribos.

It is a heading style with a number (however when I try and modify the style it shows the numbering 1.1.1 in the preview but in the 'Numbering' dialog says no numbers are used !?!?!)

Is there no way you (or another export) known to automate the insertion of both of these parts pointing so the same location ?

It is actually a 3 step process: insert paragraph number, type a space, insert paragraph text.

When you are inserting dozens and dozens of references this 3 step process is very annoying
DrTribosCommented:
Hi. Sorry. I'm quite sure the feature has changed.  I remember it working the way you describe, but when I  tested in Wd2013 I faced the same issue.
DrTribosCommented:
A simple macro could make it a 1 step process...
AL_XResearchAuthor Commented:
I would agree but how can you make a macro that would show that dialog (for the user's selection) and then insert the 3 items at the current insertion point ?

I am very familiar with VBA (particularly in Excel) but i don't know how to show this dialog in Word
DrTribosCommented:
Hmmm

I am using phone.... once you have the first xRef you might be able to edit the field switches to display what you want.  You do that by editing the Field.Code.Text

Not sure if you know that word uses a ref field and a corresp bookmark to create xRefs
DrTribosCommented:
You might have  use the dialogue for insertXref if you want us er to choose...
AL_XResearchAuthor Commented:
I have got the following macro so far but I am unable to complete the sections commented with '???'

Basically as I say I can't see how to get the selected numbered item from the dialog (in my attempt below it is '9' - whatever that means) ...

Sub AddHeadingCrossRefNumberAndText()

    Dim dlgCrossRef As Word.Dialog
    
    ' Init. var.
    Set dlgCrossRef = Word.Dialogs(wdDialogInsertCrossReference)
    
    ' Show the dialog to allow the user to select their reference
    With dlgCrossRef
        ' ??? Preselect the reference type ???
        .Show
        ' ??? Get the selected reference item ???
    End With

    With Selection
    
        ' Insert the heading number
        .InsertCrossReference ReferenceType:="Numbered item", _
           ReferenceKind:=wdNumberRelativeContext, ReferenceItem:="9", _
           InsertAsHyperlink:=True, IncludePosition:=False, SeparateNumbers:=False, _
           SeparatorString:=" "
           
        ' Insert seperator
        .TypeText Text:=" "
        
        ' Insert the heading text
        .InsertCrossReference ReferenceType:="Numbered item", _
        ReferenceKind:=wdContentText, ReferenceItem:="9", InsertAsHyperlink:=True _
        , IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
    End With
        
End Sub

Open in new window

DrTribosCommented:
DrTribosCommented:
Did a quick Google... seems this dialog needs send keys to make it work 😢
AL_XResearchAuthor Commented:
I don't want to automate the dialog (which is what you would use sendkeys for) - it want to get the user's selection from the dialog so i can pass it to my code which will do the actual insertions (see my extract).

Besides I always try to avoid SendKeys if I can - bad coding practice and suggests that I have not gone about the task in the proper way (e.g. found how to use the dialog properly)
DrTribosCommented:
Same page there.  I do something similar but I don't have my code or pc to look at and not quite sure of the detail.  Basically I createf my owm dialogue via userform
AL_XResearchAuthor Commented:
If you create your own dialog how do you list the available headings ? As I say I am not as familiar with Word VBA as I am with Excel.
DrTribosCommented:
Ok... use a list box and loop thru (search thru) doc to find  each heading then add temp bookmark to heading, then add heading to listbox visible column and bookmark name to hidden column

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DrTribosCommented:
User select from list and presd an ok button. Get list index. Goto corresp temp Bookmark and use the Bookmark range to add xRef to your doc
DrTribosCommented:
Then clesn up all temp Bookmarks.

Do you mind if I ask what the application is?
DrTribosCommented:
Gah! Typos!!
AL_XResearchAuthor Commented:
Thanks. I will look into it.

Why do you need to create  bookmark. Can't you cycle through all the headings in some kind of document object collection ?

There is no 'application'. I am writing a technical document (at present over 110 pages which will probably bloom to around 300) and I need to cross-reference almost every section. Therefore manually doing the three steps we stated out with would be very time consuming,

Would be ideal to have some sample code to speed up writing the macro for the function that MS should have retained.
DrTribosCommented:
The temp BM is so you can confidently find the right heading - activedocument.bookmarks(tmp0009).rangeparagraphs(1).range.text is fast and precise.

Listbox has 2 columns 1st for display text. 2nd for hidden bookmark. For sure there are other approaches - this happens to be one that seems to work 😀
AL_XResearchAuthor Commented:
I am afraid that is what I don't understand:

If you have using the bookmark to get the heading text and then insert a reference to that text - how does word create the cross reference 'link' since the same text may occur elsewhere in the doc. It is not like Excel where a specific range can only occur once in a document.

If you use
ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading)

Open in new window

It will return a string array and if the user selects a specific string are you saying you add the relevant array index as the value of 'Reference Item' to create a link . cross ref to the heading ?

That seems a very 'weak connection' since as I say the same text may appear elsewhere and when you click the hyperlink Word will have to search through the doc to find that exact text and determine the target.
DrTribosCommented:
Yes, that is why I loop thru the doc first to find the headings and add the temp bookmarks as each is found. It is the Bookmark that is unique
DrTribosCommented:
Sorry I am rusty on the specifics, wrote this 3 almost yrs ago!  Stuck on phone
AL_XResearchAuthor Commented:
Ah I see. Phone responses can be limiting ! EE should really update their app as I find it very hard to use (not to mention that they need to allow the font to be adjusted so we can read the articles).
DrTribosCommented:
And code....

Not to mention the keyboard covering messages. ...
AL_XResearchAuthor Commented:
Well I have created a form and a function (based on your advise and a little guesstimate) and what do you know it works.

I have included it below for the reference of others.

In addition to the below:
Create a userform called 'frmCrossRefHeadings'
Add a listbox 'lstHeadings' to the form with only 1 column. Make it almost as high as the screen to ensure you can see all headings.
Add two command buttons to the form ('Cancel' and 'Insert')
Add code to both buttons to set the form's tag to either 'CANCEL' or 'INSERT' and then hide the form

NOTE: Heading text is automatically indented

Sub AddHeadingCrossRefNumberAndText()

    Dim strHeadings() As String
    
    Dim intMaxIdx As Integer
    Dim intCurIdx As Integer
    Dim intSelectedHeading As Integer
    
    ' Init. var.
    strHeadings = ActiveDocument.GetCrossReferenceItems(wdRefTypeHeadings)
    intMaxIdx = UBound(strHeadings)
    
    ' Show the dialog to allow the user to select their reference
    With frmCrossRefHeadings
    
        ' Load headings
        With .lstHeadings
        
            For intCurIdx = 1 To intMaxIdx
                .AddItem strHeadings(intCurIdx)
            Next intCurIdx
        
        End With
    
        ' Show the form
        .Show
        
        ' Process the result
        intSelectedHeading = .lstHeadings.ListIndex + 1
        
        If .Tag = "INSERT" Then
        
            With Selection
            
                ' Insert the heading number
                .InsertCrossReference ReferenceType:="Numbered item", _
                   ReferenceKind:=wdNumberRelativeContext, ReferenceItem:=intSelectedHeading, _
                   InsertAsHyperlink:=True, IncludePosition:=False, SeparateNumbers:=False, _
                   SeparatorString:=" "
                   
                ' Insert seperator
                .TypeText Text:=" "
                
                ' Insert the heading text
                .InsertCrossReference ReferenceType:="Numbered item", _
                ReferenceKind:=wdContentText, ReferenceItem:=intSelectedHeading, InsertAsHyperlink:=True _
                , IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
            End With
            
        End If
    
    End With
    
    ' Clear the form from memory
    Unload frmCrossRefHeadings
        
End Sub

Open in new window

DrTribosCommented:
Nice.... I might have added the bookmarks for a different purpose. Your solution looks good, thanks for sharing.

Ps Sorry I couldn't get to a PC after my initial comments
AL_XResearchAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 250 points for DrTribos's comment http:#a41150227
Assisted answer: 250 points for DrTribos's comment http:#a41147188
Assisted answer: 0 points for AL_XResearch's comment http:#a41150944

for the following reason:

DrTribos confirmed that Word 2010 no longer supported the feature I required and provided my a starting point to roll-my-own but my solution / developed code restores the needed functionality and I needed to highlight that.
DrTribosCommented:
41150944:
Well I have created a form and a function (based on your advise and a little guesstimate) and what do you know it works.

In essence OP asked a question to which the answer was NO.  OP then based their solution on my suggestions & advice... just saying 😤
AL_XResearchAuthor Commented:
DrTribos: Why have you objected ? I have given you the points and only included my code in the 'Accepted solutions' for the reference of others.
DrTribosCommented:
I'm sorry, this is the message that EE sent me.

EE Message
I've never seen  (nor expected to see) this message when my answer is accepted.

I don't recall seeing the point allocation. Was using phone.

Thanks for resolving and seeking clarification, hope this make the objection easier to understand.

Kind regards
DrTribosCommented:
Hi thermoduric -

"was clearly just an unfortunate misunderstanding" .... An unfortunate misunderstanding - absolutely; however I respectfully disagree with using the word "just"... IMHO this situation is better described as a bug that led to a misunderstanding.  

I say this because when my answers are:
accepted - I typically get a message from EE saying "Congratulations..."
assisted - I also get some form of congratulations
not accepted - I typically get  a message from EE saying "Thanks but..."

So the message I received was very much unexpected, I suspect this different message was triggered by the fact that the OP accepted his own comment as an assisted solution, and possibly because there were no other experts involved?

Perhaps this can be given some further consideration by EE and a more appropriate message sent to experts when this situation arises in the future?

Cheers,
AL_XResearchAuthor Commented:
DrTribos: Couldn't agree with your last comment more. There was no objection from either side here. I quite clearly closed the question and awarded you the points.

The only source of 'the bug' could be accepting one of my own postings as part of the answer. I have however done this on a number of questions and never had this before - either a new bug or triggered by a unique combination of events.

Glad you got the points.
DrTribosCommented:
Yes - very weird. Glad we're all on the same page ☺

Cheers
DrTribosCommented:
BTW Thanks for the grade 😀
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.