2012-07-25 90 views
1

如何VSTO C# 我一直在寻找,但没有任何成功将焦点设置Excel应用程序

+0

看看这个[SO接听](http://stackoverflow.com/questions/795059/vsto-application-focus)帮助。并看看这个[链接](http://www.add-in-express.com/forum/read.php?FID=1&TID=3616) – 2012-07-25 05:07:06

回答

1

试试这个代码

Process[] processes = Process.GetProcessesByName("excel"); 
foreach (Process p in processes) 
{ 
    if (p.MainWindowTitle.Contains(fileName.Substring(fileName.LastIndexOf("/") + 1))) 
    { 
     SetForegroundWindow(p.MainWindowHandle); 
    } 
} 
0

中的解决方案将焦点设置Excel应用程序对象我的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的主窗口。

问候, 约尔格