determine last word selected or changed possition

F

filo666

Hello, Is there a way to set a variable with the possition of the last word
changed???
last word selected???

TIA
 
J

Julian

Yes, I have done something similar myself but the functionality you seek is
embedded deep in code that doesn't match your specifics so I shan't dump the
whole lot here... however here are some pointers...

1. You need to use events, specifically the WindowSelectionChange event,
which means creating a Class module (e.g. "eventClassModule") and declaring
Public WithEvents App As Word.Application in it, and in a code module
initialising events declare e.g. "Public myWord As New eventClassModule" and
then somewhere put e.g. Set myWord.App = Word.Application
2. On selection change (i.e. in the class module created in step 1, in sub
App_WindowSelectionChange...) you then need to determine whether you are in
the same word or not, and if that word has changed each time the selection
changes
2a. Assuming, for simplicity that the selection is a Selection point at the
time of change, copy the selection range and expand it to the encompassing
word; record the range in a range variable.
2b. The next time the selection changes compare the location (perhaps
linenumber and distance from beginning of the line) of the current word with
that of the previous word... then look back at the previous range that
contained the "word" and see if the text has changed...

Finally, be aware that as you type, the selection does not change - only
clicks and navigation keys and goto and search etc. actually move the
selection...

Someone more virtuous might post you specific code, but if you feel up to
doing it yourself I hope the above helps... at least I hope it's more of a
help than a hindrance!

Julian
 
F

fumei via OfficeKB.com

You need to define your requirements more. Define "last word".

Say you have (as text):

"the quick brown fox
yadda yadda"

Say you move the selection to the "w" of "brown", and type "greeeee"

"the quick browgreeeeen fox
yadda yadda"

Now say you move the Selection (cursor) to the second "yadda", and run:

Selection.GoTo what:=wdGoToBookmark, Name:="\PrevSel1"

the Selection (cursor) will move to the last "e" of the "greeeee" you typed.

Now, say you actually select "quick"...but you do NOT do any action. You
just selected it....then changed your mind, and moved the cursor to that
second yadda. Remember, you DID select "quick".

Selection.GoTo what:=wdGoToBookmark, Name:="\PrevSel1"

will STILL go the the last "e" of the "greeeee" you typed. Even though the
last selection was "quick". If you DO something with "quick", then PrevSel1
will go back to it. If you just select it, but do nothing, it won't.

What I am saying is you have to clearly define exactly what it is you want to
do.

Last word changed? Yes, you could use PrevSel1 and then expand the Selection
to the whole word - if it was not the whole word in the first place. if the
previous Selection WAS the whole word, then PrevSel1 would get the whole word.
But typing is not a whole word. PrevSel1 goes to the last location as a
single character.
 
F

fumei via OfficeKB.com

I also want to emphasize what Julian wrote regarding using events:

"be aware that as you type, the selection does not change - only
clicks and navigation keys and goto and search etc. actually move the
selection..."
You need to define your requirements more. Define "last word".

Say you have (as text):

"the quick brown fox
yadda yadda"

Say you move the selection to the "w" of "brown", and type "greeeee"

"the quick browgreeeeen fox
yadda yadda"

Now say you move the Selection (cursor) to the second "yadda", and run:

Selection.GoTo what:=wdGoToBookmark, Name:="\PrevSel1"

the Selection (cursor) will move to the last "e" of the "greeeee" you typed.

Now, say you actually select "quick"...but you do NOT do any action. You
just selected it....then changed your mind, and moved the cursor to that
second yadda. Remember, you DID select "quick".

Selection.GoTo what:=wdGoToBookmark, Name:="\PrevSel1"

will STILL go the the last "e" of the "greeeee" you typed. Even though the
last selection was "quick". If you DO something with "quick", then PrevSel1
will go back to it. If you just select it, but do nothing, it won't.

What I am saying is you have to clearly define exactly what it is you want to
do.

Last word changed? Yes, you could use PrevSel1 and then expand the Selection
to the whole word - if it was not the whole word in the first place. if the
previous Selection WAS the whole word, then PrevSel1 would get the whole word.
But typing is not a whole word. PrevSel1 goes to the last location as a
single character.
Look at the builtin bookmark "\PrevSel1".
[quoted text clipped - 4 lines]
 

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