Log in

View Full Version : Insert multiple pictures with filename caption



vwdevotee
10-27-2011, 01:10 PM
Hi. I'm new to VBA, but was hoping someone could help me out. I found this old macro here:



Sub InsertPicture2()

Dim Doc As Dialog
Dim BClicked As String
Dim picPath As String
Dim picName As String

Set Doc = Dialogs(wdDialogInsertPicture)
With Doc
.Name = "*.jpg"
BClicked = .Show 'using show executes the dialog
picPath = .Name
End With
'strip off the path to get the file name
picName = Right(picPath, Len(picPath) - InStrRev(picPath, "\"))

'only do this if OK was clicked
If BClicked = -1 Then
Selection.InsertCaption Label:="Figure", _
TitleAutoText:="", Title:=": " & picName, _
Position:=wdCaptionPositionBelow
End If
End Sub


but it only puts one caption at the bottom of a long slew of pictures rather than the filename caption under each picture. Can anyone point me towards a version of this that puts individual captions in or could anyone please show my what's wrong with this? (that way I can start figure out how to do things automagically on my own) Thanks in advance.

Damon

Tinbendr
10-28-2011, 02:25 PM
Look at this message. (http://www.vbaexpress.com/forum/archive/index.php/t-6100.html)

If you select multiple pictures, then yes, it's acting as designed. It will insert all the pictures selected and THEN insert the caption after that.

There are several ways to handle this.

Can you tell us a little bit about how you're planning on using this?

vwdevotee
10-28-2011, 03:21 PM
The plan was to use the macro to insert of butt load of pictures from a directory, have it put a caption under each one with "Figure {incremented figure number}. Filename.jpg."

There's a spot in that thread (where I got the original code anyway) where it says to put a carriage return in after the caption insert, but I can't tell where to put that in. :0(

As an add-in question, is Word 2010 substantively different that this some code shouldn't work in it? I have 2003 at work and 2010 at home and it won't let me test it hear. It tries to make me select word files instead of jpg.

Tinbendr
10-31-2011, 12:50 PM
This works in 2007.

Sub InsertSelectedPixs()
'Declare a variable as a FileDialog object
Dim fd As FileDialog
Dim picPath As String

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.

Dim vrtSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.

With fd
'Set the initial path to the C: drive.
.InitialFileName = "*"
.AllowMultiSelect = True
.Filters.Clear
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.tif"

'Use the Show method to display the File Picker dialog box and return the user's action.

'If the user presses the action button...

If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.

For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem is a String that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example simply displays the path in a message box.
Set MyShape = ActiveDocument.InlineShapes.AddPicture(vrtSelectedItem)
'Parse the filename only
picName = Right(vrtSelectedItem, Len(vrtSelectedItem) - InStrRev(vrtSelectedItem, "\"))

'We have to select the Shape as InsertCaption only works with Selection.
MyShape.Select
Selection.InsertCaption Label:="Figure", _
Title:=": " & picName, Position:=wdCaptionPositionBelow
Selection.Collapse wdCollapseEnd

Next vrtSelectedItem

'If the user presses Cancel...
Else
End If
End With

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

End Sub