I need help and it is obvious. I have cut together code that will allow a directory selection then import all the pictures in that directory. The macro works but needs two more functions to work best.
I wrote some of this myself and I got some of the source code here and some from other sites. I tried to post elsewhere but the question must be too tough for that forum.
I often will have to write reports that will have very many imported pictures that I want to move around the document and resize. when the picture and caption are a group this works well. I do it often and want the import function to do those last two steps.
I start with a word document that has the width set to 1.75". This makes the pictures start small.
When the macro runs it will allow selection of the directory. Import each picture (sized to 1.75 wide) with tight text wrap. The caption is added below the picture as the filename. (I expand the filename with the detail I want in the caption.)
I then take all the photos and copy them into the report and move them around as necessary.
I search here and many other sites. I can't find the code to select the caption textfield and then the command to expand the selection to include the picture or group the caption and picture into one element.
Here is the code, (FrankenCode), I have so far: (thanks to all who provided some part!) (and I don't think i included the code correctly here.)
Sub Picture_Import_with_Caption()
Dim intResult As Integer
Dim strPath As String
Dim PicName As String ' new picture name variable
Dim strFolderPath As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
Dim ThisPic As InlineShape
Dim ThisPicCap As InlineShape
'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
Set ThisPic = ThisDocument.InlineShapes(ThisDocument.InlineShapes.Count)
' this bit selects the picture that was just inserted
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend ' try to select
' This bit sets the picture to tight word wrap.
Selection.ShapeRange.WrapFormat.Type = wdWrapTight ' lets try this
' remove the path from strPath
PicName = Right(strPath, Len(strPath) - InStrRev(strPath, "\"))
' add the new caption to the picture
Selection.InsertCaption Label:="Picture", TitleAutoText:="InsertCaption", _
Title:=": " + PicName, Position:=wdCaptionPositionBelow, ExcludeLabel:=0
' ThisPic.Select 'this does select the picture
' Set ThisPicCap = (msoTextBox) this does not work
'
' At this point I want to select the picture and the text box caption together.
' After they are both selected Group the two as one unit. (array)
' next deselect the group so a line can be added below and next file can be imported.
'
'
' blank line added after each
Selection.TypeParagraph
'
Next objFile
End If
End Sub