简体   繁体   English

VSTO 功能区按钮打开 Windows 窗体但无法在不关闭窗体的情况下使用另一个 Excel 实例 C#

[英]VSTO Ribbon Button Opens Windows Form But Unable to Use another Instance of Excel without closing the form C#

I have a VSTO application that has multiple groups and buttons on the ribbon.我有一个 VSTO 应用程序,它在功能区上有多个组和按钮。 When a user clicks on a button a specific form is loaded on Excel.当用户单击一个按钮时,一个特定的表单会加载到 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.看起来当表单打开时,用户无法打开 Excel 的另一个实例,除非表单在第一个实例中关闭。 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.我开发了代码,它使用 getVisbility 回调来决定是否根据特定工作簿在功能区中显示选项卡。 However this doesnt allow users to use multiple Excel instances whilst a windows form is open.但是,这不允许用户在 Windows 窗体打开时使用多个 Excel 实例。 As soon as I close the form - a new instance of excel has been opened.我一关闭表单 - 一个新的 excel 实例已经打开。 The VSTO Excel tool has been developed on application level. VSTO Excel 工具是在应用程序级别开发的。

MainRibbon.xml主功能区.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();
    }

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.如果在 VSTO 按钮事件处理程序中打开模式对话框,Excel 主线程将被阻塞,Excel 将不会响应用户输入消息。

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. 1) 一种不需要编程的快速解决方法是在尝试打开新工作簿时单击 ALT 按钮。 Excel will show the message asking you to open a new instance of Excel. Excel 将显示消息,要求您打开 Excel 的新实例。

2) Another approach is opening a modalless dialog in c# (using Show function instead of ShowDialog). 2)另一种方法是在 c# 中打开一个无模态对话框(使用Show函数而不是 ShowDialog)。 This is similar to Find/Replace window of Excel.这类似于 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 https://www.codeproject.com/Articles/27010/Modal-and-Modeless-Dialog-Box-in-C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM