none
Excel Add-In with WorkbookOpen event causes error in Word RRS feed

  • Question

  • EDIT: New information

    I have narrowed the problem to a specific code. Following code produces the error message (see below in the original part):

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Globals.ThisAddIn.Application.WorkbookOpen += Application_WorkbookOpen;
        }
    
        void Application_WorkbookOpen(Excel.Workbook Wb)
        {
            int i = 0;
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
    
        #region VSTO generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
    
        #endregion
    }
    

    It does not matter whether I unregister the WorkBookOpen event in the ThisAddIn_Shutdown method or not. Whenever this piece of code is present Word gives me the error.

    It seems to be a bug in Excel. Can anyone test and confirm my assumption? Does anyone know a workaround?

    ORIGINAL

    I have a problem with my Excel add-in (VSTO) in a very specific situation. For some reason I can't debug the add-in when it is happening.

    In Word it is possible to insert and edit OLE objects. In this specific situation a user wants to insert and edit an Excel sheet (INSERT -> Table -> Excel Spreadsheet).

    Let's say that Excel is not running. In an ideal world the Excel process is started by Word when the user edits the spreadsheet. The Excel process is terminated when the Word document is closed. This works fine, if my Excel add-in is deactivated.

    If the Excel add-in is activated the user cannot edit the spreadsheet in Word. An error with following message is shown within Word: "The program used to create this object is Excel. That program is either not installed on your computer or it is not responding. To edit this object, install Excel or ensure that any dialog boxes in Excel are closed". Also the Excel process which was started by Word is not terminated when closing the document or exiting Word. Excel started by the user does not give any error message. No dialog is opened. The add-in works fine. So there must be an error with the Excel add-in when Excel is started by Word.

    I can reproduce the error with and without an attached debugger (Visual Studio). But for some reason there is no exception caught by Visual Studio while the error is happening.

    Can anyone help me how to investigate on that error?

    Yavuz


    • Edited by Yavuz T Tuesday, January 20, 2015 3:02 PM new insights
    Monday, January 19, 2015 3:48 PM

Answers

  • Hi Yavuz,

    Based on my investigatiton, when the Excel doesn't start up by user the add-in would shut down immediately. So when the workbook open event was fired the callback already be relreased.

    As a workaround, we can use application.UserControl to detect whether the Excel applicaiton was started by user. And here is the code for your reference:

       private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
              
                if(Application.UserControl)
                {
                    
                    Globals.ThisAddIn.Application.WorkbookOpen += Application_WorkbookOpen;
                }
            }
    

    Please let me know whether it works for you.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Yavuz T Thursday, January 22, 2015 1:29 PM
    Thursday, January 22, 2015 3:00 AM
    Moderator

All replies

  • Hi Yavuz,

    Based on my investigatiton, when the Excel doesn't start up by user the add-in would shut down immediately. So when the workbook open event was fired the callback already be relreased.

    As a workaround, we can use application.UserControl to detect whether the Excel applicaiton was started by user. And here is the code for your reference:

       private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
              
                if(Application.UserControl)
                {
                    
                    Globals.ThisAddIn.Application.WorkbookOpen += Application_WorkbookOpen;
                }
            }
    

    Please let me know whether it works for you.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Yavuz T Thursday, January 22, 2015 1:29 PM
    Thursday, January 22, 2015 3:00 AM
    Moderator
  • Hi Fei,

    Application.UserControl was the solution. Thank You!

    Yavuz

    Thursday, January 22, 2015 1:29 PM