0
votes

I have a VSTO application that has multiple groups and buttons on the ribbon. When a user clicks on a button a specific form is loaded on Excel. It looks like when the form is open a user is unable to open another instance of Excel unless the form is closed in the first instance. Is there a way to seperate the Add-In from different instances?

I have code developed which uses getVisbility call back to decide whether to show the tab in the ribbon based on a specific workbook. However this doesnt allow users to use multiple Excel instances whilst a windows form is open. As soon as I close the form - a new instance of excel has been opened. The VSTO Excel tool has been developed on application level.

MainRibbon.xml

  <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns" label="MDS" insertBeforeMso="TabHome" getVisible="setVisbility" >
        <group id="CreateLoadModel"
               label="Create/Load Model">
          <button id="createmodelbutton" label="Create New Model"
             screentip="Text" onAction="OnCreateModel"
             supertip="Create a new Model"
             imageMso="GroupSmartArtCreateGraphic"/>
        <button id="loadmodelbutton" label="Load Existing Model"
             screentip="Text" onAction="OnLoadModel"
             supertip="Load an Exisitng Model"
             imageMso="FileOpen"/>
        </group>

MainRibbon.cs

        public bool setVisbility(Office.IRibbonControl control)
    {
        int nWorkbooks = Globals.ThisAddIn.Application.Workbooks.Count;
        if (nWorkbooks == 0)
        {
            return false;
        }

        if (Globals.ThisAddIn.Application.ActiveWorkbook != null && Globals.ThisAddIn.Application.ActiveWorkbook.Name == "MDS.xlsm")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

AddIn.cs

    private void ThisAddIn_Startup(object sender, EventArgs e)
    {
        this.Application.WorkbookActivate += Application_WorkbookActivate;
        this.Application.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);

    }

    private void Application_WorkbookActivate(Workbook Wb)
    {
        MainRibbon.ribbon.Invalidate();
    }
1
Are these truly other instances, or are these workbook windows open in the same instance? I suspect the latter. Excel (and other Office applications) run multiple windows within the same application instance. Look in the Task Manager to see how many instances of excel.exe are listed. Or in Excel's Windows menu under the View tab.Cindy Meister
Sorry these are workbook windows open in the same instance! Is there still a way for a user to open excel workbook without it being affected by the Windows form in another workbook? If a user had the form open but wants to check another workbook at the same time at present the user can’t do this without closing the form?roa765
Can you provide a code snippet of how you show the form to the user?STHOH

1 Answers

1
votes

If you open a modal dialog box in an VSTO button event handler, the Excel main thread will be blocked and Excel will not response to user input messages.

This includes also trying to open another Workbook. This behavior is similar to opening "Format Cells" dialog for example.

Solutions:

1) A quick workaround that does not require programming is clicking ALT button when trying to open a new workbook. Excel will show the message asking you to open a new instance of Excel.

2) Another approach is opening a modalless dialog in c# (using Show function instead of ShowDialog). This is similar to Find/Replace window of Excel.

Please look at the following example for more details and how to get a result from such dialog. https://www.codeproject.com/Articles/27010/Modal-and-Modeless-Dialog-Box-in-C