【问题标题】:From Excel get the dimensions of a shape in PowerPoint with VBA从 Excel 中使用 VBA 在 PowerPoint 中获取形状的尺寸
【发布时间】:2016-09-27 16:17:45
【问题描述】:

我正在尝试构建一个工具,用户可以在该工具中单击 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 的交互不正常,但看不到其他情况。

【问题讨论】:

  • 尝试debug.Print typename(ActiveShape) 来验证您的假设,即您有一个形状。 ActiveShape 在您的代码中定义在哪里?
  • 我不知道如何在 powerpoint 中做到这一点,但在 excel 中是这样的:Dim Pic as Shape : Set Pic = Selection.Shaperange(1)

标签: vba excel ms-office powerpoint


【解决方案1】:

代替:

 Set ActiveShape = PowerPointApp.ActivePresentation.ActiveWindow.Selection

用途:

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

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多