Avatar of henk53
 asked on

Autonumbering of figures in Word


I would like to be able to autonumber figures (grouped autoshapes etc), just like LaTex does for example.

So, I added a caption (from the insert menu) to my figure and grouped the caption with it.

Unfortunately, (due to a Bug?) Word does not update the fields inside a grouped object. It seems such a basic thing, numbering figures, but Word does not seem to support this.

I wrote a macro to ungroup all shapes (it does not work if I ungroup one shape at a time), remember the original groups, update all fields, and regroup.

The macro almost works, but the problem is that once ungrouped, the parts often scatter all over the document. If I regroup, parts don't always regroup in the same combination, and rarely does the regrouped shape appears at the original location again.

This is my macro so far. I only learned VBA a day ago, so the code may not be that fancy. I hope someone can suggest an improvement, or perhaps knows of another way of numbering figures.

Sub updateFigures()

Dim shapeArray(1 To 50) As ShapeRange
Dim totalGroups As Integer
Dim bla As Collection
Dim sShape As Shapes
Dim qwe As ShapeRange
Dim myshape As ShapeRange
Dim nCounter As Integer

Dim origShapeArray(1 To 50) As Shape 'how to set a dynamic size ?
Dim origAnchorArray(1 To 50) As Range 'for now, 50 will do
Dim dupShape As Shape

totalGroups = 0

Set sShape = ActiveDocument.Shapes
    For nCounter = sShape.Count To 1 Step -1
        ' ungroup all grouped shapes
        If (sShape(nCounter).Type = Office.MsoShapeType.msoGroup) Then
           totalGroups = totalGroups + 1
           'selecting Anchor prevents scattering of parts a bit
           Set origAnchorArray(totalGroups) = sShape(nCounter).Anchor
           'remember original shape, doesn't help much...
           Set dupShape = sShape(nCounter).Duplicate
           dupShape.Visible = msoFalse
           Set origShapeArray(totalGroups) = dupShape
           Set qwe = sShape(nCounter).Ungroup
           Set shapeArray(totalGroups) = qwe
        End If
    Next nCounter
    ' update all fields if shapes have text
    For nCounter = 1 To totalGroups Step 1
        Set myshape = shapeArray(nCounter)
        Dim groupCnt As Integer
        For groupCnt = myshape.Count To 1 Step -1
            If myshape(groupCnt).TextFrame.HasText Then
            End If
        Next groupCnt
    Next nCounter
    Dim regroupedShape As Shape
    Dim origShape As Shape
    Dim origAnchor As Range
    ' regroup shapes back
    For nCounter = 1 To totalGroups Step 1
        Set myshape = shapeArray(nCounter)
        ' select the same Anchor as the original shape was linked to
        Set origAnchor = origAnchorArray(nCounter)
        Set regroupedShape = myshape.Regroup
        Set origShape = origShapeArray(nCounter)
        'Copy most of the original properties back
        Selection.ShapeRange.Fill.Transparency = origShape.Fill.Transparency
        Selection.ShapeRange.Line.Style = origShape.Line.Style
        Selection.ShapeRange.Line.Transparency = origShape.Line.Transparency
        Selection.ShapeRange.Line.BackColor.RGB = origShape.Line.BackColor.RGB
        Selection.ShapeRange.LockAspectRatio = origShape.LockAspectRatio
        Selection.ShapeRange.Height = origShape.Height
        Selection.ShapeRange.Width = origShape.Width
        Selection.ShapeRange.RelativeHorizontalPosition = origShape.RelativeHorizontalPosition
        Selection.ShapeRange.RelativeVerticalPosition = origShape.RelativeVerticalPosition
        Selection.ShapeRange.Left = origShape.Left
        Selection.ShapeRange.Top = origShape.Top
        Selection.ShapeRange.LockAnchor = origShape.LockAnchor
        Selection.ShapeRange.WrapFormat.AllowOverlap = origShape.WrapFormat.AllowOverlap
        Selection.ShapeRange.WrapFormat.Side = origShape.WrapFormat.Side
        Selection.ShapeRange.WrapFormat.DistanceTop = origShape.WrapFormat.DistanceTop
        Selection.ShapeRange.WrapFormat.DistanceBottom = origShape.WrapFormat.DistanceBottom
        Selection.ShapeRange.WrapFormat.DistanceLeft = origShape.WrapFormat.DistanceLeft
        Selection.ShapeRange.WrapFormat.DistanceRight = origShape.WrapFormat.DistanceRight
        Selection.ShapeRange.WrapFormat.Type = origShape.WrapFormat.Type
    Next nCounter
End Sub
Microsoft Office

Avatar of undefined
Last Comment

8/22/2022 - Mon

Word does have autonumbering for figures. They call it captioning and you can customize it to apply to different types of objects - excel charts, word pictures, etc. Unfortunately, it seems that autoshapes are one of the few types of figures that aren't on the list.

I found you could add captions to an autoshape (grouped or otherwise) if you create the autoshape in another word document (you don't need to save it), then Copy/Paste Special it as a picture instead of autoshape. However, it was still a bit glitchy (created a visible text box around the caption instead of doing a tidy caption like it did for other types of figures).

How many of your figures are autoshapes and how many are other formats? If there are only a few that are autoshapes, it might be worth playing with the captioning because it works well for the figure types on the preset list. However, if they are almost all autoshapes, it may be more trouble than it's worth.

If you want more details on the autonumbering through captions, let me know. Also, I only played with this for a few minutes so it may be possible to get it working more smoothly with autoshapes as well.


Hmmmm, well it's an interesting aproach. I tried what you said, but the kind of caption Word adds to a picture is exactly the same kind of caption you can (manually) add to an autoshape. Just try selecting an autoshape (grouped or not) and select 'caption' from the insert menu.

The visible text box is easy enough to get rid of, just right click the box and select 'Format text box...' from the context menu.

The problem though, is exactly the same with these pictures. As soon as you group the caption text box with the picture, Word does not update the field (SEQ) anymore.
Neither does manual updating work.

The obviously solution would be to simply not group a caption with an autoshape. However, when I do that, and add text to my document, Word often places the autoshape (of picture) on the bottom of one page and the caption box on the top of the next.

I think the problem is more fundamental than we might think. Word has so much legacy code in it. Autoshapes were added relatively late to the series, and their 'field processing code' is obviously not at all compatible with them.
Brian Mulder

This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in the next round.  I would appreciate any comments by the experts that would help me in making a recommendation.

It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:


EE Cleanup Volunteer
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

I am still interested in the answer, but to be honest I think the problem is unsolvable; that is Word 2000 and earlier are simply lacking this feature or have a bug in their implementation.

On the bright side, I do have been able to autonumber figures where the caption is grouped with the figure, but it only works in Word XP. The trick is to set tools->options->print->update fields. Then, do a print (to file, so not to waste paper) and the numbers will be updated. CTRL-A + F9 does not work.
Again, this only works in Word XP.

It works most conviently by putting the figure (after the caption has been grouped with it) in a drawing canvas, and use a "style" to control the distance between the figure and the surrounding text (top and bottom). Use draw->relative to canvas to center the figure.

Unfortunately there are still some glitches. Although field updating now works, the build-in TOF as found in Insert->Reference->Index and Tables->Table of Figures does not see {seq figure} fields in the caption, and therefore does not work. The Insert->Reference->Cross Reference->Numbered item/Figure + Page number does not work either. The cross-reference always outputs 0, no amount of field updating puts it to the real page number.


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/No Refund

Please leave any comments here within the next seven days.


John Pullin
EE Cleanup Volunteer

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.