如何在vsto C#中设置对Excel Application对象的关注 我一直在寻找它,但没有任何成功
答案 0 :(得分:1)
试试此代码
Process[] processes = Process.GetProcessesByName("excel");
foreach (Process p in processes)
{
if (p.MainWindowTitle.Contains(fileName.Substring(fileName.LastIndexOf("/") + 1)))
{
SetForegroundWindow(p.MainWindowHandle);
}
}
答案 1 :(得分:0)
我的VSTO应用程序中的解决方案是:
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);
/// <summary>
/// Gets the main excel window.
/// </summary>
/// <returns>An ArbitraryWindow that represents the main excel window.</returns>
public static ArbitraryWindow GetMainExcelWindow()
{
var excelHwnd_IntPtr = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
var excelWindow = new ArbitraryWindow(excelHwnd_IntPtr);
return excelWindow;
}
/// <summary>
/// Activates the excel window.
/// </summary>
public static void ActivateExcelWindow()
{
var currentlyActiveForm = Form.ActiveForm;
//if (currentlyActiveForm != null && currentlyActiveForm.GetType() == typeof(FormProgress)) return;
var handle = GetMainExcelWindow().Handle;
BringWindowToTop(handle);
}
只需调用“ActivateExcelWindow()”即可激活Excel主窗口。
此致 约尔格