Words Collection Object

Multiple objects
Multiple objects

A collection of words in a selection, range, or document. Each item in the Words collection is a Range object that represents one word. There is no Word object.

Using the Words Collection

Use the Words property to return the Words object. The following example displays how many words are currently selected.

MsgBox Selection.Words.Count & " words are selected"

Use Words(index), where index is the index number, to return a Range object that represents one word. The index number represents the position of the word in the Words collection. The following example formats the first word in the selection as 24-point italic.

With Selection.Words(1)
    .Italic = True
    .Font.Size = 24
End With

The item in the Words collection includes both the word and the spaces after the word. To remove the trailing spaces, use Visual Basic's RTrim function — for example, RTrim(ActiveDocument.Words(1)). The following example selects the first word (and its trailing spaces) in the active document.



If the selection is the insertion point and it is immediately followed by a space, Selection.Words(1) refers to the word preceding the selection. If the selection is the insertion point and is immediately followed by a character, Selection.Words(1) refers to the word following the selection.

The Count property for this collection in a document returns the number of items in the main story only. To count items in other stories use the collection with the Range object. Also, the Count property includes punctuation and paragraph marks in the total. If you need a count of the the actual words in a document, use the Word Count dialog box. The following example retrieves the number of words in the active document and assigns the value to the variable numWords.

Set temp = Dialogs(wdDialogToolsWordCount)
' Execute the dialog box in order to refresh its data.
numWords = temp.Words

For more information about calling built-in dialog boxes, see Displaying built-in Word dialog boxes .

The Add method isn't available for the Words collection. Instead, use the InsertAfter method or the InsertBefore method to add text to a Range object. The following example inserts text after the first word in the active document.

ActiveDocument.Range.Words(1).InsertAfter "New text "

Properties | Application Property | Count Property | Creator Property | First Property | Last Property | Parent Property

Methods | Item Method

Parent Objects | Document | Range | Selection

Child Objects | Bookmarks | Borders | Cells | Characters | Columns | Comments | Document | Editors | EndnoteOptions | Endnotes | Fields | Find | Font | FootnoteOptions | Footnotes | FormFields | Frames | HTMLDivisions | Hyperlinks | InlineShapes | ListFormat | ListParagraphs | PageSetup | ParagraphFormat | Paragraphs | ProofreadingErrors | Range | ReadabilityStatistics | Revisions | Rows | Sections | Sentences | Shading | ShapeRange | SmartTags | Subdocuments | SynonymInfo | Tables | TextRetrievalMode | Words | XMLNode | XMLNodes

See Also | Characters Collection Object | Sentences Collection Object