Class Selection (Word VBA)

The class Selection represents the current selection in a window or pane. A selection represents either a selected (or highlighted) area in the document, or it represents the insertion point if nothing in the document is selected. There can be only one Selection object per document window pane, and only one Selection object in the entire application can be active.

The main procedures of class Selection are Copy, Delete and Select

Set

To use a Selection class variable it first needs to be instantiated, for example

Dim slc as Selection
Set slc = Selection

The following procedures can be used to set variables of type Selection: Application.Selection, Pane.Selection and Window.Selection

Themes

Some procedures in this class have been grouped together in themes and are described on separate theme pages

Format with its procedures Borders, ClearCharacterAllFormatting, ClearCharacterDirectFormatting, ClearCharacterStyle, ClearFormatting, ClearParagraphAllFormatting, ClearParagraphDirectFormatting, ClearParagraphStyle, CopyFormat, FitTextWidth, Font, FormattedText, Information, InsertStyleSeparator, PageSetup, ParagraphFormat, PasteAndFormat, PasteFormat, ReadingModeGrowFont, ReadingModeShrinkFont, SelectCurrentColor, SelectCurrentFont, SelectCurrentSpacing and Style
Sort with its procedures Sort, SortAscending, SortByHeadings and SortDescending

Methods

These are the main methods of the Selection class

Copy - Copies the specified selection to the Clipboard.

Selection.Copy

Delete - Deletes the specified number of characters or words.

Dim lngDelete As Long
lngDelete = Selection.Delete()

Select - Selects the specified text.

Selection.Select

Other Methods

Collapse - Collapses a selection to the starting or ending position. After a selection is collapsed, the starting and ending points are equal.

Selection.Collapse

ConvertToTable - Converts text within a range to a table. Returns the table as a Table object.

Dim tblConvertToTable As Table
Set tblConvertToTable = Selection.ConvertToTable()

EndKey - Moves or extends the selection to the end of the specified unit.

Dim lngEndKey As Long
lngEndKey = Selection.EndKey()

EndOf - Moves or extends the ending character position of a range or selection to the end of the nearest specified text unit.

Dim lngEndOf As Long
lngEndOf = Selection.EndOf()

GoTo - Moves the insertion point to the character position immediately preceding the specified item, and returns a Range object (except for the wdGoToGrammaticalError, wdGoToProofreadingError, or wdGoToSpellingError constant).

Dim rngGoTo As Range
Set rngGoTo = Selection.GoTo()

HomeKey - Moves or extends the selection to the beginning of the specified unit. This method returns an integer that indicates the number of characters the selection was actually moved, or it returns 0 (zero) if the move was unsuccessful.This method corresponds to functionality of the HOME key.

Dim lngHomeKey As Long
lngHomeKey = Selection.HomeKey()

InsertCaption - Inserts a caption immediately preceding or following the specified selection.

Dim strLabel As String: strLabel =  
Selection.InsertCaption Label:=strLabel

InsertCrossReference - Inserts a cross-reference to a heading, bookmark, footnote, or endnote, or to an item for which a caption label is defined (for example, an equation, figure, or table).

Dim strReferenceItem As String: strReferenceItem =  
Selection.InsertCrossReference ReferenceType:=wdRefTypeBookmark, ReferenceKind:=wdContentText, ReferenceItem:=strReferenceItem

InsertDateTime - Inserts the current date or time, or both, either as text or as a TIME field.

Selection.InsertDateTime

InsertFile - Inserts all or part of the specified file.

Dim strFileName As String: strFileName =  
Selection.InsertFile FileName:=strFileName

InsertFormula - Inserts an = (Formula) field that contains a formula at the selection.

Selection.InsertFormula

InsertSymbol - Inserts a symbol in place of the specified selection.

Dim lngCharacterNumber As Long: lngCharacterNumber =  
Selection.InsertSymbol CharacterNumber:=lngCharacterNumber

InsertXML - Inserts the specified XML into the document at the cursor, replacing any selected text.

Dim strXML As String: strXML =  
Selection.InsertXML XML:=strXML

Move - Collapses the specified selection to its start or end position and then moves the collapsed object by the specified number of units. This method returns a Long value that represents the number of units by which the selection was moved, or it returns 0 (zero) if the move was unsuccessful.

Dim lngMove As Long
lngMove = Selection.Move()

MoveDown - Moves the selection down and returns the number of units it has been moved.

Dim lngMoveDown As Long
lngMoveDown = Selection.MoveDown()

MoveEnd - Moves the ending character position of a range or selection.

Dim lngMoveEnd As Long
lngMoveEnd = Selection.MoveEnd()

MoveEndUntil - Moves the end position of the specified selection until any of the specified characters are found in the document.

Dim strCset As String: strCset =  
Dim lngMoveEndUntil As Long
lngMoveEndUntil = Selection.MoveEndUntil(Cset:=strCset)

MoveEndWhile - Moves the ending character position of a selection while any of the specified characters are found in the document.

Dim strCset As String: strCset =  
Dim lngMoveEndWhile As Long
lngMoveEndWhile = Selection.MoveEndWhile(Cset:=strCset)

MoveLeft - Moves the selection to the left and returns the number of units it has been moved.

Dim lngMoveLeft As Long
lngMoveLeft = Selection.MoveLeft()

MoveRight - Moves the selection to the right and returns the number of units it has been moved.

Dim lngMoveRight As Long
lngMoveRight = Selection.MoveRight()

MoveStart - Moves the start position of the specified selection.

Dim lngMoveStart As Long
lngMoveStart = Selection.MoveStart()

MoveStartUntil - Moves the start position of the specified selection until one of the specified characters is found in the document. If the movement is backward through the document, the selection is expanded.

Dim strCset As String: strCset =  
Dim lngMoveStartUntil As Long
lngMoveStartUntil = Selection.MoveStartUntil(Cset:=strCset)

MoveStartWhile - Moves the start position of the specified selection while any of the specified characters are found in the document.

Dim strCset As String: strCset =  
Dim lngMoveStartWhile As Long
lngMoveStartWhile = Selection.MoveStartWhile(Cset:=strCset)

MoveUntil - Moves the specified selection until one of the specified characters is found in the document.

Dim strCset As String: strCset =  
Dim lngMoveUntil As Long
lngMoveUntil = Selection.MoveUntil(Cset:=strCset)

MoveUp - Moves the selection up and returns the number of units that it has been moved.

Dim lngMoveUp As Long
lngMoveUp = Selection.MoveUp()

MoveWhile - Moves the specified selection while any of the specified characters are found in the document.

Dim strCset As String: strCset =  
Dim lngMoveWhile As Long
lngMoveWhile = Selection.MoveWhile(Cset:=strCset)

Next - Returns a Range object that represents the next unit relative to the specified selection.

Dim rngNext As Range
Set rngNext = Selection.Next()

PasteExcelTable - Pastes and formats a Microsoft Excel table.

Selection.PasteExcelTable LinkedToExcel:=True, WordFormatting:=True, RTF:=True

PasteSpecial - Inserts the contents of the Clipboard.

Selection.PasteSpecial

Previous - Moves the selected text by the specified number of units, and returns a Range object relative to the collapsed selection.

Dim rngPrevious As Range
Set rngPrevious = Selection.Previous()

SetRange - Sets the starting and ending character positions for the selection.

Dim lngStart As Long: lngStart = 
Dim lngEnd As Long: lngEnd =  
Selection.SetRange Start:=lngStart, End:=lngEnd

StartOf - Moves or extends the start position of the specified range or selection to the beginning of the nearest specified text unit. This method returns a Long that indicates the number of characters by which the range or selection was moved or extended. The method returns a negative number if the movement is backward through the document.

Dim lngStartOf As Long
lngStartOf = Selection.StartOf()

Properties

Active true if the selection in the specified window or pane is active.

Dim booActive As Boolean
booActive = Selection.Active

BookmarkID returns the number of the bookmark that encloses the beginning of the specified selection.

Dim lngBookmarkID As Long
lngBookmarkID = Selection.BookmarkID

Bookmarks returns a Bookmarks collection that represents all the bookmarks in a document, range, or selection.

Dim bkmsBookmarks As Bookmarks
Set bkmsBookmarks = Selection.Bookmarks

Cells returns a Cells collection that represents the table cells in a selection.

Dim cllsCells As Cells
Set cllsCells = Selection.Cells

Characters returns a Characters collection that represents the characters in a document, range, or selection.

Dim chrsCharacters As Characters
Set chrsCharacters = Selection.Characters

ChildShapeRange returns a ShapeRange collection representing the child shapes contained within a selection.

Dim sreChildShapeRange As ShapeRange
Set sreChildShapeRange = Selection.ChildShapeRange

Columns returns a Columns collection that represents all the table columns in a selection.

Dim clmsColumns As Columns
Set clmsColumns = Selection.Columns

ColumnSelectMode true if column selection mode is active.

Selection.ColumnSelectMode = True

Comments returns a Comments collection that represents all the comments in the specified.

Dim cmmsComments As Comments
Set cmmsComments = Selection.Comments

Document returns a Document object associated with the specified selection.

Dim docDocument As Document
Set docDocument = Selection.Document

Editors returns an Editors object that represents all the users authorized to modify a selection within a document.

Dim edtsEditors As Editors
Set edtsEditors = Selection.Editors

End returns or sets the ending character position of a selection.

Selection.End =

EndnoteOptions returns an EndnoteOptions object that represents the endnotes in a selection.

Dim eosEndnoteOptions As EndnoteOptions
Set eosEndnoteOptions = Selection.EndnoteOptions

Endnotes returns an Endnotes collection that represents all the endnotes contained within a selection.

Dim endsEndnotes As Endnotes
Set endsEndnotes = Selection.Endnotes

EnhMetaFileBits returns a Variant that represents a picture representation of how a selection or range of text appears.

Dim varEnhMetaFileBits As Variant
varEnhMetaFileBits = Selection.EnhMetaFileBits

ExtendMode true if Extend mode is active.

Selection.ExtendMode = True

Fields returns a read-only Fields collection that represents all the fields in the selection.

Dim fldsFields As Fields
Set fldsFields = Selection.Fields

Find returns a Find object that contains the criteria for a find operation.

Dim fndFind As Find
Set fndFind = Selection.Find

Flags returns or sets properties of the selection.

Selection.Flags = wdSelActive

FootnoteOptions returns FootnoteOptions object that represents the footnotes in a selection.

Dim fosFootnoteOptions As FootnoteOptions
Set fosFootnoteOptions = Selection.FootnoteOptions

Footnotes returns a Footnotes collection that represents all the footnotes in a range, selection, or document.

Dim ftnsFootnotes As Footnotes
Set ftnsFootnotes = Selection.Footnotes

FormFields returns a FormFields collection that represents all the form fields in the selection.

Dim ffsFormFields As FormFields
Set ffsFormFields = Selection.FormFields

Frames returns a Frames collection that represents all the frames in a selection.

Dim frmsFrames As Frames
Set frmsFrames = Selection.Frames

HasChildShapeRange true if the selection contains child shapes.

Dim booHasChildShapeRange As Boolean
booHasChildShapeRange = Selection.HasChildShapeRange

HeaderFooter returns a HeaderFooter object for the specified selection.

Dim hfrHeaderFooter As HeaderFooter
Set hfrHeaderFooter = Selection.HeaderFooter

HTMLDivisions returns an HTMLDivisions object that represents an HTML division in a web document.

Dim htmsHTMLDivisions As HTMLDivisions
Set htmsHTMLDivisions = Selection.HTMLDivisions

Hyperlinks returns a Hyperlinks collection that represents all the hyperlinks in the specified selection.

Dim hypsHyperlinks As Hyperlinks
Set hypsHyperlinks = Selection.Hyperlinks

InlineShapes returns an InlineShapes collection that represents all the InlineShape objects in a selection.

Dim issInlineShapes As InlineShapes
Set issInlineShapes = Selection.InlineShapes

IPAtEndOfLine true if the insertion point is at the end of a line that wraps to the next line.

Dim booIPAtEndOfLine As Boolean
booIPAtEndOfLine = Selection.IPAtEndOfLine

IsEndOfRowMark true if the specified selection or range is collapsed and is located at the end-of-row mark in a table.

Dim booIsEndOfRowMark As Boolean
booIsEndOfRowMark = Selection.IsEndOfRowMark

LanguageDetected returns or sets a Boolean that specifies whether Microsoft Word has detected the language of the selected text.

Selection.LanguageDetected = True

LanguageID returns or sets the language for the specified object.

Selection.LanguageID = wdAfrikaans

LanguageIDFarEast returns or sets an East Asian language for the specified object.

Selection.LanguageIDFarEast = wdAfrikaans

LanguageIDOther returns or sets the language for the specified object.

Selection.LanguageIDOther = wdAfrikaans

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.

Selection.NoProofing =

OMaths returns an OMaths collection that represents the OMath objects within the current selection.

Dim omsOMaths As OMaths
Set omsOMaths = Selection.OMaths

Orientation returns or sets the orientation of text in a selection when the Text Direction feature is enabled.

Selection.Orientation = wdTextOrientationDownward

Paragraphs returns a Paragraphs collection that represents all the paragraphs in the specified selection.

Dim prgsParagraphs As Paragraphs
Set prgsParagraphs = Selection.Paragraphs

Parent returns an object that represents the parent object of the specified Selection object.

Dim objParent As Object
Set objParent = Selection.Parent

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 is no corresponding bookmark.

Dim lngPreviousBookmarkID As Long
lngPreviousBookmarkID = Selection.PreviousBookmarkID

Range returns a Range object that represents the portion of a document that's contained in the specified object.

Dim rngRange As Range
Set rngRange = Selection.Range

Rows returns a Rows collection that represents all the table rows in a range, selection, or table.

Dim rwsRows As Rows
Set rwsRows = Selection.Rows

Sections returns a Sections collection that represents the sections in the specified selection.

Dim sctsSections As Sections
Set sctsSections = Selection.Sections

Sentences returns a Sentences collection that represents all the sentences in the selection.

Dim sntsSentences As Sentences
Set sntsSentences = Selection.Sentences

Shading returns a Shading object that refers to the shading formatting for the specified selection.

Dim shdShading As Shading
Set shdShading = Selection.Shading

ShapeRange returns a ShapeRange collection that represents all the Shape objects in the selection.

Dim sreShapeRange As ShapeRange
Set sreShapeRange = Selection.ShapeRange

Start returns or sets the starting character position of a selection.

Selection.Start =

StartIsActive true if the beginning of the selection is active.

Selection.StartIsActive = True

StoryLength returns the number of characters in the story that contains the specified selection.

Dim lngStoryLength As Long
lngStoryLength = Selection.StoryLength

StoryType returns the story type for the specified selection.

Dim wstStoryType As WdStoryType
wstStoryType = Selection.StoryType

Tables returns a Tables collection that represents all the tables in the specified selection.

Dim tblsTables As Tables
Set tblsTables = Selection.Tables

Text returns or sets the text in the specified selection.

Selection.Text =

TopLevelTables returns a Tables collection that represents the tables at the outermost nesting level in the current selection.

Dim tblsTopLevelTables As Tables
Set tblsTopLevelTables = Selection.TopLevelTables

Type returns the selection type.

Dim wstType As WdSelectionType
wstType = Selection.Type

WordOpenXML returns a String that represents the XML contained within the selection in the Microsoft Word Open XML format.

Dim strWordOpenXML As String
strWordOpenXML = Selection.WordOpenXML

Words returns a Words collection that represents all the words in a selection.

Dim wrdsWords As Words
Set wrdsWords = Selection.Words

XML returns a String that represents the XML text in the specified object.

Dim strXML As String
strXML = Selection.XML