Reputation: 295
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();
}
Upvotes: 0
Views: 1119
Reputation: 6103
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
Upvotes: 1