在 VBA 中确定图片对象的实际大小和显示大小

Determine actual and displayed size of picture object in VBA

在 VBA 中,我试图确定工作表上图片对象的实际大小和显示大小。由于比例因素,显示的尺寸可能与实际尺寸不同。到目前为止,我已经找到了方法 ScaleWidthScaleHeight,但我并不想实际修改图片对象。有什么想法吗?

很遗憾,原来的三围好像不是一张public属性的图片。如果您不想修改原始图片,您可以创建该图片的副本,仅用于缩放目的。

这个函数接受一个形状(在我们的例子中是一张图片)和returns一个单一类型的数组(宽度和高度)

Private Function GetOriginalMeasurements(ByRef myShape As Excel.Shape)
    Dim shpCopy As Excel.Shape
    Dim measurements(1) As Single

    Set shpCopy = myShape.Duplicate

    ' Reset original measurements
    shpCopy.ScaleHeight 1, msoTrue

    measurements(0) = shpCopy.width
    measurements(1) = shpCopy.height

    shpCopy.Delete

    GetOriginalMeasurements = measurements
End Function

Main() 过程只是如何使用它的一个基本示例

Sub Main()
    Dim myShape As Excel.Shape
    Dim measurements() As Single
    Dim width As Single
    Dim height As Single

    Set myShape = ActiveWorkbook.ActiveSheet.Shapes(1)
    measurements = GetOriginalMeasurements(myShape)

    width = measurements(0)
    height = measurements(1)

    Debug.Print width
    Debug.Print height
End Sub

在我的电脑上复制和删除形状是即时的,但如果你看到一些闪烁,你可能希望关闭该功能中的屏幕更新。