如何通过Excel的C#实例迭代 [英] How to iterate through instance of Excel c#
问题描述
我可以在内存使用Marshal.GetActiveObject访问Excel的一个实例。但是,这总是返回现存的最古老的实例。
I can access an instance of Excel in memory using Marshal.GetActiveObject. But this always returns the oldest existing instance.
我想重复,虽然所有实例,能够选择一个链接。
I would like to iterate though all instances and be able to choose the one to link to.
任何人都可以用这个吧。
Can anyone help with this please.
推荐答案
试试这个。
List<Process> procs = new List<Process>();
procs.AddRange(Process.GetProcessesByName("excel"));
编辑:
有,在的 http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514的.aspx 。 GetActiveObject将总是从表返回的第一个对象。这是因为办公室没有注册新的对象。你必须获得子窗口的应用程序
There is an article that fully implements this at http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx. GetActiveObject will always return the first object from the table. This is because Office doesn't register new objects. You have to get the application from the child windows.
编辑:
这是对我工作的代码
This is the code that worked for me.
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("Oleacc.dll")]
public static extern int AccessibleObjectFromWindow(
int hwnd, uint dwObjectID, byte[] riid,
ref Microsoft.Office.Interop.Excel.Window ptr);
public delegate bool EnumChildCallback(int hwnd, ref int lParam);
[DllImport("User32.dll")]
public static extern bool EnumChildWindows(
int hWndParent, EnumChildCallback lpEnumFunc,
ref int lParam);
[DllImport("User32.dll")]
public static extern int GetClassName(
int hWnd, StringBuilder lpClassName, int nMaxCount);
public static bool EnumChildProc(int hwndChild, ref int lParam)
{
StringBuilder buf = new StringBuilder(128);
GetClassName(hwndChild, buf, 128);
if (buf.ToString() == "EXCEL7")
{
lParam = hwndChild;
return false;
}
return true;
}
static void Main(string[] args)
{
Excel.Application app = new Excel.Application();
EnumChildCallback cb;
List<Process> procs = new List<Process>();
procs.AddRange(Process.GetProcessesByName("excel"));
foreach (Process p in procs)
{
if ((int)p.MainWindowHandle > 0)
{
int childWindow = 0;
cb = new EnumChildCallback(EnumChildProc);
EnumChildWindows((int)p.MainWindowHandle, cb, ref childWindow);
if (childWindow > 0)
{
const uint OBJID_NATIVEOM = 0xFFFFFFF0;
Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
Excel.Window window = null;
int res = AccessibleObjectFromWindow(childWindow, OBJID_NATIVEOM, IID_IDispatch.ToByteArray(), ref window);
if (res >= 0)
{
app = window.Application;
Console.WriteLine(app.Name);
}
}
}
}
}
}
这篇关于如何通过Excel的C#实例迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!