Skip to content

April 2, 2010

5

Insert Multiple image quickly into Microsoft Word 2007

Ever wanted to quickly insert multiple images in to Microsoft Word? The following macro allows you to do this but as an added extra it will also add a caption underneath each image with the name of the file. Handy for all those images you need to include in your reports!

When in Word 203/2007, press alt-F11 and you will be greeted with the visual basic module.  Copy and Paste the code in to the module window under the Normal document (this is your template document, this will allow you to use this code on all new documents).

Sub InsertImages()
Dim doc As Word.Document
Dim bkmName As String
Dim SigFile As String
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set doc = ActiveDocument
Dim vrtSelectedItem As Variant
Dim theText() As String
Dim theText2 As StringWith fd
.Filters.Add “Images”, “*.gif; *.jpg; *.jpeg”, 1

‘Sets the initial file filter to number 2.
.FilterIndex = 1

If .Show = -1 Then

For Each vrtSelectedItem In .SelectedItems
Set mg2 = ActiveDocument.Range
mg2.Collapse wdCollapseEnd

doc.InlineShapes.AddPicture _
FileName:=vrtSelectedItem, _
LinkToFile:=False, SaveWithDocument:=True, Range:=mg2
Set mg1 = ActiveDocument.Range
mg1.Collapse wdCollapseEnd
‘mg1.MoveEnd Unit:=wdCharacter, Count:=-1

theText() = Split(vrtSelectedItem, “\”)
theText2 = theText(UBound(theText))
theText2 = Left(theText2, InStrRev(theText2, “.”) – 1)

mg1.Text = Chr(13) + Chr(10) + theText2 + Chr(13) + Chr(10) + Chr(13) + Chr(10)
Next vrtSelectedItem
Else
End If
End With

‘Set the object variable to Nothing.
Set fd = Nothing

End Sub

5 Comments Post a comment
  1. Travis Smith
    Aug 26 2010

    This Macro works gret in Word 2007 but in Word 2003 it does not work.

  2. Susan M
    Oct 17 2011

    Didn’t work in 2010. Received Syntax Error on:
    Dim theText2 As StringWith fd

  3. Oct 18 2011

    I will take a look and see what is causing the problem. Thanks for your feedback BTW.

  4. Oct 18 2011
  5. Jason
    May 14 2012

    In word 2011 it gives the following compile error:

    “User-defined type not defined” while highlighting the code “fd As FileDialog”

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

*

This blog is monetized using Are-PayPal WP Plugin