Macro To Find And Select What Is Between Words

A

Alan Stancliff

I would like a macro that can find and select what is between the word
the cursor is presently on and its neighbor to the left. That could be
any of the following:

1. One or more spaces
2. A comma and one or more spaces (, )
3. A period and one or more spaces. (. )
4. A semicolon and one or more spaces (; )
5. A colon and one or more spaces :) )

The macro would highlight (select) what is between the word the cursor
is on and its neighbor to the left.

Any help would be appreciated.

Regards,

Alan
 
D

Doug Robbins - Word MVP

Use

Selection.Words(1).Select
Selection.Collapse wdCollapseStart
Selection.MoveStartWhile Cset:=" ,.;:", Count:=wdBackward


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

Alan Stancliff

Hi Doug,

That definitely works. I was playing with it, looking in he help files,
trying to figure out why it works, and I decided to see if I could make
it do the same thing but for the word on its right. I did a bit of
browsing and found this link here:

http://msdn2.microsoft.com/en-us/library/bb217042.aspx

But when I substituted wdForward for wdBackward in line 3, the cursor
moved in the right direction, but it did not seem to select.

Why not and what can I do to make it work? Your first example will help
solve a problem for me but now I'm really curious about wdBackward and
wdForward.

Earlier, I had been trying to do it with wildcards, but I don't seem to
be able to figure out how to make that angle work either. I had been
looking at this page:
http://www.gmayor.com/replace_using_wildcards.htm


Regards,

Alan
 
H

Helmut Weber

Hi Alan,

forwards instead of backwards is a bit more difficult.
Word's definition of a word is different from
what a word is (meant to be) in natural language.

For Word, a word consists of a sequence of characters
plus the trailing spaces, if there are any!

So you have to find a way, to exclude them, like that:

Sub Moveright()
Dim rTmp As Range
Set rTmp = Selection.Words(1)
While Right(rTmp, 1) = " "
rTmp.MoveEnd unit:=wdCharacter, Count:=-1
Wend
rTmp.Select
Selection.Collapse wdCollapseEnd
Selection.MoveEndWhile _
Cset:=" ,.;:", Count:=wdForward
End Sub

HTH

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

Alan Stancliff

Hello Helmut,

Thank you Helmut. You have been very helpful. Thanks to you and Doug, I
have lots of stuff to study.

Regards,

Alan Stancliff
 

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