Share this Page URL

The Word Object Model > The Range Object - Pg. 432

ErrorHandler: MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description Resume ErrorHandlerExit End Sub The Selection Object When you record a macro, and open it for editing, you will probably find that the code makes extensive use of the Selection object, which represents the text currently selected in the Word document (or the position of the cursor, if no text is selected). While you can work with paragraphs, sentences, words, and characters via their respective collections under the Document object, it is more common to work with these collections under the Selection object. The procedure in Example 18-37 selects the entire document and converts field codes into text, then places the cursor at the top of the document, using the Whole Story method of the Selection object (WholeStory means the entire document). Example 18-37. Working with Selections Sub CodeToText( ) Selection.WholeStory Selection.Fields.Unlink Selection.HomeKey Unit:=wdStory