B. Baker
B. Baker

Reputation: 32

How would you access the Shape objects WITHIN a SmartArt object without getting Type Mismatch errors?

Question: Is there any way to access Shape objects for each node inside of a SmartArt object? It seems like SmartArtNode objects have a Shapes property that could be used for this purpose, but I am getting Type mismatch errors when I try to send shapes from the SmartArtNode.Shapes to other subroutines.

Background: I am writing an iterator subroutine that takes in 2 parameters: a SmartArt object and a custom class object that has a method to perform actions on Shape objects. The subroutine is supposed to iterate over the Shape objects for each node inside the SmartArt, and call a method from the custom class object on each Shape.

Here is code for the iterator subroutine:

Public Sub IterateOverShapesInSmartArt(mySmartArt As SmartArt, manipulator As ShapeManipulator)
    Dim node As SmartArtNode
    Dim shpRange As ShapeRange
    For Each node In mySmartArt.AllNodes
        Set shpRange = node.Shapes
        If shpRange.count > 0 Then
            manipulator.ManipulateShape shpRange.Item(1)
        End If
    Next node
End Sub

For reference, the signature on the custom class (ShapeManipulator) method being called is as follows:

Public Sub ManipulateShape(myShape As Shape)

Specific Problem: When I try running this code, I get a Run-time error '13': Type mismatch triggered by the line Set shpRange = node.Shapes. Actually, I originally tried forgoing assignment of node.Shapes to a temporary variable and using the method call manipulator.ManipulateShape node.Shapes(1) instead, but then that method call produced the same Type mismatch error. I've also tried using a For loop with counter variables instead of For Each loop only to get the same error message. What is going on? When I debug, the Locals window shows the right types that match my declarations, so I am at a loss.

Upvotes: 1

Views: 462

Answers (1)

Domenic
Domenic

Reputation: 8104

I tested your code and I can confirm that Set shpRange = node.Shapes causes a type mismatch error.

I noticed, though, if shpRange is declared as a generic object instead (ie Dim shpRange as Object), it gets resolved to a ShapeRange, and there's no error.

In any case, you can avoid the assignment as follows...

Public Sub IterateOverShapesInSmartArt(mySmartArt As SmartArt, manipulator As ShapeManipulator)
    Dim node As SmartArtNode
    For Each node In mySmartArt.AllNodes
        With node
            If .Shapes.Count > 0 Then
                manipulator.ManipulateShape .Shapes.Item(1)
            End If
        End With
    Next node
End Sub

Upvotes: 1

Related Questions