Group:  Microsoft Word » microsoft.public.word.vba.beginners
Thread: Set range = whole document minus last word (conditionally?)

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

Set range = whole document minus last word (conditionally?)
"KR" <nospam[ at ]nospam.com> 10.07.2006 01:59:22
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



Re: Set range = whole document minus last word (conditionally?)
"Jean-Guy Marcil" <NoSpam[ at ]LeaveMeAlone> 10.07.2006 14:32:41
KR was telling us:
KR nous racontait que :

[Quoted Text]
> 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
jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Re: Set range = whole document minus last word (conditionally?)
"KR" <nospam[ at ]nospam.com> 10.07.2006 15:29:29
Jean-Guy

Thank you for the code! It looks like I was going about it the wrong way :-)

[Quoted Text]
> 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


Re: Set range = whole document minus last word (conditionally?)
Helmut Weber <nbhymsjxdgcn[ at ]mailinator.com> 10.07.2006 16:13:24
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"
Re: Set range = whole document minus last word (conditionally?)
"Jean-Guy Marcil" <NoSpam[ at ]LeaveMeAlone> 10.07.2006 16:59:35
Helmut Weber was telling us:
Helmut Weber nous racontait que :

[Quoted Text]
> 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
jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Re: Set range = whole document minus last word (conditionally?)
"KR" <nospam[ at ]nospam.com> 10.07.2006 19:35:41
Awesome- this is exactly what I needed!
Thanks again,
Keith

"Helmut Weber" <nbhymsjxdgcn[ at ]mailinator.com> wrote in message
news:8vu4b2hsa3bgs906mmjti7kql5f869s486[ at ]4ax.com...
[Quoted Text]
> 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"


Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net