Export (0) Print
Expand All
Cut
Run
Expand Minimize
This topic has not yet been rated - Rate this topic

WordLeft Method (TextSelection Object)

Visual Studio .NET 2003

Moves the selected text left the specified number of words.

[Visual Basic .NET]
Public Sub WordLeft( _
   Optional ByVal Extend As Boolean = False, _
   Optional ByVal Count As Integer = 1 _
) 
[Visual Basic 6]
Sub WordLeft( _
   Optional ByVal Extend As Boolean = False, _
   Optional ByVal Count As Long = 1 _
) 
[C++]
HRESULT __stdcall WordLeft(
   VARIANT_BOOL Extend,
   long Count
);
[C#]
public void WordLeft(
   bool Extend,
   int Count
);
[JScript .NET]
public function WordLeft(
   Extend : Boolean,
   Count : int
)

Parameters

Extend
Optional. Determines whether the moved text is collapsed or not. The default is False.
Count
Optional. Represents the number of words to move left. The default value is 1.

Remarks

If Extend is True, then the active end of the selected text is moved left Count number of words. Otherwise, the selected text is collapsed and positioned Count words left from the active end. If the beginning of the document is encountered before Count words, then the position remains at the beginning of the document.

If the value of Count is negative, then WordLeft performs like the WordRight Method.

The active language manager for the text document defines the meaning of a "word."

Example

Sub WordLeftExample()
    ' Before running this example, open a text document.
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    If objSel.IsEmpty Then
        ' If there is no text selected, swap the words before and after 
        ' the insertion point. We begin by selecting the word before the 
        ' insertion point.
        objSel.WordLeft(True)
        If Not objSel.IsEmpty Then
            ' We can continue only if the selection was not already at the 
            ' beginning of the document.
            Dim strBefore As String = objSel.Text

            ' The text is saved in strBefore; now delete it and move past 
            ' the following word.
            objSel.Delete()
            objSel.WordRight(True)
            If objSel.Text.StartsWith(" ") Or objSel.Text.StartsWith(Microsoft.VisualBasic.ControlChars.Tab) Then
                ' The previous call to WordRight may have skipped some 
                ' white space instead of an actual word. In that case, we 
                ' should call it again.
                objSel.WordRight(True)
            End If

            ' Insert the new text at the end of the selection.
            objSel.Insert(strBefore, vsInsertFlags.vsInsertFlagsInsertAtEnd)
        End If
    Else
        ' If some text is selected, replace the following word with the 
        ' selected text.
        Dim strSelected As String = objSel.Text

        objSel.MoveToPoint(objSel.BottomPoint)
        objSel.WordRight(True)
        If objSel.Text.StartsWith(" ") Or objSel.Text.StartsWith(Microsoft.VisualBasic.ControlChars.Tab) Then
            ' The previous call to WordRight may have skipped some 
            ' white space instead of an actual word. In that case, we 
            ' should call it again.
            objSel.WordRight(True)
        End If

        ' Insert the text, overwriting the existing text and leaving the 
        ' selection containing the inserted text.
        objSel.Insert(strSelected, vsInsertFlags.vsInsertFlagsContainNewText)
    End If
End Sub

See Also

Applies To: TextSelection Object

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.