Need Macro Help to Insert Pictures into Table with a Picture per R

A

alex20850

Here is what I have done so far just by doing Record New Macro.

Sub InsertPix()
'
' InsertPix Macro
' Macro recorded 9/25/2008 by Alex Campbell
'
Selection.InlineShapes.AddPicture FileName:= _
"U:\Ax\1-HowTo\Maillist\20080826-01.gif", LinkToFile:=False, _
SaveWithDocument:=True
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.InlineShapes.AddPicture FileName:= _
"U:\Ax\1-HowTo\Maillist\20080826-02.gif", LinkToFile:=False, _
SaveWithDocument:=True
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.InlineShapes.AddPicture FileName:= _
"U:\Ax\1-HowTo\Maillist\20080826-03.gif", LinkToFile:=False, _
SaveWithDocument:=True
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.InlineShapes.AddPicture FileName:= _
"U:\Ax\1-HowTo\Maillist\20080826-04.gif", LinkToFile:=False, _
SaveWithDocument:=True
End Sub


I have NO experience writing macros.
I would like to start with a table with the first column empty and the
images in the second column.

I want to end up with the first column empty and each image is on a
different row in the second column.

The purpose of this to pull all the screenshots into a table where the first
column will be used for instructions explaining the images in the second
column.

I set the default location of Clipart Pictures with File Locations. It
would be very useful if the user could choose the file location as part of
the macro.

I created a table with one row and two columns.

I put the cursor in the second column, chose Insert, Picture, From File.

I highlighted the first picture and clicked on Insert.

Then I pressed tab twice.
The first is to create a new row.
The second was to move to the second column.

I would like the macro to move through the selected pictures in the folder.
 
G

Graham Mayor

The macro recorder is OK as far as it goes but is not going to take you much
farther here I'm afraid. Based on the information you have provided, as long
as the images have the same filename with the addition of "-nn" where nn is
an incrementing number, the following macro will extract the choices from
the user and insert the images into an existing table large enough to
accommodate them. The defaults reflect the filenames in your example.


Sub InsertPix()
Dim oTable As Table
Dim sQuery As String
Dim sName As String
Dim sNum As Long
Dim i As Long
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog 'Let the user select the folder containg the images
.Title = "Select Folder containing the images" _
& "to be inserted and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
DocDir = fDialog.SelectedItems.Item(1)
If Right(DocDir, 1) <> "\" Then DocDir = DocDir + "\"
End With
'Insert the common part of the filename - here 20080826
sName = InputBox("What is the common filename" _
& vbCr & "of the images", "insert Pix" _
, "20080826")
'If the first picture in the sequence is not 01 insert the correct start
number
sNum = InputBox("What is the start number of the picture sequence", _
"Insert Pix", 1)
'How many images in the sequence - they must be
'consecutively numbers for this to work.
sQuery = InputBox("Insert how many images?", "Insert Pix", 4)
Set oTable = ActiveDocument.Tables(1)
For i = sNum To sQuery
'name the images based on the collected info
sImage = DocDir & sName & "-" & format(i, "00") & ".gif"
'Choose the table cell to enter the next image
oTable.Cell(i, 2).Select
With Selection ' and insert it
.InlineShapes.AddPicture FileName:=sImage _
, LinkToFile:=False, _
SaveWithDocument:=True
End With
'repeat until all the images are inserted
Next i
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

alex20850

Thanks for your work, but I thought I had included in my original question
this macro I had already found and I would like to adapt to my use:

Use the FileDialog rather can the Word's Dialogs. The FileDialog collection
allows multiple selection.
From the VBA Help file's example for FileIndex Property I modified the code
to insert multiple graphics into the document.
Sub AddMuliplePictures()
'Declare a variable as a FileDialog object.
Dim fd As FileDialog

'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

'Add a filter that includes GIF and JPEG images and make it the
second item in the list.
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg"

'Sets the initial file filter to number 2.
.FilterIndex = 2

'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.

Selection.InlineShapes.AddPicture FileName:= _
vrtSelectedItem _
, LinkToFile:=False, SaveWithDocument:=True
Next vrtSelectedItem
'If the user presses Cancel...
Else
End If
End With

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

End Sub
 
G

Graham Mayor

You hadn't included the macro :(

The macro you propose will certainly do the job, but because of the way Word
selects files you will find that you have little control over the order in
which the images are inserted when using that method. If that doesn't
present a problem, start with a one row table and the following will add as
many rows as necessary and fill column 2 with the images.

Sub AddMultiplePictures()
Dim fd As FileDialog
Dim oTable As Table
Dim i As Long
Dim vrtSelectedItem As Variant
Set oTable = ActiveDocument.Tables(1)
oTable.AutoFitBehavior (wdAutoFitFixed)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
i = 1
With fd
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg"
.FilterIndex = 2
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
oTable.Cell(i, 2).Select
With Selection
.InlineShapes.AddPicture FileName:= _
vrtSelectedItem _
, LinkToFile:=False, SaveWithDocument:=True
.InsertRowsBelow 1
End With
i = i + 1
Next vrtSelectedItem
Else
End If
End With
Selection.Rows.Delete
Set fd = Nothing
End Sub

The original macro whilst not particularly elegant will, when used as
suggested, insert the pictures in the order in which they are selected.

I have an add-in available from my web site - boiler.dot - which uses a
different technique that provides control over the insertion order. It was
simple enough to modify that to insert the pictures into column 2 of a table
and you can download the modified version from
http://www.gmayor.com/Extras/InsertPix.dot. This is an add-in and should be
installed in the Word startup folder. It requires a one row table of at
least two columns to work. Sufficient rows to accommodate the pictures you
select will be added, using the same technique shown above.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top