PDA

View Full Version : How to put multiple images in word file using macro?



rgone9
09-09-2019, 04:57 AM
I want to create a macro which help me to put multiple images in word file. brief is below

1) The word document have tables
2) The images are already patsed into the left side of the table in word file
3) Just want to paste the images in the right side of the table in word file

Please find the attachment i want to add images in the "ab" table in sequence.

Thanks in advance.

gmaxey
09-09-2019, 05:41 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim lngIndex As Long
For Each oTbl In ActiveDocument.Tables
For lngIndex = 1 To oTbl.Rows.Count
If oTbl.Cell(lngIndex, 1).Range.InlineShapes.Count > 0 Then
oTbl.Cell(lngIndex, 1).Range.Cut
oTbl.Cell(lngIndex, 2).Range.Paste
End If
Next
Next
lbl_Exit:
Exit Sub
End Sub

gmayor
09-10-2019, 09:48 PM
Or you could do it using ranges, but somehow I get the impression it is a different set of images that need to be added, about which we know nothing.


Sub Macro1()'Graham Mayor - https://www.gmayor.com - Last updated - 11 Sep 2019
Dim oTable As Table
Dim oRow As Row
Dim oCell1 As Range, oCell2 As Range
For Each oTable In ActiveDocument.Tables
For Each oRow In oTable.Rows
If oRow.Cells(1).Range.InlineShapes.Count > 0 Then
Set oCell1 = oRow.Cells(1).Range
oCell1.End = oCell1.End - 1
Set oCell2 = oRow.Cells(2).Range
oCell2.End = oCell2.End - 1
oCell2.FormattedText = oCell1.FormattedText
oCell1.Text = "" 'if you want to remove the image from the left cell
End If
Next oRow
Next oTable
lbl_Exit:
Set oTable = Nothing
Set oRow = Nothing
Set oCell1 = Nothing
Set oCell2 = Nothing
Exit Sub
End Sub

rgone9
10-24-2019, 03:18 AM
Thanks gmaxey and gmayor for the help and sorry for the confusion for my 3rd point.

3) Just want to paste the images in the right side of the table in word file

I want to paste the images in the right side of the table in the word file from the folder. so can you please help me to do that? Thanks in advance.

gmayor
10-24-2019, 10:27 PM
You might consider https://www.gmayor.com/photo_gallery_template.html which has the option to create 1 column galleries with a caption column on the left, that doesn't have to have content