当I按文件->退出(或Alt+F4)或X时,我会得到"Do you want to -> changes“对话框的提示,其中我可以选择"Cancel”。因此Excel可以继续,但我的excel-dna外接程序再也不会收到通知,并且会被卸载。



提前感谢,伙计们!
发布于 2019-04-09 01:12:18
阿曼多!非常感谢你的帮助!
我发现有一个IDTExtensibility2接口,它托管外接程序发生的事件通知,比如加载、卸载、更新等等。因此,我将该接口与名称空间ExcelDna.Integration中的ExcelComAddIn类一起使用:
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()将不会运行,并且我的窗格仍然在那里!
无论如何,你的方法非常酷,如果不是你的帮助,我从来没有机会弄清楚发生了什么,谢谢你让我走上了找到这个解决方案的轨道。
发布于 2019-04-06 02:36:40
据我所知,这个问题是在用户单击cancel之前执行WorkbookBeforeClose时发生的。有一些替代方案。
最简单的方法是可以在WorkbookBeforeClose事件中保存活动的woorkbook ( woorkbook对象有一个save和saveas方法)。由于工作簿已经保存,保存对话框不会显示,用户也不会单击cancel按钮。
另一种解决方案是调用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);
}希望这能帮上忙,阿曼多
https://stackoverflow.com/questions/55465438
复制相似问题