从 Excel 使用 VBA 在 PowerPoint 中获取形状的尺寸

From Excel get the dimensions of a shape in PowerPoint with VBA

我正在尝试构建一个工具,用户可以通过该工具单击 Excel 中的按钮并获取 PowerPoint 中所选形状的高度、宽度、顶部、左侧属性(使他们能够调整形状的大小Excel 更有效)。

目前我似乎无法在 PowerPoint 中引用选定的形状,尽管有以下代码:

Dim PowerPointApp As Object
Dim ActivePresentation As Object
Dim ActiveSlide As Object

Public Sub getDimensionsFromPowerPoint()

    'Create an Instance of PowerPoint
    On Error Resume Next

    'Is PowerPoint already opened?
    Set PowerPointApp = GetObject(Class:="PowerPoint.Application")

    'Clear the error between errors
    err.Clear

    'If PowerPoint is not already open then open PowerPoint
    If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(Class:="PowerPoint.Application")

    'Handle if the PowerPoint Application is not found
    If err.Number = 429 Then
        MsgBox "PowerPoint could not be found, aborting."
        Exit Sub
    End If

    On Error GoTo 0

    'Optimize Code
    Application.ScreenUpdating = False

    'Create a New Presentation

    Set ActiveShape = PowerPointApp.ActivePresentation.ActiveWindow.Selection

    Debug.Print ActiveShape.width

End Sub

我有一种感觉,我没有正确地与 PowerPoint 交互,但看不出还有什么其他的可能。

来自该网页https://msdn.microsoft.com/en-us/library/office/ff745871.aspx

看来您需要使用 ShapeRange 方法来获取对形状的引用。

我认为 ShapeRange 支持 For Each,因为 (1) 因此您可以遍历它们。

(1) 支持 For Each 的证据是存在隐藏的 _NewEnum 方法。

而不是:

 Set ActiveShape = PowerPointApp.ActivePresentation.ActiveWindow.Selection

使用:

 Set ActiveShape = PowerPointApp.ActiveWindow.Selection.ShapeRange(1)