Modifying a portion of a document

Visual Basic includes objects which you can use to modify the following document elements: characters, words, sentences, paragraphs and sections. The following table includes the properties that correspond to these document elements and the objects they return.

This expression Returns this object
Words(index) Range
Characters(index) Range
Sentences(index) Range
Paragraphs(index) Paragraph
Sections(index) Section

When these properties are used without an index, a collection object with the same name is returned. For example, the Paragraphs property returns the Paragraphs collection object. However, if you identify an item within these collections by index, the object in the second column of the table is returned. For example, Words(1) returns a Range object. After you have a Range object, you can use any of the range properties or methods to modify the Range object. For example, the following instruction copies the first word in the selection to the Clipboard.

Selection.Words(1).Copy

Note   The items in the Paragraphs and Sections collections are singular forms of the collection rather than Range objects. However, the Range property (which returns a Range object) is available from both the Paragraph and Section objects. For example, the following instruction copies the first paragraph in the active document to the Clipboard.

ActiveDocument.Paragraphs(1).Range.Copy

All of the document element properties in the preceding table are available from the Document, Selection, and Range objects. The following examples demonstrate how you can drill down to these properties from Document, Selection, and Range objects.

The following example sets the case of the first word in the active document.

ActiveDocument.Words(1).Case = wdUpperCase

The following example sets the bottom margin of the current section to 0.5 inch.

Selection.Sections(1).PageSetup.BottomMargin = InchesToPoints(0.5)

The following example double spaces the text in the active document (the Content property returns a Range object).

ActiveDocument.Content.ParagraphFormat.Space2

Modifying a group of document elements

To modify a range of text that consists of a group of document elements (characters, words, sentences, paragraphs or sections), you need to create a Range object. The Range method creates a Range object given a start and end point. For example, the following instruction creates a Range object that refers to the first ten characters in the active document.

Set myRange = ActiveDocument.Range(Start:=0, End:=10)

Using the Start and End properties with a Range object, you can create a new Range object that refers to a group of document elements. For example, the following instruction creates a Range object (myRange) that refers to the first three words in the active document.

Set Doc = ActiveDocument
Set myRange = Doc.Range(Start:=Doc.Words(1).Start, _
    End:=Doc.Words(3).End)

The following example creates a Range object (aRange) beginning at the start of the second paragraph and ending after the third paragraph.

Set Doc = ActiveDocument
Set myRange = Doc.Range(Start:=Doc.Paragraphs(2).Range.Start, _
    End:=Doc.Paragraphs(3).Range.End)

For more information on defining Range objects, see Working with Range objects.