Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS Word Cross References and VBA

Posted on 2012-12-29
7
Medium Priority
?
1,886 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
Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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.

618 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