开发者

Call event in Excel VBA when switching to another app

开发者 https://www.devze.com 2023-03-22 04:12 出处:网络
I\'m lookin开发者_JS百科g for an event which gets called when the user switches to another app (without closing Excel) and when he switches back to Excel again. I need to know this to stop a timer tri

I'm lookin开发者_JS百科g for an event which gets called when the user switches to another app (without closing Excel) and when he switches back to Excel again. I need to know this to stop a timer triggered event which isn't necessary when app isn't active or nothing is changed.

I would use it to implement an autosave-method which shouldn't save the file if its in the background and therefore not been changed.


There's an excellent article on how to find what events are available here http://www.cpearson.com/excel/Events.aspx "Open the Object Browser in the VBA Editor (press F2 or choose Object Browser from the View menu. In the Classes list, scroll down and select Worksheet. Right-click anywhere in the primary window and choose Group Members on the pop up menu. Then scroll down in the Members Of "Worksheet" list until you see items with yellow lightening bolts next to them. These are the events for the Worksheet objects. Do the same for the Workbook and Application objects."


I am not sure whether you will be able to find EXCEL events that capture your needs, as jumping to another application is not an EXCEL mechanism, but a general Windows mechanism. In fact you try to capture a kind of "LostFocus" event of the Excel application itself. Excel however gives you only event triggers for events that are "local to the Excel application".

What about calling your external program by pressing an Excel Button which contains code to

  • set a global status variable to "I JUMPED OFF"
  • stop the timer
  • start the external application (Shell or similar), minimize the window or whatever

plus a global Selection_Change trigger that

  • examines the status variable, and if it was set to "I JUMPED OFF"
  • set it back to "I AM HERE"
  • restart the timer

Of course this is a bit weak and the user can trick you out by simply manoevering with the mouse or Alt-TAB, but if you maximize the window on Startup you can motivate the user to use the Buttons.

0

精彩评论

暂无评论...
验证码 换一张
取 消