Macro needed to count cross references by type

marrick13 used Ask the Experts™
I am trying to build a macro that counts the number of cross references in a Word document by cross ref type:


as well as by the other types without wd enumeration (‘Figure’, ‘Equation’, and ‘Table’).

I came up with ‘Count_Crossrefs_by_Label’ (in the attached) for counting the ‘Figure’, ‘Equation’, and ‘Table’ types because of their standardized text, but am at a loss as to how to count the others.  The ‘GetCrossReferenceItems’ method seems to count all of the entities, not whether they have cross references or not.

It seems that if I can get the above wd types into a loop that also loop through the wdFieldRef types, through would give me a count by cross ref type. But I’m not having any success in doing this. Does someone know how to make this work?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Please try this macro. Is this the information you're looking for?

Option Explicit
Sub DocStat()
Dim bm, en, fi, fn, hd, tb, sh, ht, ni As Long
Dim msg, stat As String
Dim rng As Word.Range

Set rng = ActiveDocument.Content

bm = ActiveDocument.Bookmarks.count
en = ActiveDocument.Endnotes.count
fi = ActiveDocument.Fields.count
fn = ActiveDocument.Footnotes.count
hd = UBound(ActiveDocument.GetCrossReferenceItems(wdRefTypeHeading))
sh = ActiveDocument.InlineShapes.count
tb = ActiveDocument.Tables.count

msg = "This document contains:" & Chr(13) & bm & " Bookmarks" & Chr(13) & en & " Endnotes" & Chr(13) & fi & " Fields" _
        & Chr(13) & fn & " Footnotes" & Chr(13) & hd & " Headings" & Chr(13) & sh & " Inline Shapes" & Chr(13) & tb & " Tables"

stat = MsgBox(msg, vbInformation, "Document Statisics")

End Sub

Open in new window



Thanks, Paul, but that's not what I was after. When you create a cross reference in the Word interface, you are presented with a menu of 'Reference Types' (Numbered Item, Heading, Bookmark, Footnote, Endnote, Equation, Figure, and Table (plus any custom caption labels that may have been created).  I am looking for a way to count the cross references that are linked to those types, not the actual number of Numbered Items, Headings, Bookmarks, etc., that are in the document.

I thought the 'GetCrossReferenceItems(wdRefTypeHeading' might be a way to do this, but unless I know the actual text of the cross ref, such as "Figure", "Table", etc., I cannot figure out how to get an itemized count of the cross references. I do not need a count of the bookmarks or endnotes, but I do want the number of cross refs linked to or associated with bookmarks, endnotes, headings, and so forth.  Does this clarify what I'm looking for?
Give this one a try:

Sub ListXRefs()
Dim fld As Word.Field
Dim xr As Long
Dim rObjct, rTxt, rLLvl, rPrgrph, rPg, rInfo, rSum As String

For Each fld In ActiveDocument.Fields
    If fld.Type = wdFieldRef Then
        rObjct = "Object : " & fld.Result & Chr(13)
        rTxt = "Text : " & fld.Code.Text & Chr(13)
        rPg = "Page : " & fld.Code.Information(wdActiveEndAdjustedPageNumber) & Chr(13)
     xr = xr + 1
     Selection.EndKey Unit:=wdStory 'Results will print at the end of the document
        rInfo = rObjct & rTxt & rPg & Chr(13)
        Selection.TypeText (rInfo)
     End If
Next fld

    Select Case xr

        Case 0
            rSum = "There are no cross references in this document."
            Selection.TypeText (rSum)
        Case 1
            rSum = "There is 1 cross reference in this document."
            Selection.TypeText (rSum)
        Case Else
            rSum = "There are " & xr & " cross references in this document."
            Selection.TypeText (rSum)
    End Select

End Sub

Open in new window

It lists the type and location of the references and prints the results at the end of the document.


Thank you, but I already have code that produces the same results (except for the text, but I don't find that useful), and exports them to a separate document (see attached). I am looking for something that counts the number of cross references by reference type, such as numbered item, heading, footnote, endnote, etc, - whatever the cross reference is associated with as per the Reference Type in the Cross Reference dialog box. I can do this with cross refs tied to captions, such as "Figure" or "Table" because I can search the wdfieldref for that text, but cannot use this method for the others because their text is not standardized.

For example, if I have 35 headings in a document but cross references to 12 of them, I want to see a count of 12 heading-type cross references. Or if there are 48 footnotes and cross refs to 21 of them, I'd want to see a count of 21 footnote-type cross refs, and so on. I don't want a count of the headings or footnotes, nor the field text, just the number of cross references by their associated types (and page and line numbers, but I can add code for those values).

Is this even possible?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial