Word VBA, Adding Caption to Images

In this article I will explain how you can add captions to images using VBA. As previously explained there are 2 types of images:

  1. Inline
  2. Floating

The concept of adding caption to the two different types of images is pretty much the same.


Inline Images:

Inline images are images that appear on a separate line. Adding caption to these images is quite straight forward. Assume we have the following document:

Word, Inline Image Example

In the article below I’ve explained how you can loop through all the inline images in a document:

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

Option Explicit
Sub 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 i
End Sub

The line below selects the image object:

InlineShapes.Item(i).Select

The lines below add the caption to the picture:

Selection.InsertCaption Label:="Figure", _
    Title:="<<Your Code Here>>", _
    Position:=wdCaptionPositionBelow

The “Label” parameter determines what type of shape it is:

  • Figure
  • Equation
  • Table

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:

Result:

Word, Caption, Result VBA


Floating Images:

Floating images are images that appear among the text rather than on a separate line. As explained in the article below they belong to the Shapes collection:

Below you can see a document with floating images:

Word VBA, Floating images

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 i
End Sub

Result:

Word VBA, Floating Images, 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 i
End Sub

 So the complete code would look something like this:

Sub main()
Call Example2
Call Example1
End Sub

Result:

Result, Word VBA, Captions

 

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

See also:

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website  www.software-solutions-online.com

 

Leave a Reply

Your email address will not be published. Required fields are marked *