tags:

views:

291

answers:

3

I want to take an action in an Excel workbook macro after a period of inactivity (hide/protect some worksheets). What is the best/simplest way to achieve this?

Í'm assuming I'll use Application.OnTime to periodically check if the user has been active. But what events should I handle to see if the user was "active" (i.e. has does something - anything - with the workbook)?

Clarification: I want to detect all activity, not just changes. I.e. including mouse clicks, selecting, copying, navigating with the keyboard, changing worksheets, ...

I'm assuming that when a UI event happens that represents user activity, I will set a variable thus:

LastActivityTime = Now

and the macro run by Application.OnTime will check this variable to see if the user has been active recently. Which events (other than SheetChange) would I need to handle to set this variable? I had kind of hoped there would be KeyUp and MouseUp events, these two would probably have been enough.

Update: I have implemented this by handling Workbook_SheetActivate, Workbook_SheetSelectionChange and Workbook_WindowActivate. Realistically this is probably enough.

A: 

One simple way is to compare the content of the workbook with that of the last time you check. I believe combining this with Application.OnTime will solve your concern.

Martin
Thanks, but I want to detect activity that doesn't change the workbook too. I've added some clarification to my question.
Joe
+1  A: 

I can only see two solutions -- either handle evary single event the Application object has or use GetLastInputInfo function.

GSerg
GetLastInputInfo sounds promising but seems to be related to the user session rather than an application. I am only interested in user activity related to my Excel workbook - or the Excel process would do.
Joe
A: 

I have implemented this by handling Workbook_SheetActivate, Workbook_SheetSelectionChange and Workbook_WindowActivate. Realistically this is probably enough.

Joe