如何检测 Excel 工作簿中的用户不活动
我想在一段时间不活动后在 Excel 工作簿宏中执行操作(隐藏/保护某些工作表)。 实现这一目标的最佳/最简单方法是什么?
假设我将使用 Application.OnTime
定期检查用户是否处于活动状态。 但是我应该处理哪些事件来查看用户是否处于“活动”状态(即对工作簿执行了某些操作)?
澄清:我想检测所有活动,而不仅仅是变化。 即包括鼠标单击、选择、复制、使用键盘导航、更改工作表...
我假设当发生代表用户活动的 UI 事件时,我将设置一个变量:
LastActivityTime = Now
以及由 运行的宏Application.OnTime
将检查此变量以查看用户最近是否处于活动状态。 我需要处理哪些事件(SheetChange
除外)来设置此变量? 我本来希望有 KeyUp
和 MouseUp
事件,这两个可能就足够了。
更新:我通过处理 Workbook_SheetActivate
、Workbook_SheetSelectionChange
和 Workbook_WindowActivate
实现了这一点。 实际上这可能就足够了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只能看到两种解决方案 - 要么处理 Application 对象具有的每个单一事件,要么使用 GetLastInputInfo 函数。
I can only see two solutions -- either handle evary single event the Application object has or use GetLastInputInfo function.
我通过处理 Workbook_SheetActivate、Workbook_SheetSelectionChange 和 Workbook_WindowActivate 来实现这一点。 实际上这可能就足够了。
I have implemented this by handling Workbook_SheetActivate, Workbook_SheetSelectionChange and Workbook_WindowActivate. Realistically this is probably enough.
一种简单的方法是将工作簿的内容与上次检查的内容进行比较。 我相信将此与 Application.OnTime 结合起来会解决您的担忧。
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.