Image size when setting image in Word using VBA (1 Viewer)

Zakraket

Registered User.
Local time
Today, 08:06
Joined
Feb 19, 2013
Messages
88
In a certain database I'm setting (square-sized) images in a table in word.

The code should handle 1 or two images in a table in a wordtemplate. The code finds the correct table, add a column if needed and places 1 or 2 images in the cell (or two cells)

wTbl is a word table object and in this case the table I want to place images in

Code:
'objWord.Visible = True
'objWord.Activate

'set columns
For i = 1 To tItem
 wTbl.Columns.PreferredWidthType = wdPreferredWidthPercent
   If i > 1 Then
	  wTbl.Columns.Add
   End If
   wTbl.Cell(1, i).VerticalAlignment = wdCellAlignVerticalCenter
Next i
wTbl.PreferredWidthType = wdPreferredWidthPercent
wTbl.PreferredWidth = 100

'set images
For i = 1 To tItem
   strFilename = Nz(lookupLV("LISTVALUE2", rstLV, _
				   "[DELETED]=0 AND [LISTVALUEGROUP]='BRIDGETYPEGROUP' AND [LISTVALUESHORT]='" & Split(rstQ!BRIDGETYPE, ";")(i - 1) & "'"), "")
   If IsNothing(strFilename) Then strFilename = GetSetting("NoImage")
   
   Set wImg = wTbl.Cell(1, i).Range.InlineShapes.AddPicture(FileName:=strFilename, LinkToFile:=False, SaveWithDocument:=True)
   wImg.AlternativeText = "BridgetypeImage" & i
   wImg.LockAspectRatio = True
   wImg.Width = wTbl.Cell(1, i).Width - wTbl.Cell(1, i).RightPadding * 2
   Debug.Print wImg.AlternativeText & " " & wImg.Height
Next i
tItems is always 1 or 2

It works....

However: based on the code and my template (that has a certain layout), the images should get a height of 234, being the width of the cells in the case of my template.


When I loop this code with the Wordobject visible, my image.height get set to this value and everything is correct.

When I loop the code and the Wordobject is NOT visible (I want this since it speeds up building the worddocument greatly, and I don't want to show the users a worddocument that's being built), none of the heightsettings seem to work
The images are enlarged to the width of the table....?

In both cases the code is exactly the same, the only difference is that I switch the first two lines on or off. The result is not the same.

When I add a line like:
If wImg.Height > 234 Then wImg.Height = 234
it works, I get images with 234 height

When I check the cell.width using debug.print cell.widht, the cells are reported at 490 (width of the table), but when I have the document visible, they are reported as 234.
It seems cellwidth is not available on a not visible worddocument?
 
Last edited:

Users who are viewing this thread

Top Bottom