Total Pageviews

Friday 11 January 2013

System Idle Time Tracking using VBA


Every time you call the function idle time (either by timer event) in a procedure, it would return the idle time of the computer in seconds.
Here is the code you can try out in Excel module to understand better.  

Before you try out these codes, I recommend you to save & close existing workbook and open a fresh workbook.

Go to Visual Basic editor
Before you proceed make sure immediate window is visible.  To view the window Go to View > Immediate window in VB Editor.
Insert Module (Insert > Module)
Paste the below code
Once pasted highlight your cursor on Idletime_Immediate_Window1 and hit F5
See the immediate window.
For every one second computer idle time is displayed in seconds.

To stop the Macro hold ctrl+break button and execution would stop.

Code:
 '/// Idle Time Tracking  
 Private Declare Sub GetLastInputInfo Lib "user32" (ByRef plii As LASTINPUTINFO)  
 Private Declare Function GetTickCount Lib "kernel32" () As Long  
 Dim SngIdleTime As Single  
 Private Type LASTINPUTINFO  
  cbSize As Long  
  dwTime As Long  
 End Type  
 Function IdleTime() As Single  
  Dim a As LASTINPUTINFO  
  a.cbSize = LenB(a)  
  GetLastInputInfo a  
  IdleTime = (GetTickCount - a.dwTime) / 1000  
 End Function  
 Sub Idletime_Immediate_Window1()  
 Application.OnTime TimeValue(Now) + TimeValue("00:00:01"), "Idletime_Immediate_Window2"  
 Debug.Print IdleTime  
 End Sub  
 Sub Idletime_Immediate_Window2()  
 Application.OnTime TimeValue(Now) + TimeValue("00:00:01"), "Idletime_Immediate_Window1"  
 Debug.Print IdleTime  
 End Sub  



Screenshot :


1 comment:

  1. How Can we fetch the Idle Time,, with this we are getting the idle time,, but after closing the sheet,, complete details are removing automatically.

    ReplyDelete