Word VBA, Loop Through Images

In this article I will explain how you can use VBA to iterate through the images in a word document. There are basically two types of images in a word document:

  • Inline
  • Floating

Inline Images:

Inline images appear on a separate line. There will not be any text to the left or right of the image:

Word, Inline Image Example
Inline images all belong to the InlineShapes collection. They can be reference by iterating through the InlineShapes collection.

Example:

The code below does the following:

  1. Iterates through all the InlineShapes in the word document.
  2. Checks if the InlineShape object is a picture.
  3. If the InlineShape is a picture it will modify its border

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 an picture
    If InlineShapes.Item(i).Type = _
        wdInlineShapePicture Then
        'check the border color to black
        InlineShapes.Item(i).Line.BackColor = vbBlack
        'create the border with to 20
        InlineShapes.Item(i).Line.Weight = 20
        'change the border style to single
        InlineShapes.Item(i).Line.Style = msoLineSingle
    End If
Next i
End Sub

The code below loops through the inline shapes in the documents:

For i = 1 To InlineShapes.Count
...
Next i

The If statement checks if the inline shape is a picture:

If InlineShapes.Item(i).Type = _
        wdInlineShapePicture Then
...
End If

The lines below modify the InlineShape object:

InlineShapes.Item(i).Line.BackColor = vbBlack
'create the border with to 20
InlineShapes.Item(i).Line.Weight = 20
'change the border style to single
InlineShapes.Item(i).Line.Style = msoLineSingle

Result:

Word, Inline Image Result


Floating Image:

Floating images are images that appear among the text:

Word, Floating Images
These images belong to the Shapes collection. They can be reference by iterating through the Shape collection.

Example:

The code below does the following:

  1. Iterates through all the shapes in the word document.
  2. Checks if the shape object is a picture.
  3. If the shape is a picture it will modify its border

Sub Example2()
Dim intCount As Integer
Dim i As Integer
'loop through inline shapes
For i = 1 To Shapes.Count
    'check if the current shape is an picture
    If Shapes.Item(i).Type = msoPicture Then
        'check the border color to black
        Shapes.Item(i).Line.BackColor = vbBlack
        'create the border with to 20
        Shapes.Item(i).Line.Weight = 20
        'change the border style to single
        Shapes.Item(i).Line.Style = msoLineSingle
    End If
Next i
End Sub

Result:

Word, Floating Image Result

You can download the files and codes 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

2 thoughts on “Word VBA, Loop Through Images”

Leave a Reply

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