The code below will loop through all the inline images and add a caption:

Option ExplicitSub Example1()Dim intCount As Integer Dim i As Integer 'loop through inline shapes For i = 1 To InlineShapes.Count'check if the current shape is a picture If InlineShapes.Item(i).Type = _ wdInlineShapePicture Then 'select the picture InlineShapes.Item(i).Select 'assign a caption to it Selection.InsertCaption Label:="Figure", _ Title:="<<Your Code Here>>", _ Position:=wdCaptionPositionBelow End If Next iEnd Sub

The “Title” parameter, determines what to display after the “Label”. The position parameter determines where to display the caption. For more information about the InsertCaption parameters please see the article below:

The code below will loop through the images and add a caption to them:

Sub Example1()Dim intCount As Integer Dim i As Integer 'loop through shapes For i = 1 To Shapes.Count'check if the current shape is a picture If Shapes.Item(i).Type = _ msoPicture Then 'select the picture Shapes.Item(i).Select 'assign a caption to it Selection.InsertCaption Label:="Figure", _ Title:="<<Your Text Here>>", _ Position:=wdCaptionPositionBelow End If Next iEnd Sub

Result:

As you can see, after inserting a caption for the first image, the second image was moved to the right. I have discussed this issue in the article below:

As mentioned in that article in order to prevent such problems the position of the images will be changed from “relative to column” to “relative to page“. This can be done using the code below:

Sub Example2()Dim i As Integer 'position before making the changes Dim intPrevPosition As Integer For i = 1 To Shapes.Count'check if the image position is actually relative 'to the the column If Shapes.Item(i).RelativeHorizontalPosition = _ wdRelativeHorizontalPositionColumn Then 'get the current position intPrevPosition = Shapes.Item(i).Left Shapes.Item(i).RelativeHorizontalPosition = _ wdRelativeHorizontalPositionPage 'moves the shape back to where it was Shapes.Item(i).Left = intPrevPosition + _ PageSetup.LeftMargin End If Next iEnd Sub

So the complete code would look something like this:

Sub main()Call Example2Call Example1End Sub

Result:

You can download the files and code related to this article from the links below: