Page 1 of 3 123 LastLast
Results 1 to 15 of 39
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Monitoring typing (Word97SR2)

    I want to monitor the state of the document while the user is typing. Typical example: I'm writing an article and want to be warned as I pass the 300-word mark.

    Is it possible to have a routine in Word97(VBA) that monitors typing?


    I'm familiar with the KeyDown events, but they apply only to controls in a GUI form.

    My routine wouldn't have to monitor every keystroke - taking inventory as a space character was typed would suffice.

    I don't think I want a timer checking on me every ten seconds or so. I think I'd like the code to be dormant if the Application Window wasn't active.




    "KeyDown, KeyUp events: Occur in sequence when a user presses and releases a key."

    "object Required. A valid object name."

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Monitoring typing (Word97SR2)

    Word monitors for spaces, punctuation, etc., for AutoCorrect, but there isn't any obvious way to hook into that.

    The WindowSelectionChange event only monitors multiple character selections and not insertion points.

    Sounds like WinAPI time.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    (groan)

    >Sounds like WinAPI time.


    Thanks!

  4. #4
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    That groan was a little premature ...

    <pre>Sub SetKeyBindings()
    CustomizationContext = NormalTemplate
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
    Command:="CountInStatusBar", _
    KeyCode:=wdKeySpacebar
    End Sub

    Sub CountInStatusBar()
    Dim myRange As Range
    Set myRange = Selection.Range
    myRange.Start = ActiveDocument.Content.Start
    Selection.TypeText " "
    StatusBar = "Characters: " & _
    myRange.ComputeStatistics(wdStatisticCharacters) & _
    " / Words: " & _
    myRange.ComputeStatistics(wdStatisticWords)
    End Sub</pre>

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  5. #5
    Lounger
    Join Date
    Nov 2001
    Location
    Fort Dodge, Iowa, USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    There's a wordcountField (Insert->Field...) but I can remember what exactly it's called.

    I know where I last saw it, though...

    If you check the end of the Access Archon column in the last issue of Woody's Acces Watch, there is a link to Helen Fedema's (sp?) site. From her site, you can download a copy of all referenced files _as well as_ a copy of that Issue's Access Archon in MS Word format. At the end of this Word document is a field which has a wordcount in it.

    It isn't an alarm, but you could set up a template with that Field inserted a few lines from the top. When you create a new article from this template and start typing _at the top of the document_, the wordcount Field would always stay a couple lines from the end of your typing.

    Hope that helps

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    "Yeee", as they say, "Haaaaaaaaaaaaaaaaah!"

    Just what the doctor ordered. A nice status display as I type.

    The display is triggered by a space character on account of the "KeyCode:=wdKeySpacebar", I'll be bound, so i could change that if I wanted to.

    The "CountInStatusBar" does the deed, so i could place that as a toolbar icon or shortcut key, too, if I wanted.

    But I like the automated use of the status bar best of all.



    Hats off to the proverbial Klaus!

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    >There's a wordcountField (Insert->Field...)

    Choose Insert, Field, Document Information, NumWord.

    malkor , Thanks.

    I used to use this field, and inserted it at the head of each essay or poem so's the editor could see up front how much I'd written ("2,500 words? let's save this for the next millenium edition ....")

    You will see Klaus's post - a much more elegant solution than mine.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    Klaus,

    Very clever. I hope there's no downside to trapping the user's every press of the spacebar.

    Very clever.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    > I hope there's no downside to trapping the user's every press of the spacebar.

    I withdraw my suggestion. The problem isn't the intercepted spacebar (though I haven't tested it extensively). The counting is just too slow in documents of any size, and you'll suffer writer's block.

    I thought of the following alternative a while back; but it also has its drawbacks (constant screen flicker):

    <pre>Sub StatusBarWordCount()

    Dim MyRange As Range
    Dim start, speed
    Dim WordCount As Long
    start = Timer

    Do
    Set MyRange = Selection.Range
    While Timer - start < 3 * speed
    DoEvents
    Wend
    start = Timer
    MyRange.start = ActiveDocument.Range.start
    WordCount = MyRange.ComputeStatistics(wdStatisticWords)
    StatusBar = WordCount & " words (quit with Ctrl-Pause)."
    speed = Timer - start
    Loop
    End Sub</pre>


    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    <pre>Sub CountInStatusBar()
    Dim myRange As Range
    Set myRange = Selection.Range
    myRange.Start = ActiveDocument.Content.Start
    myRange.End = ActiveDocument.Content.End 'render count from start TO THE END OF THE DOCUMENT
    Selection.TypeText " "
    StatusBar = "Characters: " & _
    myRange.ComputeStatistics(wdStatisticCharacters) & _
    " / Words: " & _
    myRange.ComputeStatistics(wdStatisticWords)
    End Sub
    </pre>



    I note the question about performance. I imagine it won't be much of a problem for me, because I generally lose my train of thought after 300 words (grin!).

    I had, however, gone ahead and contemplated making the trigger less-common character, such as period, question-mark, or even the at-sign.

    I reasoned that if I wanted a task to be run periodically in the background, but not too often (Checking on available space on the hard drive would be a poor example; displaying how many minutes/characters had elapsed sicne the last time I saved my development template would be a better example) then a rare trigger character would suffice.

    Kevin's Timer is still sitting in my utilities template. Couldn't we combine the periodic trigger character event (Klaus) with a more definite time check (Kevin) to achieve a trigger of a timed event without too much overhead?

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Monitoring typing (Word97SR2)

    Hah! I mistakenly assumed that since the dialog wouldn't let me assign a macro to a space, it couldn't be coded. I guess MS was trying to protect the casual user (and me) from this technique. Thanks for clearing that up. (And just think of all the fun I can have with an April 1 memo this year. <img src=/S/wink.gif border=0 alt=wink width=15 height=15>)

    Later...

    I had tested that macro in a document I closed. Now I can't type any spaces. Whoops. The only way out, without doing a ClearAll and losing all my keys, is to close Word without saving Normal.dot.

  12. #12
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Monitoring typing (Word97SR2)

    Hi Klaus,
    I confess I've only tested this on a 5000 odd word document (and only in Word2000) but there's no perceptible speed hit at that sort of size if you change your code to:
    <pre>Sub CountInStatusBar()
    Selection.TypeText " "
    StatusBar = "Words: " & ActiveDocument.BuiltInDocumentProperties(wdPropert yWords)
    End Sub
    </pre>

    I presume this is because Word is keeping track of it anyway.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  13. #13
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Monitoring typing (Word97SR2)

    Hi Rory,

    Seems to work nicely. Thanks for saving my little macro!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  14. #14
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Traps for young players

    ... so there I was, bopping along merrily, spell-checking a document; minding, as we say, my own business, when what pops up as a spelling error but "keystring".

    Not a problem, I think, I'll just stick a space in the keystring, between key and string.

    Than I get the (attached) run-time error from the macro. Inserting a space, of course, triggers the status-bar update code, and it doesn't like to co-exist with spell-checker, and vice-versa!
    Attached Files Attached Files

  15. #15
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Traps for young players

    Chris,
    Adding the following should work (again only briefly tested under Word2000) assuming you're running spell check from the usual button on the toolbar:

    Code:
    Sub toolsproofing()
        For Each myKey In Application.KeysBoundTo(wdKeyCategoryCommand, "CountInStatusBar")
            myKey.Clear
        Next myKey
        ActiveDocument.CheckSpelling
        SetKeyBindings
    End Sub
    Hope that helps.
    Last edited by jscher2000; 2011-03-19 at 16:26. Reason: Fixed [code][/code] tags.
    Regards,
    Rory

    Microsoft MVP - Excel

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •