|
#1
|
|||
|
|||
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
Thanks Elias |
#2
|
||||
|
||||
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] |
#3
|
||||
|
||||
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 |
#4
|
|||
|
|||
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 |
#5
|
||||
|
||||
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] |
#6
|
|||
|
|||
Yes, that worked! Thank you for your help!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Mac - Macro insert pictures in table | Nina | Word VBA | 16 | 08-27-2018 01:53 AM |
Macro to insert multiple pictures with adjustable column size | nando88 | Word VBA | 6 | 05-09-2016 11:56 PM |
Insert an article with pictures into a table cell | skatiemcb | Word Tables | 2 | 01-24-2015 08:18 AM |
Macro to insert multiple pictures to word to a specific size and text wrap | mescaL | Word VBA | 3 | 11-03-2014 10:51 PM |
Resize multiple pictures in a Word 2010 table | JBA479 | Word VBA | 1 | 01-24-2014 08:51 PM |