I have a section of code that uses Late Binding on PowerPoint, and one of the actions that I wish to perform is to Merge 2 shapes.
When I attempt this, I get "Run-time error '13': Type mismatch". Specifically, this only occurs when I initially declare the ShapeRange as an Object (because, Late Binding), rather than as a ShapeRange (which requires Early Binding). When calling the following 2 functions within PowerPoint, the first (MergeShapes_Success
) will succeed, while the second (MergeShapes_Failure
) will fail. (The first function will also succeed if called from another Application using Early Binding)
The only difference between the two functions is whether shpRng
is Declared as Object
or ShapeRange
.
Private Function MergeShapes_Success(ByRef Shape1 As Object, Shape2 As Object) As Boolean
Dim shpRng As PowerPoint.ShapeRange 'This is the only line that differs
If Not (Shape1.Parent Is Shape2.Parent) Then
MsgBox "Shapes to Merge must be on the same Slide!", vbCritical, "Error!"
MergeShapes_Success = False
Exit Function
End If
Set shpRng = Shape1.Parent.Shapes.Range(Array(Shape1.ZOrderPosition, Shape2.ZOrderPosition))
shpRng.MergeShapes msoMergeUnion
' Shapes Merge Successfully
MergeShapes_Success = True
End Function
Private Function MergeShapes_Failure(ByRef Shape1 As Object, Shape2 As Object) AS Boolean
Dim shpRng As Object 'This is the only line that differs
If Not (Shape1.Parent Is Shape2.Parent) Then
MsgBox "Shapes to Merge must be on the same Slide!", vbCritical, "Error!"
MergeShapes_Failure = False
Exit Function
End If
Set shpRng = Shape1.Parent.Shapes.Range(Array(Shape1.ZOrderPosition, Shape2.ZOrderPosition))
shpRng.MergeShapes msoMergeUnion
' /*************************
' ** Run-time error '13': **
' ** Type mismatch **
' *************************/
MergeShapes_Failure = True
End Function
I have checked the TypeName
of shpRng after I Set
it (which returns "ShapeRange" in both cases), and I have confirmed that TypeOf shpRng Is ShapeRange
in both cases too.
Is anyone able to advise as to why this happens and, ideally, how to make this function… well, function, with Late Binding?
The error message does not mean that the declaration (As Object
) is wrong... The parameters of `MergeShapes1 are not suitable. Please, test the adapted function:
Private Function MergeShapes_Failure(ByRef Shape1 As Object, Shape2 As Object) As Boolean
Dim shpRng As Object
If Not (Shape1.Parent Is Shape2.Parent) Then
MsgBox "Shapes to Merge must be on the same Slide!", vbCritical, "Error!"
MergeShapes_Failure = False
Exit Function
End If
Set shpRng = Shape1.Parent.Shapes.Range(Array(Shape1.ZOrderPosition, Shape2.ZOrderPosition))
On Error GoTo Problem
shpRng.MergeShapes 1, Shape1 'The second parameter is required, even if in documentation is Optional
'Using Late Binding the constant msoMergeUnion does not have much of a meaning, too...
MergeShapes_Failure = True
Exit Function
Problem:
End Function