none
Excel is not ready when Addin Shutdown is invoked from ComAddins Dialog. How do i cleanup? RRS feed

  • Question

  • I have a VSTO Excel addin that must close some loaded files when it's shutting down.

    However when the shutdown is invoked from the ComAddins (modal!) dialog the application is not ready and my code fails. To reproduce, create a new Excel 20xx Addin and copy following code, optionally sign, and run.

    Public Class ThisAddIn
    
        Dim wkb As Excel.Workbook
    
        Private Sub ThisAddIn_Startup() Handles Me.Startup
            wkb = Application.Workbooks.Add
        End Sub
    
        Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
            If wkb IsNot Nothing Then
                wkb.Saved = True
                wkb.Close(False)
            End If
        End Sub
    
    End Class

    Now goto Developer Tab: ComAddins and Uncheck the addin. Code will fail as the modal dialog is not yet dismissed and the application is not ready.

    I've tried to create a separate cleanup thread but failed. I now resort to an API timer in the workbook that requires closing, which checks the ComAddins("xx").Connect and closes the workbook when it finds it false.

    Any suggestions?



    • Edited by volkerink Monday, July 8, 2013 9:00 AM
    Monday, July 8, 2013 8:58 AM

All replies

  • Hi volkerink,

    Thank you for posting in the MSDN Forum.

    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 9, 2013 1:52 PM
    Moderator
  • Hi Volkerink,

    I have re-produced your issue on my lab machine successfully.

    Here is figure to help us understand this issue:

    Actually, you will get “Exception from HRESULT: 0x800AC472” when you try to manipulate a COM object in “Addin_Shutdown” function. Because “COM Add-Ins” is a modal dialog, and you are not able to manipulate a Wookbook before it is closed. For more details about this exception, please refer HRESULT 800ac472 from set operations in Excel.

    To resolve this kind of issue, we need to start a new thread to wait the modal dialog being closed. But in this case, it could be more complex.

    The Office Add-in is running on an isolated AppDomain, when you uncheck this add-in in “COM Add-Ins” dialog, this AppDomain will be unloaded, it means that all threads run in this domain will be aborted. As you can see from the figure above, when the new thread is waiting for the dialog being closed, the Add-ins AppDomain is unloaded.

    Consider the user may uncheck a running Add-ins, Addin_Shutdown is not a good place to manipulate COM objects. Could you provide us more details so that we can see whether there is a chance to close the workbook in a better place?

    Thursday, July 11, 2013 10:19 AM
    Moderator
  • Thanks for responding, not for the response ;-)

    "Not a good place to manipulate ComObjects" 
    LOL.  VSTO is intended to manipulate comobjects... as that's all we can do with office...

    My app manipulates workbooks and does cleanup when the UI closes. Depending on Excel versions my UI is either a CustomTaskPane or a modeless WPF Window on it's own STA thread. Total app has around 10k lines of code.

    I monitor workbook and window close events to prevent user error and do cleanup... (lot more complex since xl2013)

    IMHO it's a plain and simple bug in Excel, and it's been there ever since ComAddins were introduced.. Word does it as expected, but Excel calls the IDTExtensibility's OnDisconnect when it hasn't dismissed the modal dialog.
    Such a simple thing to fix for the Excel team.

    I have a modeless UI. I don't get any warning of impending close prior to the ONShutdown (which by VSTO will be raised only AFTER taskpanes and ribbons have been disposed, but I've fixed that)
    When the OnShutDown occurs due to user deactivating the app from the ComAddins dialog I'm up "the" creek without a paddle.

    Is it possible to trap the ComAddins routedcommand? If so: how?

    Regards,
    Jurgen Volkerink


    UPDATE: I've repurposed the ComAddInsDialog command..  it aint' pretty but it serves my purpose.
    Thursday, July 11, 2013 11:01 AM
  • Hi Volkerink,

    Sorry for the inconvenience brought to you and very glad to hear that you have resolved this issue. If you can share your own solution on this issue that will be a huge help to others who encounter similar issues.

    Regards,

    Jeffrey

    Saturday, July 13, 2013 2:27 AM
    Moderator
  • Jeffrey,

    Solution? What solution?
    Did you file a bug report and did the "Excel Team" repair the bug? Excluding all the overhead the repair should probably take a coder a few minutes...  

    I know it's microsoft policy to never call a bug a bug unless it actually crashes the application. Your answer just proves it: see the word "issue". 

    Mine's not a solution. It's a workaround. And it's already shared, probably with the NSA too. But my own post cannot be proposed as an answer to my own question.

    Regards, Jurgen



    Saturday, July 13, 2013 8:20 AM