ukie
ukie

Reputation: 99

Word VBA Insert Image with Specific Height and Fixed Aspect Ratio

I have an image C\VER\Image.png

It needs to be pasted over a selected text (inline) in Word 2007 with a specific height (constraining the image proportions or ratio).

Here is what I have:

Dim dutPic As Word.InlineShape

Set dutPic = Selection.InlineShapes.AddPicture(fileName:=imagePath, _
LinkToFile:=False, SaveWithDocument:=True)

dutPic.LockAspectRatio = msoTrue
dutPic.Height = 170

The height is changed but the width does not follow.

Upvotes: 0

Views: 6097

Answers (1)

ukie
ukie

Reputation: 99

Here is the workaround:

Dim dutPic As Word.InlineShape
Dim oH As Long, oW As Long 'Original Dimensions of the image
Dim nW As Double, aspect As Double

Set dutPic = Selection.InlineShapes.AddPicture(fileName:=imagePath, _
  LinkToFile:=False, SaveWithDocument:=True)

oW = dutPic.Width
oH = dutPic.Height
aspect = oW / oH 'aspect ratio
nW = aspect * 170 'new width

dutPic.Height = 170 'Desired height
dutPic.Width = nW

Calculate the aspect ratio yourself.

Upvotes: 2

Related Questions