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
Word 2010 vba deactivate a document
-
- 5StarLounger
- Posts: 720
- Joined: 21 Aug 2011, 21:01
- Location: The beautiful hills of Western North Carolina
Word 2010 vba deactivate a document
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Word 2010 vba deactivate a document
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?
By the way, how are you detecting idle time?
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 720
- Joined: 21 Aug 2011, 21:01
- Location: The beautiful hills of Western North Carolina
Re: Word 2010 vba deactivate a document
Hi Hans,
I'm detecting system idle time with this code. I found several references to LASTINPUTINFO and GetTickCount and made some adjustments.
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
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
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.
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Word 2010 vba deactivate a document
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
Hans
-
- 5StarLounger
- Posts: 720
- Joined: 21 Aug 2011, 21:01
- Location: The beautiful hills of Western North Carolina
Re: Word 2010 vba deactivate a document
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
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
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Word 2010 vba deactivate a document
It's not perfect, but you might use the Application.WindowSelectionChange event to restart the timers.
Best wishes,
Hans
Hans