determining if cursor is at the start of a real word

L

Larry

I use this code to determine if the cursor is directly to the left of a
real word, rather than directy to the left of a comma or close quote or
some other punctuation which is a "word" according to Word. In other
words, the proof that the cursor is at the start of a real word, and not
at the end of a real word and to the left of a close punctuation, is
that the previous character is a space or an open paren or an open quote
and so on.

Is there a more economical way of achieving the same result, so that I
wouldn't have to have all these conditions checking for the previous
character?

Here's the code:

' If IP is at start of word, move cursor one space right.

Dim prevchar As String
prevchar = Selection.Characters.First.Previous

If (Selection.Start = Selection.Words(1).Start And _
Selection.Characters(1) <> Chr(13) And prevchar = " " Or _
prevchar = "-" Or prevchar = """" Or prevchar = "[" Or _
prevchar = "(" Or prevchar = Chr(151)) Then

x = True
Selection.MoveRight wdCharacter, 1
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, notre ami < Larry > nous laissait savoir que :
In this message, our friend < Larry > informed us that:


|| I use this code to determine if the cursor is directly to the left
|| of a real word, rather than directy to the left of a comma or close
|| quote or some other punctuation which is a "word" according to Word.
|| In other words, the proof that the cursor is at the start of a real
|| word, and not at the end of a real word and to the left of a close
|| punctuation, is that the previous character is a space or an open
|| paren or an open quote and so on.
||
|| Is there a more economical way of achieving the same result, so that
|| I wouldn't have to have all these conditions checking for the
|| previous character?
||

Working with words is always tricky!

I guess that to speed up the code you could check if the word is more than 1
character long, if so, than it is a real word, if it is exactly one
character long then you have either a space, a punctuation mark, a single
letter word or a digit. But those would account for a significantly smaller
pecentage of the "words", so your code would not executre as often.

Out of curiousity, why the line

Selection.MoveRight wdCharacter, 1

once you have deterermined that the word is a word?

If the owrd is "a", "I" or a digit for example, your cursor will end up at
the end of the word, not after the fisrt character.
If you check for length and validity, once you have determined that is is a
valid one character word, then you might want to do something different with
your cursor... no?

Just my 2¢!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
L

Larry

Thank you, Jean-Guy, but your suggestion would make the macro not work
on one character words.

The code is part of a macro that replaces (or supplements) Word's
built-in Underline and Italic command. Word's built-in command cannot
underline/italicize a word if the cursor is at the beginning or end of
the word. The cursor needs to be moved into the word first. This macro
gets around that, and is convenient. But I just wanted to find simpler
code to accomplish the same thing.

My macro also underlines/italicizes a single-letter word, which Word's
built-in commands do not do. With Word, it's necessary to select a
single-letter word in order to format it.

Larry
 

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