Excel VSTO加载项:无法在MessageBox.Show(“Test”)后重新激活Excel;

我正在实现一个显示一些模式对话框的VSTO Excel加载项。 这些对话框在Windows任务栏中不显示为自己的窗口。 但是在某些情况下,这些对话框不是从Excel顶端消失,不能通过使用任务栏回来!


整个故事(用Windows XP – 7,Excel 2007 – 2010重现):

  1. 我打开Excel并创build两个或更多新的工作簿
  2. 我显示一个模式对话框,让我们说通过“MessageBox.Show”我打开“记事本”,并最大化其窗口
  3. 我尝试通过Windows任务栏重新激活其中一个Excel工作簿窗口
  4. 我期望:Excel将拿出我的模态MessageBox出现在上面
  5. 实际结果是:当您单击Windows任务栏中的工作簿项时,MessageBox或任何Excel工作簿都不会出现!

为什么???

我可以通过Ctrl + Tab重新激活Excel。 然后我的模式对话框/ MessageBox正确的在上面。 如果您有Visual Studio和Excel,则很容易重现。 请帮忙!

问候,约尔格


代码示例:

  1. 只需创build一个空的Visual C#/ Office / 2010 / Office 2010加载项
  2. 用下面的代码replace“ThisAddIn.cs”的内容:

namespace ExcelAddIn6 { public partial class ThisAddIn { private void ThisAddIn_Startup(object sender, System.EventArgs e) { NativeWindow excelWindowThatIsTheOwner = null; try { //Get excel main window to set owner (however does not help anyway) excelWindowThatIsTheOwner = new NativeWindow(); excelWindowThatIsTheOwner.AssignHandle(new IntPtr(Globals.ThisAddIn.Application.Hwnd)); //Create two more workbooks to have more than one... Globals.ThisAddIn.Application.Workbooks.Add(); Globals.ThisAddIn.Application.Workbooks.Add(); Globals.ThisAddIn.Application.WindowState = Excel.XlWindowState.xlMaximized; //Show modal dialog (here: a message box, but ) MessageBox.Show(owner: excelWindowThatIsTheOwner, text: "I am a modal MessageBox.\nNow bring another application to the foreground and then try to bring excel back via the windows taskbar..."); //Problem stays the same for modal forms var myForm = new Form(); myForm.ShowInTaskbar = false; myForm.ShowDialog(excelWindowThatIsTheOwner); } finally { //Cleanup if (excelWindowThatIsTheOwner != null) excelWindowThatIsTheOwner.ReleaseHandle(); } } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { } #region VSTO generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion }