VBA - Selection Object
The Selection object represents the current selection in a window or pane.
The current selection is either the area that is currently highlighted or if nothing is highlighted then it represents the insertion point.
There can only be one Selection object per window (or pane) and there is only ever one selection that is currently active.
Creating a Selection Object
You can use the Pane object from the active Window object to return the current selection at any time.
The Selection object can only be returned from either an Application, Pane and Window object.
Dim objSelection As Selection
Set objSelection = Application.ActiveWindow.ActivePane.Selection
Set objSelection = Application.ActiveWindow.Selection
Set objSelection = Application.Selection
Set objSelection = ActiveWindow.Selection
Set objSelection = ActiveWindow.Panes(1).Selection
Set objSelection = ActivePane.Selection
Set objSelection = Selection
Set objSelection = ActiveDocument.ActiveWindow.Selection
Important Differences
The Range and Selection objects refer to a continuous area within a document.
The Range and Selection objects are very similar but have some important differences:
1) There can only be one Selection object per window (or pane), but there can be many range objects
2) The active selection is always highlighted, whereas the range object is not visible unless selected.
The Selection object shares a lot of the same properties and methods as the Range object.
Redefining a Selection Object
You can use the SetRange method to redefine an existing Selection object
The following example defines a selection object to the be equal to the current selection and then redefines it to refer to the current selection plus the next 10 characters.
Dim objSelection As Selection
Set objSelection = ActiveWindow.Selection
objSelection.SetRange Start:=objRange.Start, _
End:=objRange.End + 10
Using the Selection Object
You should always check the Type of the Selection object to make sure that it is sensible.
It is possible for the user to select a vertical block of text that does not have to be represent contiguous text. This can be done by holding down the alt key and dragging with the mouse.
If (Selection.Type <> wdSelectionType.wdSelectionNormal) Then
Call MsgBox("This is not a valid selection")
End If
Default Property (Text)
The Text property is the default property for a Selection object.
ActiveWindow.Selection.Text
ActiveWindow.Selection
Identifying a Selection Object
You can use the Type property to help return information about the current selection
ActiveWindow.Selection.Type = wdSelectionType.wdSelectionNormal
You can use the Flags property to
You can use the Information property to
Selection.Paste
Selection.Paste
Selection.TypeText
Inserts the text at the beginning of the current selection.
The selection is turned into an insertion point at the end of the inserted text.
If Options.ReplaceSelection = True then the original selection will be replaced.
Selection.TypeText "some text"
This behaves exactly the same as typing some text at the keyboard.
Selection.TypeParagraph
Insert a paragraph mark at the beginning of the current selection.
The selection is turned into an insertion point after the inserted paragraph mark.
If Options.ReplaceSelection = True then the original selection will be replaced.
Selection.TypeParagraph
This behaves exactly the same as pressing the Enter key.
Selection.TypeBackspace
Insert a paragraph mark at the beginning of the current selection.
If the selection is an insertion point, delete the character in front of the insertion point.
If something is currently selected then the selection is turned into an insertion point at the beginning of the current selection.
If Options.ReplaceSelection = True then the original selection will be replaced.
Selection.TypeBackspace
Selection.PasteAndFormat
Selection.PasteAndFormat(wdRecoveryType.wdPasteDefault)
This method pastes the selected table cells and formats them as specified
Pastes the table cells and formats them as specified
Selection.PasteAndFormat(wdRecoveryType.
It doesn't matter what the Zoom percentage is, the image is automatically resized to fit the width of the page
This line will paste an Excel chart as a picture
Selection.PasteAndFormat(wdRecovery.wdChartPicture)
Selection.PasteExcelTable
Selection.PasteFormat
This example inserts the Clipboard contents at the insertion point as unformatted text.
Selection.Collapse Direction:=wdCollapseDirection.wdCollapseStart
This example copies the first paragraph in the document and pastes it at the insertion point.
ActiveDocument.Paragraphs(1).Range.Copy
Selection.Collapse Direction:=wdCollapseDirection.wdCollapseStart
Selection.Paste
This example copies the selection and pastes it at the end of the document.
If Selection.Type <> wdSelectionType.wdSelectionIP Then
Selection.Copy
Set Range = ActiveDocument.Content
Range.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
Range.Paste
End If
This example copies the selected text and pastes it into a new document as a hyperlink. The source document must first be saved for this example to work.
If Selection.Type = wdSelectionType.wdSelectionNormal Then
Selection.Copy
Documents.Add.Content.PasteSpecial Link:=True, _
DataType:=wdPasteDataType.wdPasteHyperlink
End If
Selection.Extend
Turns extend mode on (sets the ExtendMode property to True), or if extend mode is already on, extends the Selection object to the next larger unit of text.
The progression of selected units of text is as follows: word, sentence, paragraph, section, entire document.
objSelection.Extend(Character:="A")
Character - The character through which the selection is extended. This argument is case sensitive and must evaluate to a String or an error occurs. Also, if the value of this argument is longer than a single character, the command is ignored.
This example collapses the current selection to an insertion point and then selects the current sentence.
With Selection
' Collapse current selection to insertion point.
.Collapse
' Turn extend mode on.
.Extend
' Extend selection to word.
.Extend
' Extend selection to sentence.
.Extend
End With
Here is an example that accomplishes the same task without the Extend method.
With Selection
' Collapse current selection.
.Collapse
' Expand selection to current sentence.
.Expand Unit:=wdUnits.wdSentence
End With
This example makes the end of the selection active and extends the selection through the next instance of a capital "R".
With Selection
.StartIsActive = False
.Extend Character:="R"
End With
Selection - ExtendMode
This property is True when the Extend mode is active.
This property can only be set during run time; attempts to set it in Immediate mode are ignored. The Extend arguments of the EndOf and StartOf methods are not affected by this property.
This example moves to the beginning of the paragraph and selects the paragraph plus the next two sentences.
With Selection
.MoveLeft Unit:=wdUnits.wdCharacter, Count:=4, Extend:=True
.MoveRight Unit:=wdUnits.wdSentence, Count:=2
.ExtendMode = True
.MoveUp Unit:=wdUnits.wdParagraph
.MoveDown Unit:=wdUnits.wdParagraph
End With
This example collapses the current selection, turns on Extend mode, and selects the current sentence.
With Selection
.Collapse
.ExtendMode = True
' Select current word.
.Extend
' Select current sentence.
.Extend
End With
Expanding the current Selection
The easiest way to expand a range is to use either the MoveRight or MoveLeft methods.
The Extend argument can either be wdMove or wdExtend. The default is wdMove.
Selection.MoveRight Unit:=wdUnits.wdCharacter, _
Count:=1, _
Extend:=wdMovementType.wdExtend
This method cannot be used with a Range.
You can also use the Expand method. This method returns the number of characters added to the range.
Selection.Expand (Unit:=wdUnits.wdWord)
This example expands the selection to include the entire sentence.
Selection.Expand Unit:=wdUnits.wdSentence
objSelection.MoveStart wdUnits.wdCharacter, 3
If objSelection.Active = True Then
'this selection is currently active, ie highlighted
End If
Selection Object - Collapsing
Even when a selection is collapsed to an insertion point it is not empty.
The Text property of a collapsed Selection object will still return the character immediately to the right of the insertion point.
This character will also appear in the Characters collection of the Selection object.
Selection.HomeKey
Moves the Selection object to the beginning of the specified unit.
lNoOfChars = objSelection.HomeKey(Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove)
Unit -
Extend - The default is wdMove.
This method returns a value (Long) that indicates the number of characters the selection has actually moved.
If it returns zero (0) then the moved was unsuccessful.
Selection.EndKey
Moves the Selection object to the end of the specified unit.
lNoOfChars = objSelection.EndKey(Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove)
Unit -
Extend - The default is wdMove.
This method returns a value (Long) that indicates the number of characters the selection has actually moved.
If it returns zero (0) then the moved was unsuccessful.
Selection.MoveUp
Moves the Selection object up and returns the number of units it has moved.
The current selection is collapsed to the end point before being moved up.
The wdWindow constant can also be used to move to the top of the current screen.
The wdScreen constant can also be used to move more than one screen.
lNoOfChars = objSelection.MoveUp(Unit:=wdUnits.wdParagraph, _
Count:=1, _
Extend:=wdMovementType.wdMove)
Unit - The unit can be either wdLine, wdParagraph, wdWindow, wdScreen. The default value is wdLine.
Count - The number of units to move. The default is 1.
Extend - The default is wdMove.
Selection.MoveDown
Moves the Selection object down and returns the number of units it has moved.
The current selection is collapsed to the end point before being moved up.
The wdWindow constant can also be used to move to the top of the current screen.
The wdScreen constant can also be used to move more than one screen.
lNoOfChars = objSelection.MoveDown(Unit:=wdUnits.wdParagraph, _
Count:=1, _
Extend:=wdMovementType.wdMove)
Unit - The unit can be either wdLine, wdParagraph, wdWindow, wdScreen. The default value is wdLine.
Count - The number of units to move. The default is 1.
Extend - The default is wdMove.
Selection.MoveLeft
Moves the Selection object to the left and returns the number of units it has moved.
The current Selection object is collapsed to the end point before being moved left.
lNoOfChars = objSelection.MoveLeft(Unit:=wdUnits.wdCharacters, _
Count:=1, _
Extend:=wdMovementType.wdMove)
Selection.MoveRight
Moves the Selection object to the right and returns the number of units it has moved.
The current Selection object is collapsed to the end point before being moved right.
lNoOfChars = objSelection.MoveRight(Unit:=wdUnits.wdCharacters, _
Count:=1, _
Extend:=wdMovementType.wdMove)
Selection.
The following methods also exist for a Selection:
Move, MoveStart, MoveEnd, MoveUntil, MoveWhile, MoveStartUntil, MoveEndUntil, MoveStartWhile, MoveEndWhile
Selection.Move
Selection.MoveUntil
Selection.MoveWhile
Selection.MoveStart
Selection.MoveStartUntil
Selection.MoveStartWhile
Selection.MoveEnd
Selection.MoveEndUntil
Selection.MoveEndWhile
The following moves to the start of the current line.
objSelection.HomeKey(Unit:=wdUnits.wdLine, _
Extend:=wdMovementType.wdMove)
The following moves to the end of the line
objSelection.EndKey(Unit:=wdUnits.wdLine, _
Extend:=wdMovementType.wdMove)
This following moves the selection up three lines.
objSelection.MoveUp(Unit:=wdUnits.wdLine, _
Count:=3, _
Extend:=wdMovementType.wdMove)
Selection.Paragraphs(1).Range.Characters(1).Select
Selection.Collapse wdCollapseStart
The following moves the selection down two paragraphs.
objSelection.MoveDown(Unit:=wdUnits.wdParagraph, _
Count:=2, _
Extend:=wdMovementType.wdMove)
The following moves to the end of the document.
objSelection.EndKey(Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove)
The following example moves to the start of the document.
objSelection.HomeKey(Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove)
The following moves the Selection object two words to the left.
objSelection.MoveLeft(Unit:=wdUnits.wdWords, _
Count:=2, _
Extend:=wdMovementType.wdMove)
The following moves the start position backwards until a capital "I" is found.
When the movement is backwards, the Range is expanded.
Selection.MoveStartUntil Cset:="I", _
Count:=wdConstants.wdBackward
The following moves the start position forwards until a "%" characters is found or for a maximum of ten 10.
When the movement is forwards, the Range is reduced.
Selection.MoveStartUntil Cset:="%", _
Count:=10
Backspace
Selection.TypeBackspace
Selection.Delete Unit:=wdUnits.wdCharacter, Count:=1
selection.startof(wdline)
selection.endof(wdline)
This example inserts the text "Solutions" (enclosed in quotation marks) before the selection and then collapses the selection.
With Selection
.InsertBefore Chr(34) & "Solutions" & Chr(34) & Chr(32)
.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
End With
This example inserts all the font names in the FontNames collection into a new document.
Documents.Add
For Each aFont In FontNames
With Selection
.InsertBefore aFont
.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
.TypeParagraph
End With
Next aFont
This example inserts text at the end of the selection and then collapses the selection to an insertion point.
With Selection
.InsertAfter "appended text"
.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
End With
Deleting text
Selection.TypeBackspace
Selection.Delete Unit:=wdUnits.wdCharacter, Count:=1
Removes all character formatting (formatting applied either through character styles or manually applied formatting) from the selected text.
Selection.ClearCharacterAllFormatting
Removes character formatting (formatting that has been applied manually using the buttons on the Ribbon or through the dialog boxes) from the selected text.
Selection.ClearCharacterDirectFormatting
Removes character formatting that has been applied through character styles from the selected text.
Selection.ClearCharacterStyle
Removes text and paragraph formatting from a selection.
Selection.ClearFormatting
Removes all paragraph formatting (formatting applied either through paragraph styles or manually applied formatting) from the selected text.
Selection.ClearParagraphAllFormatting
Removes paragraph formatting that has been applied manually (using the buttons on the Ribbon or through the dialog boxes) from the selected text.
Selection.ClearParagraphDirectFormatting
Removes paragraph formatting that has been applied through paragraph styles from the selected text.
Selection.ClearParagraphStyle
Selection.Paragraph.Reset
Selection.Paragraphs.Reset
Selection.ParagraphFormat.Reset
Selection.Font.Reset
Selection Object - Properties
| BookmarkID | Returns the number of the bookmark that encloses the beginning of the specified selection or range; returns 0 (zero) if there's no corresponding bookmark. (Long) |
| Bookmarks | Returns a Bookmarks collection that represents all the bookmarks in the range Read-only. |
| Borders | Returns a Borders collection that represents all the borders for the specified object. |
| Case | Returns or sets a WdCharacterCase constant that represents the case of the text in the range. |
| Characters | Returns a Characters collection that represents the characters in the range. |
| CombinedCharacters | Returns True or False indicating if the range contains combined characters. |
| Comments | Returns a Comments collection that represents all the comments in the range. |
| Document | Returns a Document object associated with the specified pane, window, or selection. |
| Duplicate | Returns a duplicate range object representing all the properties from this range. |
| Editors | Returns an Editors object that represents all the users authorized to modify this range. |
| End | Returns or sets the ending character position of the range. Read Only (Long) |
| EndnoteOptions | Returns an EndnoteOptions object that represents the endnotes in the range. |
| EndNotes | Returns an Endnotes collection that represents all the endnotes in the range. Read-only. |
| ExtendMode | |
| Fields | Returns Fields collection that represents all the fields in the range. Read Only |
| Find | Returns a Find object that contains the criteria for a find operation. Read-only |
| FitTextWidth | Returns or sets the width (in the current measurement units) in which Microsoft Word fits the text in the current range. (Single). |
| Flags | |
| Font | Returns or sets a Font object that represents the character formatting of the range. |
| FootnoteOptions | Returns FootnoteOptions object that represents the footnotes in the range. |
| Footnotes | Returns a Footnotes collection that represents all the footnotes in the range. Read-only. |
| FormattedText | Returns or sets a Range object that includes the formatted text in the range. |
| FormFields | Returns a FormFields collection that represents all the form fields in the range. Read-only. |
| Frames | Returns a Frames collection that represents all the frames in the range. Read-only |
| GrammarChecked | Returns True or False indicating if a grammar check has been run on the range. False is returned if some of the specified range or document hasn't been checked for grammar. |
| GrammaticalErrors | Returns a ProofreadingErrors collection that represents the sentences that failed the grammar check on the range. There can be more than one error per sentence. Read-only. |
| HasChildShapeRange | |
| HeaderFooter | |
| HighlightColorIndex | Returns or sets the highlight color for the range. (WdColorIndex). |
| HorizontalInVertical | Returns or sets the formatting for horizontal text set within vertical text. (wdHorizontalInVerticalType). |
| HTMLDivisions | Returns an HTMLDivisions object that represents an HTML division in a Web document. |
| Hyperlinks | Returns a Hyperlinks collection that represents all the hyperlinks in the range. Read-only. |
| ID | Can be used to define hyperlinks in a document ?? |
| Information | Returns information about the specified selection or range. Read-only Variant. |
| InlineShapes | Returns an InlineShapes collection that represents all the InlineShape objects in the range. Read-only. |
| IsEndOfRowMark | Returns True or False indicating if the range is collapsed and is located at the end-of-row mark in a table. Read-only. |
| LanguageID | Returns or sets the language for the range. |
| ListFormat | Returns a ListFormat object that represents all the list formatting characteristics of the range. Read-only. |
| ListParagraphs | Returns a ListParagraphs collection that represents all the numbered paragraphs in the range. Read-only. |
| NextStroyRange | Returns a Range object that refers to the next story, as shown in the following table. Parameter is from wdStoryType |
| NoProofing | True if the spelling and grammar checker ignores the specified text. Returns wdUndefined if the NoProofing property is set to True for only some of the specified text. (Long) |
| Orientation | Returns or sets the orientation of text in a range when the Text Direction feature is enabled. WdTextOrientation. |
| PageSetup | Returns a PageSetup object that's associated with the specified range. Read-only. |
| ParagraphFormat | Returns or sets a ParagraphFormat object that represents the paragraph settings for the range |
| Paragraphs | Returns a Paragraphs collection that represents all the paragraphs in the range. Read-only. |
| PreviousBookmarkID | Returns the number of the last bookmark that starts before or at the same place as the specified selection or range; returns 0 (zero) if there's no corresponding bookmark. Read-only Long. |
| Range | |
| ReadabilityStatistics | Returns a ReadabilityStatistics collection that represents the readability statistics for the range. Read-only. |
| Revisions | Returns a Revisions collection that represents the tracked changes in the range. Read-only. |
| Scripts | Returns a Scripts collection that represents the collection of HTML scripts in the specified object. |
| Sections | Returns a Sections collection that represents the sections in the range. Read-only. |
| Sentences | Returns a Sentences collection that represents all the sentences in the range. Read-only. |
| Shading | Returns a Shading object that refers to the shading formatting for the specified object. |
| ShapeRange | Returns a ShapeRange collection that represents all the Shape objects in the specified range or selection. The shape range can contain drawings, shapes, pictures, OLE objects, ActiveX controls, text objects, and callouts. Read-only. |
| ShowAll | True if all nonprinting characters (such as hidden text, tab marks, space marks, and paragraph marks) are displayed. Read/write Boolean. |
| SmartTags | Returns a SmartTags object that represents a smart tag in a document |
| SpellingChecked | True if spelling has been checked throughout the specified range or document. False if all or some of the range or document hasn't been checked for spelling. Boolean |
| SpellingErrors | Returns a ProofreadingErrors collection that represents the words identified as spelling errors in the specified document or range. Read-only. |
| Start | Returns or sets the starting character position of a selection, range, or bookmark. Long. |
| StartIsActive | |
| StoryLength | Returns the number of characters in the story that contains the specified range or selection. Read-only (Long) |
| StoryType | Returns the story type for the specified range, selection, or bookmark. Read-only WdStoryType. |
| Style | Returns or sets the style for the specified object. To set this property, specify the local name of the style, an integer, a WdBuiltinStyle constant, or an object that represents the style. For a list of valid constants, consult the Microsoft Visual Basic Object Browser. Read/write Variant. |
| Subdocuments | Returns a Subdocuments collection that represents all the subdocuments in the specified range or document. Read-only. |
| SynonymInfo | Returns a SynonymInfo object that contains information from the thesaurus on synonyms, antonyms, or related words and expressions for the specified word or phrase |
| Text | Returns or sets the text in the range. (String). |
| TextRetrievalMode | Returns a TextRetrievalMode object that controls how text is retrieved from the specified Range. Read/write. |
| TwoLinesInOne | Returns or sets whether Microsoft Word sets two lines of text in one and specifies the characters that enclose the text, if any. Read/write WdTwoLinesInOneType. |
| Type | |
| Words | Returns a Words collection that represents all the words in a range, selection, or document. Read-only. Note Punctuation and paragraph marks in a document are included in the Words collection. |
| XML | Returns a String that represents the XML text in the specified object. |
| XMLNodes | Returns an XMLNodes collection that represents the collection of all XML elements within a document or in a selection or range - including those elements that are only partially within the selection or range. |
| XMLParentNode | Returns an XMLNode object that represents the parent node of a range. |
Selection Object - Methods
| BoldRun | Returns True, False or wdUndefined (a mixture of True and False). Can be set to True, False, or wdToggle. |
| ItalicRun | Returns True or False or wdUndefined indicating if the range is formatted in italic. (Long) |
| Move | |
| MoveDown | |
| MoveEnd | |
| MoveEndUntil | |
| MoveEndWhile | |
| MoveLeft | |
| MoveRight | |
| MoveStart | |
| MoveStartUntil | |
| MoveStartWhile | |
| MoveUntil | |
| MoveUp | |
| MoveWhile | |
| Next | |
| Previous | |
| UnderlineRun | Returns or sets the type of underline applied to the font or range. Read/write WdUnderline |
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext