Info

Selection Object

The Selection object represents what is selected in the document—that is, a word, sentence, or the insertion point. It has a Type property that returns the type of what is selected (wdSelectionIP, wdSelectionColumn, wdSelectionShape, and so on).

HomeKey/EndKey

The HomeKey and EndKey methods are used to change the selection; they correspond to using the Home and End keys, respectively, on the keyboard. They have two parameters: Unit and Extend. Unit is the range of movement to make, either to the beginning (Home) or end (End) of a line (wdLine), document (wdStory), column (wdColumn), or row (wdRow). Extend is the type of movement: wdMove moves the selection, wdExtend extends the selection from the original insertion point to the new insertion point.

To move the cursor to the beginning of the document, use this code: wdApp.Selection.HomeKey Unit:=wdStory, Extend:=wdMove

To select the document from the insertion point to the end of the document, use this code: .wdApp.Selection.EndKey Unit:=wdStory, Extend:=wdExtend

TypeText

The TypeText method is used to insert text into a Word document. User settings, such as the Overtype setting, can affect what will happen when text is inserted into the document:

Sub InsertText()

Dim wdApp As Word.Application

Dim wdDoc As Document

Dim wdSln As Selection

Set wdApp = GetObject(, "Word.Application") Set wdDoc = wdApp.ActiveDocument Set wdSln = wdApp.Selection wdDoc.Application.Options.Overtype = False With wdSln

If .Type = wdSelectionIP Then

.TypeText ("Inserting at insertion point. ") Elself .Type = wdSelectionNormal Then

If wdApp.Options.ReplaceSelection Then

.Collapse Direction:=wdCollapseStart End If

.TypeText ("Inserting before a text block. ")

End If End With

Set wdApp = Nothing Set wdDoc = Nothing End Sub

Range Object

The Range object represents a contiguous area, or areas, in the document. It has a starting character position and an ending character position. The object can be the insertion point, a range of text, or the entire document, including non-printing characters (such as spaces or paragraph marks).

The Range object is similar to the Selection object but, in some ways, is better: It requires less code to accomplish the same tasks; it has more capabilities; and it saves time and memory because the Range object doesn't require Word to move the cursor or highlight objects in the document to manipulate them.

Define a Range

To define a range, enter a starting and ending position, as shown in this code segment:

Range(StartPosition, EndPosition)

Sub RangeText()

Dim wdApp As Word.Application

Dim wdDoc As Document

Dim wdRng As Word.Range

Set wdApp = GetObject(, "Word.Application") Set wdDoc = wdApp.ActiveDocument

Set wdRng = wdDoc.Range(0, 22) wdRng.Select

Set wdApp = Nothing Set wdDoc = Nothing Set wdRng = Nothing End Sub

Figure 16.4 shows the results of running this code. The first 22 characters, including nonprinting characters such as paragraph returns, are selected.

0 0

Post a comment