Solved

MS Word Cross References and VBA

Posted on 2012-12-29
7
1,621 Views
Last Modified: 2012-12-31
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
0
Comment
Question by:DrTribos
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
terencino earned 250 total points
ID: 38730351
Hi S, this code cycles through all the references in the document, and displays a number of properties that might give you what you want. It can be modified easily to display properties for a selection or a specific field:
Sub CheckXRefs()
Dim aField As Word.Field

For Each aField In ActiveDocument.Fields
    If aField.Type = wdFieldRef Then
           xref_text = "Text : " & aField.Code.Text & vbCrLf
           xref_result = "Result : " & aField.Result & vbCrLf
           xref_listlevel = "List level : " & aField.Code.Paragraphs(1).Range.ListFormat.ListLevelNumber & vbCrLf
           xref_paragraph = "Paragraph : " & ActiveDocument.Range(0, aField.Code.Paragraphs(1).Range.End).Paragraphs.Count & vbCrLf
           xref_page = "Page : " & aField.Code.Information(wdActiveEndAdjustedPageNumber) & vbCrLf
           MsgBox xref_text & xref_result & xref_listlevel & xref_paragraph & xref_page
        End If
Next aField
End Sub

Open in new window

Let me know how it goes
...Terry
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38730617
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

0
 
LVL 15

Author Comment

by:DrTribos
ID: 38731448
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
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Author Comment

by:DrTribos
ID: 38731476
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?
0
 
LVL 16

Assisted Solution

by:terencino
terencino earned 250 total points
ID: 38731609
Hi S, in place of
aField.Code
use
Split(aField.Code, " ")(2)
This parses the code text (delimited by a space) into an array, giving you the ref number without worrying about length.
Here's some more information on the Split function and why it works
...Terry
0
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 250 total points
ID: 38731619
I think that I would split the code text into parts and look for the bookmark:
Sub ReferencedText(fld As Field)
    Dim strRef() As String
    Dim para As Paragraph
    Dim strMessage As String
    Dim i As Integer
    
    Select Case fld.Type
        Case wdFieldRef, wdFieldPageRef
            strRef = Split(Selection.Fields(1).Code.Text, " ")
            For i = 0 To UBound(strRef)
                If Left(strRef(i), 4) = "_Ref" Then
                    Set para = ActiveDocument.Bookmarks(strRef(i)).Range.Paragraphs(1)
                    strMessage = para.Range.ListFormat.ListString & vbTab & para.Range.Text
                    MsgBox strMessage
                    Exit For
                End If
            Next i
    End Select
End Sub

Open in new window

0
 
LVL 15

Author Comment

by:DrTribos
ID: 38733889
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question