PDA

View Full Version : Inlineshape.width and .height only works when debugging



Hawkansson
04-20-2017, 01:32 AM
Hi,

I'm inserting a picture in a Word table cell. Then I resize it using .Width and .Height. The strange thing is that it only works when I debug the code, using F8 to step through the commands.

When I remove the break points and execute the code, the picture doesn't resize. It gets the same width as the table cell... This is the code I'm using:



Dim oPic As InlineShape
Set oPic = ActiveDocument.InlineShapes.AddPicture(plotLocation & "\" & fileName, False, True, CellRange)
With oPic
.LockAspectRatio = False
.Width = CentimetersToPoints(15.5)
.Height = CentimetersToPoints(10)
End With



Note: plotLocation, fileName, CellRange are defined earlier in the code.

Any ideas?

Best regards,
David

gmayor
04-20-2017, 03:54 AM
It works fine here - given that a cell width of approximately 6 inches is pretty large?


Option Explicit
Sub Macro1()
Dim oPic As InlineShape
Dim plotLocation As String
Dim CellRange As Range
Dim FileName As String

plotLocation = "D:\My Documents\My Pictures"
FileName = "Filename.jpg"
Set CellRange = ActiveDocument.Tables(1).Cell(1, 1).Range
CellRange.End = CellRange.End - 1 'Remove the cell end character.
Set oPic = ActiveDocument.InlineShapes.AddPicture(plotLocation & "\" & FileName, False, True, CellRange)
With oPic
.LockAspectRatio = False
.Width = CentimetersToPoints(15.5)
.Height = CentimetersToPoints(10)
End With
End Sub



Bear in mind, if the aspect ratio of your picture is correct and the width of the picture is larger than the cell width. If you set the cell width to fixed, the image will shrink to the width of the cell and the height will maintain its aspect ratio.

Hawkansson
04-20-2017, 07:51 AM
Yes, my table is 16.75 cm wide. It works for me too if I put it in a new document but not in my template file..

I suspected that the AddPicture command took some time to finish, and that that was the reason the width and height commands didn't work so I tried to insert some Pause functions in between, even as long as 15 seconds, to no avail...

I'm stuck here. Don't know what to look for..