遅延バインディングを少し手伝った直後。
Excel を遅延バインドしようとしていますが、問題はありません。いくつかの問題が発生するのは、Excel のインスタンスが複数ある場合のみです。
バインドする Excel のインスタンス (およびリンク イベントなど) を特定できるようにしたいと考えています。主な理由は、サードパーティのツールから Excel ドキュメントを開くアプリケーションがあり、イベントが処理されないことです。イベントをキャッチするために開いていることがわかっている特定の Excel インスタンスを利用できるようにしたいと考えています。唯一の問題は、ユーザーが Excel を既に開いている場合です (方法は関係ありません)。
バインド後に Excel を開いても、問題はありません。エクセルが既に開いている場合のみです。
開いている最初のインスタンスにバインディングが行われているようです。
実際のコードは次のとおりです。
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);
}
何か案は?
乾杯、
デール。