vba shapes.type 在 powerpoint 中给出错误vba

vba shapes.type gives error in powerpointvba

我的代码不起作用。我可能没有使用正确的语法,希望很容易出错。

错误表示对象定义错误。

SlideNumber = ActiveWindow.Selection.SlideRange.SlideNumber

ShpCount = ActivePresentation.Slides(SlideNumber).Shapes.count

ReDim ShapeItem(1 To ShpCount, 1 To ShpProp)

For Each Shape In ActivePresentation.Slides(SlideNumber).Shapes

    'If Not .Type = msoPlaceholder Then <-- This Line is not working!

        ShapeItem(i, 1) = Shape.Name

        ShapeItem(i, 2) = Shape.top

        ShapeItem(i, 3) = Shape.left

        ShapeItem(i, 4) = Shape.Height

        ShapeItem(i, 5) = Shape.Width

        ShapeItem(i, 6) = Shape.top + (Shape.Height / 2)

        ShapeItem(i, 7) = Shape.left + (Shape.Width / 2)

        ShapeItem(i, 8) = Shape.Id

        ShapeItem(i, 9) = Shape.ZOrderPosition

        ShapeItem(i, 10) = Shape.Title

        i = i + 1

    End If

Next

三件事:

1) 看起来你并没有调暗或为 i 提供初始值。

2) 注释掉"If Not"

的同时需要注释掉"End If"

3) 尝试将 Shape 放入 If 语句中:

If Not Shape.Type = msoPlaceholder Then

您不应使用保留关键字作为变量名,在本例中为 "Shape"。尝试使用第一个字母是对象类型的约定来帮助您进行调试,例如o = 对象,因此 Shape 变为 oShp。接下来,Shapes 是 Shape 类型对象的集合,所以如果你想遍历它们,你需要按如下所示进行。您还需要更早地设置 i=i+1,因为您已将数组的下限设置为 1,并且第一次获得 属性 时,i=0。最后,ShpProp 需要初始化并且数组必须是 String 类型,因为您在数组中包含非数值。修改代码:

Option Explicit
Option Base 1

Sub GetShapeProperties()
  Dim oShp As Shape
  Dim i As Integer
  Dim SlideNumber As Long
  Dim ShpCount As Long, ShpProp As Integer
  ' If you want to get the current slide, this is the best method:
  ' SlideNumber = ActiveWindow.View.Slide.SlideIndex
  SlideNumber = ActiveWindow.Selection.SlideRange.SlideNumber
  ShpCount = ActivePresentation.Slides(SlideNumber).Shapes.Count
  ShpProp = 10
  ReDim ShapeItem(1 To ShpCount, 1 To ShpProp) As String
  For Each oShp In ActivePresentation.Slides(SlideNumber).Shapes
    With oShp
      If Not .Type = msoPlaceholder Then
        i = i + 1
        ShapeItem(i, 1) = .Name
        ShapeItem(i, 2) = CStr(.Top)
        ShapeItem(i, 3) = CStr(.Left)
        ShapeItem(i, 4) = CStr(.Height)
        ShapeItem(i, 5) = CStr(.Width)
        ShapeItem(i, 6) = CStr(.Top + (.Height / 2))
        ShapeItem(i, 7) = CStr(.Left + (.Width / 2))
        ShapeItem(i, 8) = CStr(.Id)
        ShapeItem(i, 9) = CStr(.ZOrderPosition)
        ShapeItem(i, 10) = .Title
      End If
    End With
  Next
End Sub