View Single Post
 
Old 11-29-2022, 02:07 PM
elias17 elias17 is offline Windows 11 Office 2021
Novice
 
Join Date: Nov 2022
Posts: 3
elias17 is on a distinguished road
Default Insert multiple pictures at once in Word table

Hi,


I have an inventory list where each item has a unique ID (called "Kürzel") based on its category in the format A 01, A 02, A 03, E 01, E 02 etc. Each item has a QR-Code (PNG File) with the same ID as a text value.
Screenshot 2.jpg


I now want to print out physical labels to then put on the items. Therefore I have a word template with a table. Each Label has two columns, one for the picture of the QR-Code and one for the text.
Screenshot 1.jpg


And this brings me to my question. I would like to insert all images at once (at least per column) so I do not have to do it indiviually. I found a great VBA code from this thread that I modified a bit:
Code:
Sub InsertMultipleImagesFixed()
 Dim fd As FileDialog
 Dim oTable As Table
 Dim iRow As Integer
 Dim iCol As Integer
 Dim oCell As Range
 Dim i As Long
 Dim sNoDoc As String
 Dim picName As String
 Dim scaleFactor As Long
 Dim max_height As Single
 'define resize constraints
 max_height = 275



 Set fd = Application.FileDialog(msoFileDialogFilePicker)
 With fd
 .Title = "Select image files and click OK"
 .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png; *.wmf"
 .FilterIndex = 2
 If .Show = -1 Then

 For i = 1 To .SelectedItems.Count

 iCol = 1
 iRow = i
 'get filename
 picName = Right(.SelectedItems(i), Len(.SelectedItems(i)) - InStrRev(.SelectedItems(i), "\"))
 'remove extension from filename ****
 picName = Left(picName, InStrRev(picName, ".") - 1)

 'select cell
 Set oCell = ActiveDocument.Tables(1).Cell(iRow, iCol).Range

 'insert image
 oCell.InlineShapes.AddPicture FileName:= _
 .SelectedItems(i), LinkToFile:=False, _
 SaveWithDocument:=True, Range:=oCell

 'resize image
 If oCell.InlineShapes(1).Height > max_height Then
 scale_factor = oCell.InlineShapes(1).ScaleHeight * (max_height / oCell.InlineShapes(1).Height)
 oCell.InlineShapes(1).ScaleHeight = scale_factor
 oCell.InlineShapes(1).ScaleWidth = scale_factor
 End If

 'center content
 oCell.ParagraphFormat.Alignment = wdAlignParagraphCenter

 Next i
 End If
 End With

 Set fd = Nothing
 End Sub
This works almost perfectly but there are two problems. It always inserts the pictures in the first column. I would like the images images that I select to paste into the column which I have currently selected. Also, if possible, it would be nice if the pictures had the following attributes:
  • Width and Height: 0,8cm
  • Layout options: In Front of Text and Fix position on page
  • Centered verically and horizontally
I would be very grateful if someone could help me out with modifying the code!


Thanks
Elias
Reply With Quote