Share via

Get list of MSWord Figure or Table captions in vba

Anonymous
2011-09-29T09:12:46+00:00

I posted this in the Word forum yesterday without any luck but found the Customisation forum today which is probably a better place to try.  Apologies for the double post...

When I insert a cross-reference to a Figure or Table I almost always want to reference the first Figure that occurs below my current line.  I want to create a macro which will insert a cross-reference to that nearest Figure/Table caption.  I know how to insert a cross-ref via VBA and I know how to determine what line number I am currently on.  Can anyone help me generate a list of Figure captions and their line numbers (so that I can compare to my current line number).

Thanks a lot!

Andrew

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2011-10-01T05:44:24+00:00

Unfortunately it is impossible for me to figure out which fields you are looking for. You have to know that I'm a.) not a Word expert, and b.) just have a german Word. A simple sample document would be very helpful.

But maybe you can figure out the rest by yourself, the key to a word document is the range object. Every range object has a start and a end property which signs the position in the document.

A line number doesn't matter.

As far as I can see, the GetCrossReferenceItems function returns an array of strings, I'm not sure if this really helps.

I would use the Fields collection, because any field has a range object too. So I think you can you use a FOR EACH loop through all fields, check the type of each field to get the fields which are you looking for and check the positions as I've shown you in my last post.

Andreas.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-10-03T15:18:47+00:00

    Thanks a lot - that was the tip I needed.  I found I can access the .Start property from the .Result property of the Fields collection.  Here is the full routine:

    Sub CreateCrossRefToNextFigure()

        Dim fld As Field

        Dim strRefID As String

        For Each fld In ActiveDocument.Fields

        '   Check if the field is a figure

            If Left(fld.Code, 11) = " SEQ Figure" Then

            '   Check if this is below our current position

                If fld.Result.Start > Selection.Start Then

                '   This is the field we want to cross-reference

                    strRefID = fld.Result.Text

                    Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _

                        wdOnlyLabelAndNumber, ReferenceItem:=strRefID, InsertAsHyperlink:=True, _

                        IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

                    Exit For

                End If

            End If

        Next

    End Sub

    Alles Gute!

    Andrew

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2011-09-30T09:22:43+00:00

    I have managed to find two ways I can access the captions - but I can't manage to get the line number that the caption is on.

    Option 1:

    Dim varTables

    varTables = ActiveDocument.GetCrossReferenceItems(wdCaptionTable)

    Option 2:

    ActiveDocument.Fields

    Any help much appreciated - it feels like I am almost there.

    Thanks,

    Andrew

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2011-09-29T14:36:05+00:00

    Hi Andreas,

    Thanks for your reply!  What I'm actually looking for is the Table/Figure captions rather than the Tables themselves, i.e. caption as can be inserted from the tab, References/Insert Caption (Word2007).  This button displays a dialog which lets me insert a caption for either a Table, Figure or Equation.  In theory I might insert a picture of a data table and give it a Table caption.

    I can cross-reference the caption using:

        Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _

            wdOnlyLabelAndNumber, ReferenceItem:="2", InsertAsHyperlink:=True, _

            IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

    ... but I need to know the index ("ReferenceItem") of the Figure closest to my current position.

    Hope that's clearer.

    Thanks again,

    Andrew

    Was this answer helpful?

    0 comments No comments
  4. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2011-09-29T10:23:27+00:00

    This is easy for tables, just compare the start position of the table with the start position of the selection.

    Sub Test()

      Dim T As Table

      For Each T In ActiveDocument.Tables

        If T.Range.Start > Selection.Start Then

          'This is the table that you are looking for

          T.Select

          Exit For

        End If

      Next

    End Sub

    But I don't know what you mean with "Figure". Can you please upload an example file anywhere, i.E. www.dropbox.com and post the download link here?

    Andreas.

    Was this answer helpful?

    0 comments No comments