none
Prevent users activating other workbook (SDI) without using events RRS feed

  • Question

  • Hi,

    We are performing things in VSTO which takes some time, now we want to prevent users to activate another workbook to prevent the occurence of errors. Important to realize is that we can not activate the events (then it would be easy). 

    Now the reason for this is ofcourse the fact that when we are performing things in code, we relate this to the current workbook, if the user activates another workbook (in the current Excel Session) then the code will continue but refer to the wrong workbook which can cause errors.

    Please note the code is long and complex so we can not build in checks if we are in the right workbook.

    Sofar I could not find a 'simple' solution :).

    tnx!

    Arie

    Wednesday, June 13, 2018 6:16 AM

All replies

  • Hello ArieSmit,

    As far as I know, we could not prevent user from activating another workbook without using events. Excel application is a single thread processor and it will prevent use operating on Excel while the code running by default.

    Have you handle it to give control to user to operate on Excel while the code running? If so, i would suggest you cancel the design and just make a friendly message to let user know you are operating on the excel and suggest user be patient..etc.

    Besides, if you are using ActiveWorkbook to get the workbook now, you could create a new workbook object and then set the ActiveWorkbook to the new workbook object. So the new workbook object will always refer to the recorded workbook. Event if you changed current activate workbook, the new workbook object will still refer to the previous workbook. You could use the new workbook instead of ActiveWorkbook in your code to avoid exception due to activating another workbook.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 14, 2018 2:00 AM
  • Hi Terry,

    sorry for the late reply.

    It is basically a complex proces which is kicked of, which can take a while. It is ofcourse possile to give a message but there are users who still activate the other workbook.

    To use the Activeworkbook as new object; we do use that, but we regularly need to activate cells swich workbooks etc. so this is not really an option. This because we basically need to activate the "ActiveWorkbook" object everywhere in the code. Also we use functions directly called from the application (such as xl.columns, selection.locked etc etc), so we need to check the active workbook as well.

    best regards,

    Arie

    Monday, June 25, 2018 1:01 PM