?
Solved

MS Word Cross References and VBA

Posted on 2012-12-29
7
Medium Priority
?
1,817 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
terencino earned 1000 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
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

 
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 1000 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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
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:
Suggested Courses

765 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