Excel 互操作:恢复最小化窗口
在我通过 com 互操作与 Excel 交互的 winforms 应用程序中,
我尝试附加到现有的 Excel 进程(如果有)。获取对象似乎效果很好,但如果 Excel 应用程序最小化(这在我的用例中很可能),我无法恢复窗口并将其置于前面。
我已经尝试过以下陈述:
try
app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
catch (Exception) { /* ignore */ }
if (app == null)
{
app = new Excel.Application();
app.Visible = true;
}
if (app.ActiveWindow.WindowState == Excel.XlWindowState.xlMinimized)
app.ActiveWindow.WindowState = Excel.XlWindowState.xlNormal;
wb = ...
wb.Activate();
这些都没有任何效果。我怎样才能做到这一点?
(请注意:我的问题与存在现有实例的情况有关,因此不需要“Visible = true”并且此线程不适用。)
In my winforms app interacting with Excel through the com interop,
I'm trying to attach to an existing Excel process if there is one. Getting the object seems to work well, but if the Excel application is minimized (which is quite likely in my use case), I don't manage to restore the window and bring it to the front.
I've tried the following statements:
try
app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
catch (Exception) { /* ignore */ }
if (app == null)
{
app = new Excel.Application();
app.Visible = true;
}
if (app.ActiveWindow.WindowState == Excel.XlWindowState.xlMinimized)
app.ActiveWindow.WindowState = Excel.XlWindowState.xlNormal;
wb = ...
wb.Activate();
None of these had any effect. How can I achieve that?
(Please Note: My problem relates to the case when there is an existing instance, so the "Visible = true" is not necessary and this thread does not apply.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
app.WindowState = xlNormal
因为app.ActiveWindow
是当前工作表而不是应用程序实例主窗口。You want
app.WindowState = xlNormal
asapp.ActiveWindow
is the current sheet not the application instances main window.