Solved

MS Word Cross References and VBA

Posted on 2012-12-29
7
1,543 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
Comment Utility
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
Comment Utility
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 14

Author Comment

by:DrTribos
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Author Comment

by:DrTribos
Comment Utility
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
Comment Utility
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
Comment Utility
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 14

Author Comment

by:DrTribos
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now