locked
Excel 2007 VSTO add-in causes error during edit of embedded Excel data in Word document RRS feed

  • Question

  • Hello Everyone, I am working on the implementation of an Excel 2007 VSTO add-in but I am encountering a problem I don't know how to solve.  I created a simple add-in and during the add in start-up I am subscribing to the Excel WorkbookOpen event.  Now, I run the project in Visual Studio and it opens an instance of Excel.  I open a Word document with an embedded Excel table (the excel range was added to the Word document via Paste -> Paste Special -> Microsoft Office Excel Worksheet Object).  When I double-click the Excel table in the Word document I get the following error dialog: "The program used to create this object is Excel.  That program is not installed on your computer.  To edit this object, you must install a program that can open the object".  Now, if I comment-out the subscribing to the Workbook Open event in the add-in and re-run it, I am able to edit the Excel data in the Word document without problems.  Here is the add-in code.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    using Microsoft.Office.Tools.Excel;
    using System.Windows.Forms.Integration;
    
    namespace ExcelAddinTestEmbeddedError{
      public partial class ThisAddIn {
    
        private Excel.AppEvents_WorkbookOpenEventHandler openWbH;            //event handler for opening a new Excel workbook
    
        private void ThisAddIn_Startup(object sender, System.EventArgs e){
          Excel.Application app = this.Application;
    
          // subscribe to the workbook opened event
          openWbH = new Excel.AppEvents_WorkbookOpenEventHandler(app_WorkbookOpen);
          app.WorkbookOpen += openWbH;
        }
    
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e){
    
          ///Cleanup Stuff
          Excel.Application app = this.Application;
          
          // cnx subscription to the workbook opened event
          app.WorkbookOpen -= openWbH;
          openWbH = null;
        }
        
        /// <summary>
        /// </summary>
        /// <param name="Wb"></param>
        void app_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook Wb) {
    
        }
    
        #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
      }
    }
    
    

     

    I would appreciate any help.  Thanks.

    Monday, October 11, 2010 10:20 PM

Answers

All replies

  • Hello erikra,

    Thanks for posting. For this issue, please use the code as below. As far as I see, it behaviors the same as yours.

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {           

                this.Application.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);

     

            }

     

            void Application_WorkbookOpen(Excel.Workbook Wb)

            {

                //throw new NotImplementedException();

            }

    By running this add-in, now this issue also happens? Which version of Visual Studio in this development machine?

    I am looking forward to you reply. Have a nice day.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, October 13, 2010 7:50 AM
  • Hi Erika / Bessie

    See the discussions that are listed in this search result:

    http://social.msdn.microsoft.com/Search/en-US/?query=OLE%20Whitechapel%20Cindy&rq=meta:Search.MSForums.ForumID(0e69520a-0af0-4085-a8c4-c5f21ce20e01)+site:microsoft.com&rn=Visual+Studio+Tools+for+Office+Forum

    There have been numerous discussions on the point of COM Add-ins running when Office apps are embedded as OLE objects. Basically, it doesn't work, but can be avoided to a certain extent. Pay special attendtion to answers by Andrew Whitechapel, Geoff Darst and Tim Li.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by erikra Friday, October 15, 2010 3:27 PM
    Wednesday, October 13, 2010 8:05 AM
  • Thanks Bessie and Cindy for your responses.

    From the discussions in the search results referenced, I understand the problems resulting from COM Add-ins when Office apps are embedded as OLE objects.  However, my problem is that I don’t want the features of my add-in available to embedded Excel tables, however because the add-in gets initiated and the Workbook Open event gets registered I see the error message.  I see this error in two scenarios:

    1.)    A user opens a Word document with an embedded Excel OLE object.  Excel will run, my add-in gets initiated, and the error is displayed because in the startup method of my add-in I add an event handler for the WorkbookOpen event.

    a.       I can get around this issue by adding a checkbox to the ribbon that the user must click, which hides/shows my add-in task pane, and I register the event handlers after the user has clicked the checkbox, not at startup, therefore I am able to avoid this error message.

    2.)    A user currently has my add-in open and enabled, therefore the event handler has been registered.  Now, the user opens a Word document with embedded Excel data and attempts to edit data in en embedded Excel table.  Because my Excel add-in is currently running, that Excel application instance is used by the embedded OLE object and the error message is displayed.  I looked into the UserControl property but it is always true in this scenario.  Do you know how I can get around this problem? Or will I need to inform the users of my add-in that they must first close the Excel application with my add-in running before attempting to edit Excel data embedded in other Office applications.

    Again, thanks for your help.

    Wednesday, October 13, 2010 9:25 PM
  • Hi Erikra

    <<2.)    A user currently has my add-in open and enabled, therefore the event handler has been registered.  Now, the user opens a Word document with embedded Excel data and attempts to edit data in en embedded Excel table.  Because my Excel add-in is currently running, that Excel application instance is used by the embedded OLE object and the error message is displayed.  I looked into the UserControl property but it is always true in this scenario.  Do you know how I can get around this problem? Or will I need to inform the users of my add-in that they must first close the Excel application with my add-in running before attempting to edit Excel data embedded in other Office applications.>>

    The only possibility that occurs to me is that you unlink the event when the Excel window loses the focus (the Application.WindowDeactivate event), and link back up when it gets the focus (WindowActivate)?


    Cindy Meister, VSTO/Word MVP
    Thursday, October 14, 2010 7:30 AM
  • Hello again erikra,

    Actually, as far as I know, besides what you side, there is no a direct way to work around this issue. However, I think Cindy may have some good suggestion for you last question. Thanks,

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, October 14, 2010 7:34 AM
  • Thanks again Bessie and Cindy for your responses.  Now that I know what is going on, I will work around these issues.
    Friday, October 15, 2010 3:27 PM
  • Hallo, erikra,

    thanks for your explanation. I have tried with my addin with Solution 2, it doesn't work.

    Solution 1 is the way for me. But I can't add CheckBox in UI because my customer will not be satisfied. I try to use Win32API to write some condition to avoid registering event handler in this case.

    Thanks.

    Tuesday, September 1, 2015 11:18 AM