当选择“是否要保存提示”取消按钮时,Excel-DNA加载项将卸载

问题描述 投票:1回答:2

当我按下文件 - >退出(或Alt + F4)或X时,系统会提示“是否要保存更改”对话框,我可以选择“取消”。所以Excel可以继续,但我的excel-dna加载项永远不会再次通知并且被卸载。

Image 1

Image 2

Image 3

先谢谢你们!

c# html excel wpf excel-dna
2个回答
1
投票

阿曼多!非常感谢你的帮助!

我发现有一个IDTExtensibility2接口可以承载加载项发生的事件通知,例如加载,卸载,更新等等。所以我在命名空间ExcelDna.Integration中使用ExcelComAddIn Class的接口:

public class ExcelComAddIn : IDTExtensibility2
{
    public ExcelComAddIn();

    protected string ProgId { get; }

    public virtual void OnAddInsUpdate(ref Array custom);
    public virtual void OnBeginShutdown(ref Array custom);
    public virtual void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom);
    public virtual void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom);
    public virtual void OnStartupComplete(ref Array custom);
}

我注意到OnBeginShutdown()方法在对话框提示后运行!这就是我想要的,所以我摆脱了WorkbookBeforeClose事件,我重写了OnBeginShutdown()方法,并将我在WorkbookBeforeClose事件中的代码放入OnBeginShutdown(),如下所示:

public override void OnBeginShutdown(ref Array custom)
{
    base.OnBeginShutdown(ref custom);

    //I PUT MY CUSTOM CODE HERE:
    CloseAllPanes();

    ExcelTaskExecutor.Destroy();
}

现在,如果用户选择在保存对话框中单击“取消”,则OnBeginShutdown()不会运行,我的窗格仍然存在!

无论如何,你的方法非常酷,如果不是你的帮助,我从来没有机会弄清楚发生了什么,并感谢让我走上正轨找到这个解决方案。


0
投票

据我所知,在用户单击取消之前执行WorkbookBeforeClose时会出现问题。有一些替代方案。

简单的一点就是你可以将活动的woorkbook保存在WorkbookBeforeClose事件中(woorkbook对象有一个方法save和saveas save)。由于已保存工作簿,因此不会显示保存对话框,用户也不会单击取消按钮。

另一种解决方案是在WorkbookBeforeClose中调用自定义保存对话框。我在其他项目中使用了以下代码。 WorkbookBeforeClose可能如下所示:

  private void ActiveWorkbook_BeforeClose(ref bool Cancel)
    {
        DefaultSaveExcel(Excel.ActiveWorkbook,ref Cancel);

        if (!Cancel)
        {
            //if enters here is because the workbook is actually closing
            Delete(Excel.ActiveWorkbook.Name);
        }                     
    }

实际的DefaultSaveExcel实现可能如下所示:

 public  void DefaultSaveExcel(Workbook wb, ref bool Cancel)
    {
        while (wb.Saved == false && Cancel == false)
        {
            var result = ShowMessageDialogSave();

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                var sa = CreateExcelSaveDialog(wb.FullName);

                if (sa.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    wb.SaveAs(sa.FileName);
                    wb.Save();

                }
            }
            else if (result == System.Windows.Forms.DialogResult.No)
            {
                wb.Saved = true;
            }
            else if (result == System.Windows.Forms.DialogResult.Cancel)
            {
                Cancel = true;
            }
        }
    }

    public  System.Windows.Forms.SaveFileDialog CreateExcelSaveDialog(string name = null)
    {
        var sa = new System.Windows.Forms.SaveFileDialog();
        sa.Filter = "Excel files (*.xlsx)|*.xlsx";
        if (!string.IsNullOrEmpty(name))
            sa.FileName = name;
        sa.CreatePrompt = true;

        return sa;
    }

    public  DialogResult ShowMessageDialogSave()
    {
        var app = (Microsoft.Office.Interop.Excel.Application)ExcelDna.Integration.ExcelDnaUtil.Application;
        NativeWindow xlMain = new NativeWindow();
        xlMain.AssignHandle(new IntPtr(app.Hwnd));

        var message = "Do you want to save pending changes?";

        if (Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName.ToLower() == "es")
            message = "¿Desea guardar los cambios pendientes?";


        return System.Windows.Forms.MessageBox.Show(xlMain, message, "Microsoft Excel", System.Windows.Forms.MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1);
    }

希望这有帮助,阿曼多

© www.soinside.com 2019 - 2024. All rights reserved.