Search code examples
vbaobjectgroupingrenameword-2007

Run-time error: The ShapeRange Object must contain at least two items


I am trying to create something like my own autoshape and have Word 2007 create it and group the two shapes for me automatically. I have looked all over the internet (google, bing, MSDN Directory, Stackoverflow, Microsoft Website)looking for a solution. I tired to create a macro that would create my object on command, but I am still getting the same error each time.

ActiveDocument.Shapes.AddShape(msoShapeRoundedRectangle, 331.4, 318.45, _
    122.75, 98.8).Select
ActiveDocument.Shapes.AddConnector(msoConnectorStraight, 331.4, 354.45, _
    122.75, 0#).Select
ActiveDocument.Shapes.Range(Array("AutoShape 727", "AutoShape 728")). _
    Select
Selection.ShapeRange.Group.Select

When I run this code I get the following error: Run-time error: The ShapeRange Object must contain at least two items.

I would also like to rename the autoshapes, but I don't know how to do that either.

Thanks for your help.


Solution

  • Try this

    Sub Tester()
    
        Dim s1 As Shape, s2 As Shape, sr As ShapeRange
    
        Set s1 = ActiveDocument.Shapes.AddShape(msoShapeRoundedRectangle, _
                                              331.4, 318.45, 122.75, 98.8)
        s1.Name = "Test1"
    
    
        Set s2 = ActiveDocument.Shapes.AddConnector(msoConnectorStraight, _
                                              331.4, 354.45, 122.75, 0#)
        s2.Name = "Test2"
    
        ActiveDocument.Shapes.Range(Array(s1.Name, s2.Name)).Group.Select
    
    End Sub