Modifying a Portion of a Document

Microsoft Word Visual Basic

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.

Sub CopyWord()
    Selection.Words(1).Copy
End Sub

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.

Sub CopyParagraph()
    ActiveDocument.Paragraphs(1).Range.Copy
End Sub

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.

Sub ChangeCase()
    ActiveDocument.Words(1).Case = wdUpperCase
End Sub

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

Sub ChangeSectionMargin()
    Selection.Sections(1).PageSetup.BottomMargin = InchesToPoints(0.5)
End Sub

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

Sub DoubleSpaceDocument()
    ActiveDocument.Content.ParagraphFormat.Space2
End Sub

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.

Sub SetRangeForFirstTenCharacters()
    Dim rngTenCharacters As Range
    Set rngTenCharacters = ActiveDocument.Range(Start:=0, End:=10)
End Sub

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.

Sub SetRangeForFirstThreeWords()
    Dim docActive As Document
    Dim rngThreeWords As Range
    Set docActive = ActiveDocument
    Set rngThreeWords = docActive.Range(Start:=docActive.Words(1).Start, _
        End:=docActive.Words(3).End)
End Sub

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

Sub SetParagraphRange()
    Dim docActive As Document
    Dim rngParagraphs As Range
    Set docActive = ActiveDocument
    Set rngParagraphs = docActive.Range(Start:=docActive.Paragraphs(2).Range.Start, _
        End:=docActive.Paragraphs(3).Range.End)
End Sub

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