Link to home
Start Free TrialLog in
Avatar of DrTribos
DrTribosFlag for Australia

asked on

MS Word Cross References and VBA

Hi All

Seasons Greetings...

I have a cross reference in MS Word:  { REF _Ref344571714 \r \h }  which links to a numbered item.

What I would like to do is display the corresponding paragraph number & other reference informatoin in a VBA MsgBox...

would love some help on this one, it has been driving me nuts :-(

Cheers, S
ASKER CERTIFIED SOLUTION
Avatar of terencino
terencino
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
Avatar of GrahamSkan
Hi Steve
The Cross Reference system uses hidden bookmarks, so you need some code like this:
Sub ReferencedText()
    Dim strRef As String
    Dim para As Paragraph
    Dim strMessage As String
    
    strRef = "_Ref344626289"
    Set para = ActiveDocument.Bookmarks("_Ref344571714").Range.Paragraphs(1)
    strMessage = para.Range.ListFormat.ListString & vbTab & para.Range.Text
    MsgBox strMessage
End Sub

Open in new window

Avatar of DrTribos

ASKER

Hi Terry & Graham

Thank you both for your help.  I think I'm close to getting what I need.

I'm trying to get the para text to display based on a clicked field... so was adapting Graham's suggestion and trying this:
Set para = ActiveDocument.Bookmarks(aField.Code).Range.Paragraphs(1)

Open in new window

This fails due to too much information...
? aField.Code
 REF _Ref344627521 \n \h \t

Open in new window

Is there a way to return the reference without switches?  Otherwise I'll have to brush upon my text trimming...

Cheers, S
Would this be robust?
? mid(aField.Code, 6, 13)
_Ref344627521

Open in new window


I'm sure 6 will be fine... but can I always expect a 13 character ref?
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
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
Thank you both.  

Terry you have answered the question and provided lots of good info - thanks.

Graham - thank you for your additional comments.  I am using nested fields so your approach will probably save me the most time.