Set range = whole document minus last word (conditionally?)

K

KR

I'm unfamiliar with the word object model- I do all my VBA in Excel, but I'm
helping someone out with what should be a simple macro.

We are currently using [ActiveDocument.SpellingErrors.Count] which gives the
count of all the misspelled words in the document. however, if the user is
in the middle of typing a word (Inte when spelling Intelligent) then that
counts as a misspelled word when that line of code executes. So, we'd like
to adjust the range to count everything except the last word. Even better
would be to include all words if the cursor is not "in" a word, e.g. the
user types a space or a period or comma or anything that signifies that the
last word is complete.

I'm thinking the simple version, always excluding the last word would be
something like the following, but I don't know the syntax for Word well
enough to make it work, or to conditionally include or exclude that last
word.

Simple version (doesn't work):

myRng = ActiveDocument.SetRange(Start:=0,
End:=ActiveDocument.words.count-1)
ActiveDocument.myRng.SpellingErrors.Count

Thanks in advance for any help,
Keith
 
J

Jean-Guy Marcil

KR was telling us:
KR nous racontait que :
I'm unfamiliar with the word object model- I do all my VBA in Excel,
but I'm helping someone out with what should be a simple macro.

We are currently using [ActiveDocument.SpellingErrors.Count] which
gives the count of all the misspelled words in the document. however,
if the user is in the middle of typing a word (Inte when spelling
Intelligent) then that counts as a misspelled word when that line of
code executes. So, we'd like to adjust the range to count everything
except the last word. Even better would be to include all words if
the cursor is not "in" a word, e.g. the user types a space or a
period or comma or anything that signifies that the last word is
complete.

I'm thinking the simple version, always excluding the last word would
be something like the following, but I don't know the syntax for Word
well enough to make it work, or to conditionally include or exclude
that last word.

Simple version (doesn't work):

myRng = ActiveDocument.SetRange(Start:=0,
End:=ActiveDocument.words.count-1)
ActiveDocument.myRng.SpellingErrors.Count

Dim rgeDoc As Range

Set rgeDoc = ActiveDocument.Range
rgeDoc.MoveEnd wdWord, -2

("-2" because ¶ counts as words, so we have to remove the last ¶ from the
range as well as the last word).

But what if the user is typing in the middle of the text, nit at the exact
end?
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

KR

Jean-Guy

Thank you for the code! It looks like I was going about it the wrong way :)
But what if the user is typing in the middle of the text, nit at the exact
end?

The task we are measuring is transcription, so hopefully we won't have that
problem...unless the user happens to look up and see a spelling error, and
is in the process of fixing it.

I suppose, for the sake of our data, that the best thing to do would be to
capture the total character count of the document, and the current position
(in characters) of the cursor. That way if the cursor isn't within a few
characters of the end of the document, we'll know that they were somewhere
else in the document, presumably fixing an error.

I know how to get the overall character count; what is the best way to
determine the cursor's current location?

Thanks!!
Keith
 
H

Helmut Weber

Hi KR,

in case Jean-Guy needs a break, ;-)

Sub test9002()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
rDcm.End = selection.Range.End
MsgBox "You are " & ActiveDocument.Characters.Count - 1 - _
rDcm.Characters.Count & " characters from the doc's end"
End Sub

Note: You'll never reach the doc's end.
You can't get beyond the last character.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jean-Guy Marcil

Helmut Weber was telling us:
Helmut Weber nous racontait que :
Hi KR,

in case Jean-Guy needs a break, ;-)

Thanks!

It is July after all - and hot and sunny over here!

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

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