Solved

MS Word Cross References and VBA

Posted on 2012-12-29
7
1,682 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 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
Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

726 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