Share via

Duplicate Shape in VBA throws a runtime 13 error

Anonymous
2021-09-15T10:39:15+00:00

Hi,

I'm placing a grid of shapes on a slide using vba. The first created shape (called shpOrig) is my template, which is then duplicated several times. There are more rows in my original code, but I've reduced all non necessary lines.

I'm not that familiar with coding in powerpoint, more comfortable with excel, so I actually tried this in Excel first and it went well. Using the same code in powerpoint, just making sure Slide is used instead of Worksheet, it didn't go that well - I got runtime 13, Type Mismatch.

Sub CreateShapes() 

    Dim TargetSlide As Slide 

    Dim shpOrig As Shape 

    Dim shpCopy As Shape 

    Dim x As Long 

    Dim y As Long 

    Set TargetSlide = ActivePresentation.Slides(1) 

    Set shpOrig = TargetSlide.Shapes.AddShape(msoShapeOval, 0, 0, 40, 40) 

    For y = 0 To 9 

        For x = 0 To 9 

            Set shpCopy = shpOrig.Duplicate 

            shpCopy.Left = 100 + 40 * x 

            shpCopy.Top = 10 + 40 * y 

            Set shpCopy = Nothing 

        Next x 

    Next y 

End Sub

The line that throws an error is

Set shpCopy = shpOrig.Duplicate

A few odd things.

The shpOrig is placed on the slide as well as the first copied shape, even though it's not placed correctly since Left and Top properties have not yet kicked in. So I get a copy even though when hovering over shpCopy it says "shpCopy = Nothing"

The error says Type Mismatch, but both shapes are declared as Shape. If I change the shpCopy from Shape to Variant it works. But I'm not in the mood to just accept a working solution - I'm puzzled why it can't be declared as Shape, and why two equally declared objects get a mismatch. As I said before, this works in excel.

Have I missed something that can't be done in powerpoint?

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2021-09-15T10:53:13+00:00

Duplicate returns a ShapeRange not a Shape so that will be the problem.

If you set shpCopy to the first item in the ShapeRange that should work

For y = 0 To 9

    For x = 0 To 9 

        Set shpCopy = shpOrig.Duplicate(1) 

        shpCopy.Left = 100 + 40 \* x 

        shpCopy.Top = 10 + 40 \* y 

        Set shpCopy = Nothing 

    Next x 

Next y

Was this answer helpful?

0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2021-09-16T08:58:10+00:00

    Ah, must read more carefully in the Docs.

    In Excel it actually returns a Shape, while in Powerpoint it returns a ShapeRange.

    Ok, assuming things obviously seems bad.

    Thanks again.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-09-16T08:51:42+00:00

    Marvelous, thanks a lot.

    I'm still puzzled why it works in Excel though. But as you said, it returns a shaperange (which is what the Docs also says, missed that).

    Much appreciated!

    Was this answer helpful?

    0 comments No comments