Word VBA Insert Images

In this article I will explain how you can use VBA for word to insert images to a word document using VBA.


Insert Single Image:

The code below will insert the image “SP_A0155.jpg” from the location “D:StuffBusinessTemp”:

Sub Example1()
Selection.InlineShapes.AddPicture FileName:= _
"D:StuffBusinessTempSP_A0155.jpg", LinkToFile:=False, _
SaveWithDocument:=True
End Sub

Result:

Word VBA, Insert Result


Insert Image at Specific Location:

In order to insert the image at a specific location, you will need to move the cursor to that location. There are different methods for moving the cursor around in word using VBA:

In this example I will assume we have created a bookmark at the location we want to insert the image. The bookmark was named “bm1”. Assume we have the following text in the word document:

Word, VBA, Bookmark and text
Assume the image is called “SP_A0155.jpg” with the same location as the previous example, “D:StuffBusinessTemp”. The code below will insert the image at the location of the bookmark:


Sub Example2()
'move the cursor to the bookmark
Selection.GoTo What:=wdGoToBookmark, Name:="bm1"
'insert the image
Selection.InlineShapes.AddPicture FileName:= _
"D:StuffBusinessTempSP_A0155.jpg", LinkToFile:=False, _
SaveWithDocument:=True
End Sub

Result:

Word, VBA insert Image Result


Insert Image Using Open File Dialog:

You can use open file dialogs to ask the user to select the path of the image to insert. In the example below the user will be asked to select the location of the image to insert:

Sub Example3()
Dim intChoice As Integer
Dim strPath As String

'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
End If
'move the cursor to the bookmark
Selection.GoTo What:=wdGoToBookmark, Name:="bm1"
'insert the image
Selection.InlineShapes.AddPicture FileName:= _
strPath, LinkToFile:=False, _
SaveWithDocument:=True
End Sub

The open file dialog allows the user to select the image to insert:

Word, VBA, Open File Dialog Insert Image


Insert All Images in a Folder:

In the article below I’ve explained how you can use VBA to list all the files in a folder:

Also in the article below I’ve explained how you can use folder dialogs to ask the user to select a folder:

by combining the two and using the code explained in the previous sections we get:

Sub example4()
Dim intResult As Integer
Dim strPath As String
Dim strFolderPath As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
'dispaly message box
strFolderPath = Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1)
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(strFolderPath)
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
'get file path
strPath = objFile.Path
'insert the image
Selection.InlineShapes.AddPicture FileName:= _
strPath, LinkToFile:=False, _
SaveWithDocument:=True
Next objFile
End If
End Sub

A folder dialog is displayed asking the user to select the folder with the images in:

Word, VBA, Folder Dialog
After selecting a folder, all the images in that folder are inserted in the word document:

Word, VBA, Insert Images Result

You can download the file and code related to this article from the link 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

5 thoughts on “Word VBA Insert Images”

  1. cshaa says:

    thanks a lot..u saved my time so preciously..

  2. Satheesh says:

    Wonderful. Thank a lot.

  3. gourish says:

    How to export all images to local drive from word document. Can anyone help me on this issue.
    Thank you so much 🙂

  4. Ben Linford says:

    Does anybody have any idea how to insert an image from a URL?

  5. Luis Armas says:

    Can anyone help me with resizing the image selected with the first example code pls?

Leave a Reply

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