DrS.
DrS.

Reputation: 67

vba shapes.type gives error in powerpointvba

My code does not work. I am probably not using the right syntax and mistake is hopefully very very easy.

Error says something like wrong object definition.

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

Upvotes: 0

Views: 394

Answers (2)

Jamie Garroch - MVP
Jamie Garroch - MVP

Reputation: 2979

You should not use reserved keywords as variable names, in this case "Shape". Try using the convention whereby the first letter is the object type to help you when debugging e.g. o = object so Shape becomes oShp. Next, Shapes is a collection of objects of type Shape so if you want to loop through them, you need to do it as shown below. You also need to set i=i+1 earlier as you have set the array with a lower bound of 1 and the first time you get a property, i=0. Finally, ShpProp needs initialising and the array must be of type String as you are including non numeric values in the array. Modified code:

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

Upvotes: 1

Clay Sills
Clay Sills

Reputation: 235

Three things:

1) it doesn't look like you're dim-ing or providing an initial value for i.

2) you need to comment out the "End If" when you comment out the "If Not"

3) try putting Shape in your If statement:

If Not Shape.Type = msoPlaceholder Then

Upvotes: 1

Related Questions