Word VBA插入具有特定高度和固定宽高比的图像

时间:2014-03-19 23:38:40

标签: image ms-word word-vba

我有一张图片C \ VER \ Image.png

需要将其粘贴到Word 2007中具有特定高度的选定文本(内联)上(约束图像比例或比率)。

这就是我所拥有的:

Dim dutPic As Word.InlineShape

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

dutPic.LockAspectRatio = msoTrue
dutPic.Height = 170

高度已更改,但宽度未跟随。

1 个答案:

答案 0 :(得分:1)

以下是解决方法:

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

自己计算宽高比。