PDA

View Full Version : How to get multiple images from Insert Picture dialog



hak_888
11-04-2012, 07:26 AM
Hey guys,
I have a WORD document, that should allow user to insert multiple images ( using Insert Picture dialog ), after which I should run some VBA code to position and crop images. The problem I have now, is I don't know how to get multiple images selected from dialog. Here's the code I'm using to get the 1 selected image

With oDialog
' \\ Display the dialog
.Display

' \\Insert InlineShape if the Name property (Filepath) <> ""
If .Name <> "" Then
ActiveDocument.Shapes.AddPicture FileName:=.Name, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=5, _
top:=top, _
Width:=50, _
Height:=50, _
Anchor:=Selection.Range
End If
End With

Can you please help me out here, as I'm completely new to VBA. How to change the above code, to get all the selected images ?

Thanks in advance.

gmaxey
11-05-2012, 03:33 PM
I don't think you can using the InsertPicture dialog. You can use a file picker dialog though. If will also have to modify your postioning code or all the images will be stacked on top of each other:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oFD As FileDialog
Dim vSelectedItem As Variant
Dim oShp As Word.Shape
Dim i As Long
Set oFD = Application.FileDialog(msoFileDialogFilePicker)
i = 0
With oFD
If .Show = -1 Then
For Each vSelectedItem In .SelectedItems
Set oShp = ActiveDocument.Shapes.AddPicture(FileName:=vSelectedItem, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=5 + i, _
Top:=50 + i, _
Width:=50, _
Height:=50, _
Anchor:=Selection.Range)
i = i + 5
Next vSelectedItem
End If
End With
Set oFD = Nothing
End Sub


You might find this useful: http://gregmaxey.mvps.org/word_tip_pages/photo_gallery_add_in.html

hak_888
11-06-2012, 11:37 PM
Hi,
Thanks a ton, this is exactly what I was looking for!