Shapes.Range Method (PowerPoint)

Returns a ShapeRange object that represents a subset of the shapes in a Shapes collection.

Syntax

expression. Range( _Index_ )

expression A variable that represents a Shapes object.

Parameters

Name Required/Optional Data Type Description
Index Optional Variant The individual shapes that are to be included in the range. Can be an Integer that specifies the index number of the shape, a String that specifies the name of the shape, or an array that contains either integers or strings. If this argument is omitted, the Range method returns all the objects in the specified collection.

Return Value

ShapeRange

Remarks

Although you can use the Range method to return any number of shapes or slides, it is simpler to use the Item method if you only want to return a single member of the collection. For example, Shapes(1) is simpler than Shapes.Range(1), and Slides(2) is simpler than Slides.Range(2).

To specify an array of integers or strings for Index, you can use the Array function. For example, the following instruction returns two shapes specified by name.

Dim myArray() As Variant, myRange As Object myArray = Array("Oval 4", "Rectangle 5") Set myRange = ActivePresentation.Slides(1).Shapes.Range(myArray)

Example

This example sets the fill pattern for shapes one and three on myDocument.

Set myDocument = ActivePresentation.Slides(1)

myDocument.Shapes.Range(Array(1, 3)).Fill _
    .Patterned msoPatternHorizontalBrick

This example sets the fill pattern for the shapes named Oval 4 and Rectangle 5 on the first slide.

Dim myArray() As Variant, myRange As Object

myArray = Array("Oval 4", "Rectangle 5")

Set myRange = ActivePresentation.Slides(1).Shapes.Range(myArray)

myRange.Fill.Patterned msoPatternHorizontalBrick

This example sets the fill pattern for all shapes on the first slide.

ActivePresentation.Slides(1).Shapes.Range.Fill _
    .Patterned Pattern:=msoPatternHorizontalBrick

This example sets the fill pattern for shape one on the first slide.

Set myDocument = ActivePresentation.Slides(1)

Set myRange = myDocument.Shapes.Range(1)

myRange.Fill.Patterned msoPatternHorizontalBrick

This example creates an array that contains all the AutoShapes on the first slide, uses that array to define a shape range, and then distributes all the shapes in that range horizontally.

With myDocument.Shapes

    numShapes = .Count



    'Continues if there are shapes on the slide

    If numShapes > 1 Then

        numAutoShapes = 0

        ReDim autoShpArray(1 To numShapes)

        For i = 1 To numShapes



            'Counts the number of AutoShapes on the Slide

            If .Item(i).Type = msoAutoShape Then

                numAutoShapes = numAutoShapes + 1

                autoShpArray(numAutoShapes) = .Item(i).Name

            End If

        Next



        'Adds AutoShapes to ShapeRange

        If numAutoShapes > 1 Then

            ReDim Preserve autoShpArray(1 To numAutoShapes)

            Set asRange = .Range(autoShpArray)

            asRange.Distribute msoDistributeHorizontally, False

        End If

    End If

End With


See also

Shapes Object