VBA Word, Select Text

In this article I will explain how you can use VBA for word to select text.


Contents

Select Entire Document:

Using the code snippet below you can select the entire word document:

Selection.WholeStory

Result:

Word VBA Select All


Select The Current Line:

Using the code below you can select the current line:

Sub main()
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
End Sub

 Assume the cursor is somewhere in the middle of the line:

Word VBA Move Cursor To Start of Line
The first line moves the cursor to the start of the line:

Selection.HomeKey Unit:=wdLine

The next line move the cursor to the end of the line while selecting the text:

Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Result:

Word VBA Select Line, Result


Select to End of Line:

The code below will only select till the end of the line:

Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Result:

Word VBA Select Line, To End


Select to Start of Line:

The code below will select text up to the start of the line:

Selection.HomeKey Unit:=wdLine, Extend:=wdExtend

Result:

Word VBA Select Line, To Start


Select Certain Number of Characters:

Lets say we need to select only a certain number of characters from the current location of the cursor. The code below will select 5 characters to the right:

Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend

Result:

Word VBA Select 5 Charcter to right
The code below will select 10 characters to the left:

Selection.MoveLeft Unit:=wdCharacter, Count:=10, Extend:=wdExtend

Result:

Word VBA Select 10 Charcter to left


Select Text up to a Certain Character:

You might want to select text until you reach a certain character. For example a space character, “*”, “/”, …

The code below selects text until a space character is reached:

Sub test()
Dim flag As Boolean
flag = True
While flag = True
    Selection.MoveRight Unit:=wdCharacter, Count:=1, _
    Extend:=wdExtend
    'checks the last character to see if its a space
    If Strings.Right(Selection.Range.Text, 1) = " " Then
        'if it was a space the loop flag will end
        flag = False
    End If
Wend
End Sub

Word VBA, Select up to space
Result:

Word VBA, Select up to space, Result
The line below selects one additional character to the right:

Selection.MoveRight Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend

 The if statement below checks to see if the last character selected is a space character:

If Strings.Right(Selection.Range.Text, 1) = " " Then
...
End If

For more information about the Strings.Right function please see the article below. Although the article was written for Excel, it can be extended to VBA for Word:

See also:

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website  www.software-solutions-online.com

10 thoughts on “VBA Word, Select Text”

Leave a Reply

Your email address will not be published. Required fields are marked *