Bringing focus back to the active sheet after alt tabing to another application RRS feed

  • Question

  • Hi,

    I have a very peculiar scenario here.

    I have created a VSTO application using Excel 2007 and VS 2008.

    My scenario is that user can perform some actions in a workbook(Workbook1) and when he tries to move out of that workbook by opening another new/existing workbook(Workbook2), i create a hiddensheet in the Workbook1 and then put the focus back on the previously active sheet and then go to the new workbook.So i did this part in the Workbook Deactivate event. I assume this is the event which gets triggered when workbook loses focus.

    The problem is when i come back to Workbook1, i see the focus is not on the previously active sheet, but it is now on the same number of sheet, that was there in workbook2. So for example if workbook2 had sheet2 open, then when i Alt+Tab to workbook1, sheet2 would be active on workbook1. And if there are more number of worksheets on workbook2, then the focus goes the last sheet on Workbook1.

    This is weird. When i debug, i notice that the Workbook deactivate event is actually showing worksheets of the new Workbook and not of the old workbook. So that is probably the reason why my setting of the Activesheet is not working.

    I doubt how many of you understood what i am trying to say there, but if you did, can you please suggest some ideas.




    Thursday, December 22, 2011 8:59 PM


  • The behavior is logical because the Workbook Deacativate event actually runs twice.  The first time the user leaves Workbook 1 and opens Workbook 2 and then it runs again when the user leaves Workbook 2 and returns to Workbook1.  So you're probably writing over your stored information.

    What you're going to have to also use is the Workbook Activate event and design an array that contains each Workbook name and last active worksheet.  This shared array gets added to in the Workbook Deactive event and upon execution of the Workbook Activate event the array is searched for the Workbook name and last sheet.

    Hope that helps

    Kind Regards, Rich ...
    Friday, December 23, 2011 2:48 AM