Automatically insert captions to every chart or Image in a Word document in seconds using some VBA - Detailed guide
You probably already know how to insert a caption to a chart or an image in a Word document. From the References tab, you click on Insert Caption and you can select the label that you would like to use. If you only have a couple of charts, it is not a problem, and with a few clicks you are all set.
But, what if you are working with a document with dozens of charts or images. The process soon becomes boring, tedious and time-consuming. Following this guide, and copying and pasting the VBA code you will find in the description, you can add captions to dozens of charts or images in a matter of seconds!
Go to the developer tab. If the developer tab is not visible on the ribbon, go to File, then Options, Customize Ribbon, and in the Main Tabs menu make sure that Developer is checked. Right click on Modules, and select Insert, and then New Module. Copy and paste the following VBA code, and click the green button in the taskbar, or click on Run, and then Run Sub/Userform. In just a few seconds, with minimal work, you inserted a caption to every chart in your Word document.
If you want to add a caption to every image in a document, simply replace wdInlineShapeChart with wdInlineShapePicture. If you want the caption below the image or the chart, simply change wdCaptionPositionAbove to wdCaptionPositionBelow. This also applies to charts as well.
Optionally, you can also set the font, font color and font size of the caption by changing the captions style. Here I set each caption to Times New Roman, size 10, black and bold. This also applies to charts.
What this code does is loop through every inline shape in the document, determines if it’s a chart, or an image if you want to add captions to images, then adds a caption to each chart or image in the document using the InsertCaption method, setting the Label, its Title and its position.
You will find all the VBA code in the description.
If you have any questions let me know in the comments.
VBA Code to insert a caption to every chart in a Word document
Sub AddCaptions()
Dim intCount As Integer
Dim i As Integer
'loop through inline shapes
For i = 1 To ActiveDocument.InlineShapes.Count
'check if the current shape is a chart
If ActiveDocument.InlineShapes.Item(i).Type = _
wdInlineShapeChart Then
''assign a caption to the chart
ActiveDocument.InlineShapes.Item(i).Select
Selection.InsertCaption Label:="Figure", _
Title:=". ", _
Position:=wdCaptionPositionAbove
'the above line sets the position of the caption, if you want the caption below the chart change it to wdCaptionPositionAbove
End If
Next i
'Optional, remove if you don't want to change the style of the caption
With ActiveDocument.Styles("Caption").Font
.Name = "Times New Roman"
.Size = 10
.Italic = False
.Bold = True
.ColorIndex = wdBlack
End With
End Sub
VBA Code to insert a caption to every image in a Word document
Sub AddCaptions()
Dim intCount As Integer
Dim i As Integer
'loop through inline shapes
For i = 1 To ActiveDocument.InlineShapes.Count
'check if the current shape is an image
If ActiveDocument.InlineShapes.Item(i).Type = _
wdInlineShapePicture Then
'insert a caption above the image
ActiveDocument.InlineShapes.Item(i).Select
Selection.InsertCaption Label:="Figure", _
Title:=". ", _
Position:=wdCaptionPositionAbove
'the above line sets the position of the caption, change the position to wdCaptionPositionAbove if you want the caption below
End If
Next i
With ActiveDocument.Styles("Caption").Font
.Name = "Times New Roman"
.Size = 10
.Italic = False
.Bold = True
.ColorIndex = wdBlack
End With
End Sub