Link to home
Start Free TrialLog in
Avatar of AL_XResearch
AL_XResearchFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of AL_XResearch
AL_XResearch
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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.
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
SOLUTION
Avatar of DrTribos
DrTribos
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 😆
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
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.
A simple macro could make it a 1 step process...
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
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
You might have  use the dialogue for insertXref if you want us er to choose...
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

Did a quick Google... seems this dialog needs send keys to make it work 😢
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)
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
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Then clesn up all temp Bookmarks.

Do you mind if I ask what the application is?
Gah! Typos!!
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.
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 😀
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.
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
Sorry I am rusty on the specifics, wrote this 3 almost yrs ago!  Stuck on phone
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).
And code....

Not to mention the keyboard covering messages. ...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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 😤
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.
I'm sorry, this is the message that EE sent me.

User generated image
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
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,
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.
Yes - very weird. Glad we're all on the same page ☺

Cheers
BTW Thanks for the grade 😀