WordBasic Conversion .ExtendSelection

B

BobTheElder

I am converting some old macros and need a VBA equivalent to the
WordBasic.ExtendSelection "?" [where ? indicates some character].

My old code uses this mechanism extensively to include various sized text to
subsequently search for in the document. The VB Equivalents help only
suggests Selection.ExtendMode = True & Selection.Expand Unit:=wdUnits, which
is not helpful in this situation.

Bob
Word 2003
 
K

Klaus Linke

I am converting some old macros and need a VBA equivalent to the
WordBasic.ExtendSelection "?" [where ? indicates some character].

My old code uses this mechanism extensively to include various sized text to
subsequently search for in the document. The VB Equivalents help only
suggests Selection.ExtendMode = True & Selection.Expand Unit:=wdUnits, which
is not helpful in this situation.

Using the macro recorder and a bit of manual editing:

Selection.ExtendMode = True
Selection.Extend Character:="?"
Selection.ExtendMode = False

If your macro uses that a lot, and it turns out to be too slow, you might post back with what the macro is supposed to do.
There may be faster ways.

Regards,
Klaus
 
B

BobTheElder

This appears to perform as desired. I've been converting several old macros
and found that it sometimes takes thinking outside the box or a fresh
viewpoint to convert WordBasic. Thank you.
 
K

Klaus Linke

Just noticed to things:

Setting ExtendMode isn't actually necessary.

And the VBA help for the Extend method has a better code:

With Selection
.StartIsActive = False
.Extend Character:="R"
End With

That makes sure you're moving the end of the selection, not the start.
Quite cool that you can move the Start, really. I wasn't aware of that.

Klaus
 
J

Jay Freedman

And on the principle that there's never just one way to do anything, you
could also use the .MoveEndUntil method, whose Help example goes like this:

With Selection
.MoveEndUntil Cset:="a", Count:=wdForward
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With

The advantage with this one is that the Cset parameter can be a list of
characters, and the extension will stop when it hits any one of them. If you
wanted to extend to the next punctuation mark, you could write

Cset:=".,;:'!"

or some variation on that. There is also a .MoveStartUntil, and both
..MoveEndWhile and .MoveStartWhile.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
B

BobTheElder

Jay & Klaus
Thank you for the different suggestions, I'm learning more and more as I
work on this conversion.

Bob
 

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