在后期绑定(bind)方面得到一点帮助之后。
我正在尝试延迟绑定(bind) excel,我没有遇到任何问题。只有当我打开多个 excel 实例时,我才会遇到一些问题。
我希望能够确定要绑定(bind)到的 excel 实例(以及链接事件等)。主要原因是我有一个从第三方工具打开 excel 文档的应用程序,并且没有处理事件。我希望能够利用我知道打开的特定 excel 实例来捕获事件。唯一的问题是 excel 是否已被用户打开(无论如何打开)。
如果在绑定(bind)后打开 excel,显然,我没有遇到问题。只有当 excel 已经打开时。
似乎绑定(bind)是对第一个打开的实例完成的。
这是实际的代码:
Type excelType = Type.GetTypeFromProgID("Excel.Application");
// Throw exception if the type wasn't found
if (excelType == null)
throw new Exception(error);
//Get the Excel.Application Type by creating a new type instance
excelApplication = Marshal.GetActiveObject("Excel.Application");
//Throw exception if the object couldn't be created
if (excelApplication == null)
throw new Exception(error);
this.withEvents = withEvents;
//Create link between the Word.Applications events and the ApplicationEvents2_WordEvents class
if (this.withEvents)
{
excelEvents = new ExcelApplicationEvents();
//holds the connection point references of the Word.Application object
IConnectionPointContainer connectionPointContainer = excelApplication as IConnectionPointContainer;
//Find the connection point of the GUID found from Red Gate's .Net Reflector
Guid guid = new Guid("00024413-0000-0000-C000-000000000046");
connectionPointContainer.FindConnectionPoint(ref guid, out connectionPoint);
//Advise the Word.Application to send events to the event handler
connectionPoint.Advise(excelEvents, out sinkCookie);
excelEvents.WorkbookBeforeSaveEvent += new EventHandler<WorkbookEventArgs>(ExcelEventsWorkbookBeforeSaveEvent);
}
有什么想法吗?
干杯,
戴尔。
最佳答案
获取 特定 Excel 实例需要您使用 AccessibleObjectFromWindow API .
这在文章 Getting the Application Object in a Shimmed Automation Add-in 中有很好的解释作者:安德鲁·怀特查佩尔。
然而,您想要的是使用后期绑定(bind)来执行它。 divo对此进行了详细描述这里:How to use use late binding to get excel instance .
关于c# - 在 C# 中使用后期绑定(bind)获取特定的 Excel 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558256/