Word 2010 vba deactivate a document

User avatar
kdock
5StarLounger
Posts: 720
Joined: 21 Aug 2011, 21:01
Location: The beautiful hills of Western North Carolina

Word 2010 vba deactivate a document

Post by kdock »

Hi all,

Is there a way to cause a document to lose focus with vba? I want to deactivate a document once a person hasn't interacted with it for more than 90 seconds. I've got the 90 seconds of idle time worked out, but once the user begins editing again, I need to be able to restart the idle time listener. If s/he simply leaves the document on screen for 5 minutes, then comes back and starts working, the document doesn't seem to fire an event and I can't start idle time-r up again. It would be perfect if I could simply cause it to lose focus after the 90 second time-out. Then the WindowActivate event could start it going again.

Any thoughts?
Thanks! Kim :bananas:
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word 2010 vba deactivate a document

Post by HansV »

The only way I can think of would be to activate another document (e.g. a new document), but that may not be desirable...

By the way, how are you detecting idle time?
Best wishes,
Hans

User avatar
kdock
5StarLounger
Posts: 720
Joined: 21 Aug 2011, 21:01
Location: The beautiful hills of Western North Carolina

Re: Word 2010 vba deactivate a document

Post by kdock »

Hi Hans,

I'm detecting system idle time with this code. I found several references to LASTINPUTINFO and GetTickCount and made some adjustments.

Code: Select all

Option Explicit
'These variables manage start, stop, and idle time
Private Type PLASTINPUTINFO
  cbSize As Long
  dwTime As Long
End Type
Private Declare Function GetLastInputInfo Lib "user32.dll" (ByRef plii As PLASTINPUTINFO) As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long

'SUBS AND FUNCTION TO CAPTURE IDLE TIME
Public Function SystemIdleTime() As Long
    Dim tLastInput As PLASTINPUTINFO
    tLastInput.cbSize = Len(tLastInput)
    Call GetLastInputInfo(tLastInput)
    SystemIdleTime = GetTickCount - tLastInput.dwTime
End Function

'Check the idle time
Sub StartSystemIdleTimer()
    Application.OnTime DateAdd("s", 5, Now), "CheckIdle"
End Sub
'Test how long the idle timer has run
Sub CheckIdle()
    If SystemIdleTime / 1000 < 90 Then
        Call StartSystemIdleTimer
    Else
        StopTimer
    End If
End Sub
I actually have two timers going at the same time. One to capture editing time and one to stop the timer when inactivity reaches 90 seconds.

So, I just need to be able to restart the timers when the user starts editing again. I can do this easily if they switch to another document, or switch to another program.

I just can't find a way to start them back up again with no actual event to intercept.

Kim
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word 2010 vba deactivate a document

Post by HansV »

Your code detects overall system idle time. If the user switches to another application, for example Excel, and diligently works in it for an hour, the timer will keep on running. Only when there is no keyboard or mouse activity at all for 90 seconds will the timer stop. Is that what you intended?
Best wishes,
Hans

User avatar
kdock
5StarLounger
Posts: 720
Joined: 21 Aug 2011, 21:01
Location: The beautiful hills of Western North Carolina

Re: Word 2010 vba deactivate a document

Post by kdock »

Well, no, that doesn't happen because I use the WindowActivate and WindowDeactivate events to also start and stop the edit timer. This technique works quite well when switching between documents or programs. It's only when they leave a document open and make phone calls or go to lunch that the timer would keep running. And that's the scenario I'm trying to address now.

Since I'm only concerned with handling those times someone is not using their computer, perhaps, once the timers are all stopped, I could then start listening for activity? And start the timers again when someone uses the mouse or taps a key? Could I then use something like Application.Activate to trigger the event firing?
Kim :bananas:
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word 2010 vba deactivate a document

Post by HansV »

It's not perfect, but you might use the Application.WindowSelectionChange event to restart the timers.
Best wishes,
Hans