VBA Shapes.Count miscounting

satkin2

New Member
Joined
Oct 22, 2009
Messages
25
Hi,
I've got a VBA script set up which copies a range from Excel, and pastes it in to PowerPoint.
I then count which shape number on the slide it is, so that I can resize it and place it on the slide where I want it.

I get a problem when counting the shapes on the slide.

After I have pasted in to PowerPoint, I loop through each shape on the slide.
VBA Code:
   Dim shp As PowerPoint.Shape
   Dim ShapeNum As Long
   For Each shp In pSlide.Shapes
    ShapeNum = ShapeNum + 1
    'MsgBox "Shape Counted"
   Next

As you'll see, I've got a MsgBox on each count. If this is commented out, my final ShapeNum = 3. However my pasted shape is shape 4 in this example.
If I uncomment the MsgBox, it counts 4 times and gives ShapeNum the correct value.

It's almost as if it's going too fast. However, I tried putting in a delay, and that didn't work. It only gets the correct value when I use the MsgBox line.

I'm sure this isn't the way to do it, so is anyone able to advise on how I can do this and get the correct result without a msgbox?

Many thanks
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How are you checking the final ShapeNum with the message box commented out?
 
Upvote 0
Are you wanting to do anything inside the loop
- if so what is that

Otherwise if you simply want the count for the slides, a loop is not required
countOfShapes = pSlide.Shapes.Count

LATE EDIT
Just in case you trip over this one too ...
The shapes collection is a zero based array - ie it starts at zero
So if there are 4 shapes in the sheet, refer to them like this
pSlide.Shapes(0)
pSlide.Shapes(1)
pSlide.Shapes(2)
pSlide.Shapes(3)

or in a loop
VBA Code:
For x = 0 to pSlide.Shapes.Count -1
  pSlide(x) ...
Next x
 
Last edited:
Upvote 0
How are you checking the final ShapeNum with the message box commented out?
My next action is to resize the shape, based on the ShapeNum.
VBA Code:
With pSlide.Shapes(ShapeNum)
    .Left = CM_To_Points(posLeft)
    .Top = CM_To_Points(posTop)
    .Width = CM_To_Points(sizeWidth)
    .Height = CM_To_Points(sizeHeight)
   End With
With the MsgBox commented out, I get an error as it's looks for ShapeNum 3 and doesn't like it, so just by debugging I found the issue.
 
Upvote 0
My next action is to resize the shape, based on the ShapeNum.
VBA Code:
With pSlide.Shapes(ShapeNum)
    .Left = CM_To_Points(posLeft)
    .Top = CM_To_Points(posTop)
    .Width = CM_To_Points(sizeWidth)
    .Height = CM_To_Points(sizeHeight)
   End With
With the MsgBox commented out, I get an error as it's looks for ShapeNum 3 and doesn't like it, so just by debugging I found the issue.

You will understand if you read below LATE EDIT on post#3
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,042
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top