Share via


Shapes.Range Method

Publisher Developer Reference

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 Required 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 Index is omitted, the Range method returns all the objects in the specified collection.

Return Value
ShapeRange

Example

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.

Visual Basic for Applications
  Dim arrShapes As Variant
Dim shpRange As ShapeRange

Set arrShapes = Array("Oval 4", "Rectangle 5") Set shpRange = ActiveDocument.Pages(1) _ .Shapes.Range(arrShapes)

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

Visual Basic for Applications
  ActiveDocument.Pages(1).Shapes.Range(Array(1, 3)).Fill _
    .Patterned msoPatternHorizontalBrick
Visual Basic for Applications
  

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

Visual Basic for Applications
  Dim arrShapes As Variant
Dim shpRange As ShapeRange

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

Set shpRange = ActiveDocument.Pages(1).Shapes.Range(arrShapes)

shpRange.Fill.Patterned msoPatternHorizontalBrick

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

Visual Basic for Applications
  ActiveDocument.Pages(1).Shapes _
    .Range.Fill.Patterned msoPatternHorizontalBrick

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

Visual Basic for Applications
  Dim shpRange As ShapeRange

Set shpRange = ActiveDocument.Pages(1).Shapes.Range(1)

shpRange.Fill.Patterned msoPatternHorizontalBrick

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

Visual Basic for Applications
  Dim numShapes As Long
Dim numAutoShapes As Long
Dim autoShpArray As Variant
Dim intLoop As Integer
Dim shpRange As ShapeRange

With ActiveDocument.Pages(1).Shapes

numShapes = .Count
If numShapes > 1 Then

    numAutoShapes = 0
    ReDim autoShpArray(1 To numShapes)

    For intLoop = 1 To numShapes
        If .Item(intLoop).Type = msoAutoShape Then
            numAutoShapes = numAutoShapes + 1
            autoShpArray(numAutoShapes) = .Item(intLoop).Name
        End If
    Next

    If numAutoShapes > 1 Then
        ReDim Preserve autoShpArray(1 To numAutoShapes)
        Set shpRange = .<strong class="bterm">Range</strong>(autoShpArray)
        shpRange.Distribute _
            DistributeCmd:=msoDistributeHorizontally, _
            RelativeTo:=False
    End If

End If

End With

See Also