Range Method
Range method as it applies to the Document object.
Range method as it applies to the CanvasShapes, GroupShapes, and Shapes objects.
Returns a ShapeRange object.
expression.Range(Index)
expression Required. An expression that returns one of the above objects.
Index Required Variant. Specifies which shapes are to be included in the specified range. Can be an integer that specifies the index number of a shape within the Shapes collection, a string that specifies the name of a shape, or a Variant array that contains integers or strings.
Remarks
Character position values begin with 0 (zero) at the beginning of the document. All characters are counted, including nonprinting characters. Hidden characters are counted even if they're not displayed. If you don't specify starting and ending character positions for the Range method, the entire document is returned as a Range object.
ShapeRange objects don't include InlineShape objects. An InlineShape object is equivalent to a character and is positioned as a character within a range of text. Shape objects are anchored to a range of text (the selection, by default), but they can be positioned anywhere on the page. A Shape object will always appear on the same page as the range it's anchored to.
Most operations that you can do with a Shape object you can also do with a ShapeRange object that contains a single shape. Some operations, when performed on a ShapeRange object that contains multiple shapes, produce an error.
Example
As it applies to the Document object.
This example applies bold formatting to the first 10 characters in the active document.
Sub DocumentRange()
ActiveDocument.Range(Start:=0, End:=10).Bold = True
End Sub
This example creates a range that starts at the beginning of the active document and ends at the cursor position, and then it changes all characters within that range to uppercase.
Sub DocumentRange2()
Dim r As Range
Set r = ActiveDocument.Range(Start:=0, End:=Selection.End)
r.Case = wdUpperCase
End Sub
This example creates and sets the variable myRange
to paragraphs three through six in the active document, and then it right-aligns the paragraphs in the range.
Sub DocumentRange3()
Dim aDoc As Document
Dim myRange As Range
Set aDoc = ActiveDocument
If aDoc.Paragraphs.Count >= 6 Then
Set myRange = aDoc.Range(aDoc.Paragraphs(2).Range.Start, _
aDoc.Paragraphs(4).Range.End)
myRange.Paragraphs.Alignment = wdAlignParagraphRight
End If
End Sub
As it applies to the CanvasShapes, GroupShapes, and Shapes objects.
This example sets the fill foreground color to purple for the first shape in the active document.
Sub ShRange()
With ActiveDocument.Shapes.Range(1).Fill
.ForeColor.RGB = RGB(255, 0, 255)
.Visible = msoTrue
End With
End Sub
This example applies a shadow to a variable shape in the active document.
Sub ShpRange2(strShpName As String)
ActiveDocument.Shapes.Range(strShpName).Shadow.Type = msoShadow6
End Sub
To call the preceding subroutine, enter the following code into a standard code module.
Sub CallShpRange2()
Dim shpArrow As Shape
Dim strName As String
Set shpArrow = ActiveDocument.Shapes.AddShape(Type:=msoShapeLeftArrow, _
Left:=200, Top:=400, Width:=50, Height:=75)
shpArrow.Name = "myShape"
strName = shpArrow.Name
ShpRange2 strShpName:=strName
End Sub
This example selects shapes one and three in the active document.
Sub SelectShapeRange()
ActiveDocument.Shapes.Range(Array(1, 3)).Select
End Sub
This example selects and deletes the shapes in the first shape in the active document. This example assumes that the first shape is a canvas shape.
Sub CanvasShapeRange()
Dim rngCanvasShapes As Range
Set rngCanvasShapes = ActiveDocument.Shapes(1).CanvasItems.Range(1)
rngCanvasShapes.Select
rngCanvasShapes.Delete
End Sub