Thursday 23 May 2013

Macro to calculate system idle time in Excel.

Example-
In below example, I have shown how we can find the system idle time using excel vba macro.

Code and Syntax to calculate system idle time.
In below code, GetIdleTime() function returns the number of seconds the system has been idle for.
Here idle means that there is no input from user from keyboard or mouse.

Private Type LASTINPUTINFO
  cbSize As Long
  dwTime As Long
End Type


Private Declare Sub GetLastInputInfo Lib "user32" (ByRef plii As LASTINPUTINFO)
Private Declare Function GetTickCount Lib "kernel32" () As Long


Function GetIdleTime() As Single
  Dim a As LASTINPUTINFO
  a.cbSize = LenB(a)
  GetLastInputInfo a
  GetIdleTime= (GetTickCount - a.dwTime) / 1000
End Function


Sub check()
        Application.Wait (Now() + TimeValue("0:00:11"))   

   ' make the system idle for 11 sec. Donot type from keyboard or click from mouse
        MsgBox getIdleTime()

End Sub

LASTINPUTINFO is a structure defined by microsoft and it Contains the time of the last input.

GetTickCount  functions gets the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

When you will execute above programm, you will be prompted approx. 11 as system idle time.

This is how we can calculate the systme idle time in VBA Excel macro.

1 comment:

  1. Hello,

    Thank you for your post. I am trying to create an Excel tool that tracks system idle time (time passed without mouse clicks or keyboard usage). The mouse usage tracked for idle time is based on clicks and not movement; what is the difference in code because of this?

    Thank you

    ReplyDelete

Popular Posts

Search This Blog