Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2022, 02:07 PM
elias17 elias17 is offline Insert multiple pictures at once in Word table Windows 11 Insert multiple pictures at once in Word table Office 2021
Novice
Insert multiple pictures at once in Word table
 
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
  #2  
Old 11-29-2022, 04:03 PM
macropod's Avatar
macropod macropod is offline Insert multiple pictures at once in Word table Windows 10 Insert multiple pictures at once in Word table Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So why are you trying to retrieve the images from external files if you already have them in your inventory list? Surely they could simple be replicated from there?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-29-2022, 04:15 PM
Guessed's Avatar
Guessed Guessed is offline Insert multiple pictures at once in Word table Windows 10 Insert multiple pictures at once in Word table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

At its most basic, assume your PNGs are all in the same folder as the Word document. Then this code will iterate through a column of text to add the corresponding image in the preceding cell
Code:
Sub InsertImages()
  Dim aCell As Cell, sPath As String, sFile As String, aTbl As Table, iCol As Integer
  Dim aRng As Range, aShp As InlineShape
  
  Set aTbl = Selection.Tables(1)
  iCol = Selection.Cells(1).Column.Index
  sPath = ActiveDocument.Path & Application.PathSeparator
  For Each aCell In aTbl.Columns(iCol).Cells
    sFile = sPath & Split(aCell.Range.Text, vbCr)(0) & ".png"
    If Len(Dir(sFile)) > 0 Then
      Debug.Print sFile
      Set aRng = aCell.Previous.Range
      Set aShp = ActiveDocument.InlineShapes.AddPicture(FileName:=sFile, Range:=aRng)
      aShp.AlternativeText = sFile
      aShp.Width = CentimetersToPoints(0.8)
      aShp.Height = CentimetersToPoints(0.8)
      aRng.Cells(1).VerticalAlignment = wdCellAlignVerticalCenter
      aRng.Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
    End If
  Next aCell
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 11-30-2022, 06:46 AM
elias17 elias17 is offline Insert multiple pictures at once in Word table Windows 11 Insert multiple pictures at once in Word table Office 2021
Novice
Insert multiple pictures at once in Word table
 
Join Date: Nov 2022
Posts: 3
elias17 is on a distinguished road
Default

Hi Andrew,

thank you very much for the code, it works perfectly!

Just one thing: the images are inserted with the layout option "In Line with Text" but I would need them to be "In Front of Text". Would this be possible?
Screenshot 3.png


Thanks,
Elias
Reply With Quote
  #5  
Old 11-30-2022, 07:53 AM
macropod's Avatar
macropod macropod is offline Insert multiple pictures at once in Word table Windows 10 Insert multiple pictures at once in Word table Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If you set the paragraph indents in those cells to 0 and change their left/right padding to 0 (which you might do for the whole table), the problem may well resolve itself.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 11-30-2022, 09:39 AM
elias17 elias17 is offline Insert multiple pictures at once in Word table Windows 11 Insert multiple pictures at once in Word table Office 2021
Novice
Insert multiple pictures at once in Word table
 
Join Date: Nov 2022
Posts: 3
elias17 is on a distinguished road
Default

Yes, that worked! Thank you for your help!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert multiple pictures at once in Word table Mac - Macro insert pictures in table Nina Word VBA 16 08-27-2018 01:53 AM
Insert multiple pictures at once in Word table Macro to insert multiple pictures with adjustable column size nando88 Word VBA 6 05-09-2016 11:56 PM
Insert multiple pictures at once in Word table Insert an article with pictures into a table cell skatiemcb Word Tables 2 01-24-2015 08:18 AM
Insert multiple pictures at once in Word table Macro to insert multiple pictures to word to a specific size and text wrap mescaL Word VBA 3 11-03-2014 10:51 PM
Insert multiple pictures at once in Word table Resize multiple pictures in a Word 2010 table JBA479 Word VBA 1 01-24-2014 08:51 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:13 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft