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, _

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.


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

© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext