How to select first word into document?

A

avkokin

Hello.
I need to select first word into current document. I use follow macro:

Sub selFirstWord()
ActiveDocument.Words.First.Select
End Sub

But if text has the space or pilcrow sing at the first set-out, then
selecting it - not word. Similar with last word.
Question: how to set bound for selection only for words?
Thank you very much.
 
D

Doug Robbins - Word MVP

Dim myrange As Range
Set myrange = ActiveDocument.Words(1)
If myrange = Chr(13) Then
myrange.Collapse wdCollapseEnd
myrange.Paragraphs(1).Range.Words(1).Select
ElseIf myrange = " " Then
myrange.Delete
ActiveDocument.Words(1).Select
Else
myrange.Select
End If

There may however be more cases that you will need to consider

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
A

avkokin

I use follow code that select last word. It seems it works.
Sub selLastWord()
Dim myrange As Range
Set myrange = ActiveDocument.Words.Last
If myrange = Chr(13) Then
myrange.Collapse wdCollapseStart
myrange.Move wdWord, -1
myrange.Select
Selection.MoveLeft wdWord, 1, wdExtend
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top