The selection object refers to the currently selected text, that is the text that is highlighted. If no selection is current, the selection object still functions and works with the current cursor position.
The selection object is an application level object so can be used without any qualifiers (like ActiveDocument).
Here are some of the more important properties and methods:
Properties |
|
Document |
Read only; the document to which the selection object is associated. |
End |
Set or read the ending position of selection. |
Start |
Set or read the starting position of selection. |
Font |
Merged font of the selection, represented by Font object |
Paragraphs |
Collection of paragraphs in the selection. |
Methods |
|
Collapse(Direction) |
Turn selection back into a cursor. Possible directions are abForw & abBack. |
Copy() |
Copies it to the Clipboard. |
Cut() |
Moves it to the Clipboard. |
Delete() |
Delete the selection. |
InsertBefore(String) |
Insert text before the selection and adds it to the selection. |
InsertAfter(String) |
Insert text after the selection and adds it to the selection. |
The selection object makes it easy to insert text at the current cursor position. For example:
Selection.InsertAfter("Inserted text")
After text has been inserted it will itself be selected. This makes it easy to adjust the font properties. For example:
Selection.InsertAfter("Inserted text")
With Selection.Font
.Bold = True
.Italic = True
.Underline = True
.Size = 18
.Name = "Times New Roman"
End With
Selection.Collapse(abForw)
The last line returns back to a normal cursor.
If you want to work with a specific part of a document, use the Start and End properties. For example, the following copies the first 10 characters of a document to the clipboard and then tells the user how many characters he has copied:
Selection.Start = 0
Selection.End = 9
Selection.Copy()
MsgBox "You copied " & _
Selection.End - Selection.Start + 1 & _
" characters"
Or if you want to insert text at the end of a document, use the selection object in conjunction with the text object:
Selection.End = ActiveDocument.Text.Count
Selection.InsertAfter "End of document"
Selection.Collapse(abForw)