Freigeben über


IConnectionPoint and .NET or: How I Learned to Stop Worrying and Love Managed Event Sinks (part 1): Sample 2

Sample 2

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.InteropServices.ComTypes;

using System.Text;

using System.Threading;

using System.Windows.Forms;

using Excel = Microsoft.Office.Interop.Excel;

using Office = Microsoft.Office.Core;

namespace Sample2

{

    internal sealed class WorkbookEventSink : Excel.WorkbookEvents

    {

        #region WorkbookEvents Members

        public void Activate() { throw new NotImplementedException(); }

        public void AddinInstall() { throw new NotImplementedException(); }

        public void AddinUninstall() { throw new NotImplementedException(); }

        public void AfterXmlExport(Excel.XmlMap Map, string Url, Excel.XlXmlExportResult Result) { throw new NotImplementedException(); }

        public void AfterXmlImport(Excel.XmlMap Map, bool IsRefresh, Excel.XlXmlImportResult Result) { throw new NotImplementedException(); }

        public void BeforeClose(ref bool Cancel)

        {

            Cancel = DialogResult.Yes != MessageBox.Show("Really close?", "Excel", MessageBoxButtons.YesNo);

        }

        public void BeforePrint(ref bool Cancel) { throw new NotImplementedException(); }

        public void BeforeSave(bool SaveAsUI, ref bool Cancel) { throw new NotImplementedException(); }

        public void BeforeXmlExport(Excel.XmlMap Map, string Url, ref bool Cancel) { throw new NotImplementedException(); }

        public void BeforeXmlImport(Excel.XmlMap Map, string Url, bool IsRefresh, ref bool Cancel) { throw new NotImplementedException(); }

        public void Deactivate() { throw new NotImplementedException(); }

        public void NewSheet(object Sh) { throw new NotImplementedException(); }

        public void Open() { throw new NotImplementedException(); }

        public void PivotTableCloseConnection(Excel.PivotTable Target) { throw new NotImplementedException(); }

        public void PivotTableOpenConnection(Excel.PivotTable Target) { throw new NotImplementedException(); }

        public void RowsetComplete(string Description, string Sheet, bool Success) { throw new NotImplementedException(); }

        public void SheetActivate(object Sh) { throw new NotImplementedException(); }

        public void SheetBeforeDoubleClick(object Sh, Excel.Range Target, ref bool Cancel) { throw new NotImplementedException(); }

        public void SheetBeforeRightClick(object Sh, Excel.Range Target, ref bool Cancel) { throw new NotImplementedException(); }

        public void SheetCalculate(object Sh) { throw new NotImplementedException(); }

        public void SheetChange(object Sh, Excel.Range Target) { throw new NotImplementedException(); }

        public void SheetDeactivate(object Sh) { throw new NotImplementedException(); }

        public void SheetFollowHyperlink(object Sh, Excel.Hyperlink Target) { throw new NotImplementedException(); }

        public void SheetPivotTableUpdate(object Sh, Excel.PivotTable Target) { throw new NotImplementedException(); }

        public void SheetSelectionChange(object Sh, Excel.Range Target) { throw new NotImplementedException(); }

        public void Sync(Office.MsoSyncEventType SyncEventType) { throw new NotImplementedException(); }

        public void WindowActivate(Excel.Window Wn) { throw new NotImplementedException(); }

        public void WindowDeactivate(Excel.Window Wn) { throw new NotImplementedException(); }

        public void WindowResize(Excel.Window Wn) { throw new NotImplementedException(); }

        #endregion

        internal WorkbookEventSink()

        {

        }

    }

    internal static class Program

    {

        private static Excel.Application _application;

        private static Excel.Workbook _workbook;

        private static void Main(string[] args)

        {

            _application = new Excel.ApplicationClass();

            _application.Visible = true;

            _workbook = _application.Workbooks.Add(Type.Missing);

            // Hook up event sink using IConnectionPoint.

            WorkbookEventSink workbookEventSink = new WorkbookEventSink();

            IConnectionPointContainer connectionPointContainer = (IConnectionPointContainer)_workbook;

            Guid workbookEventsInterfaceId = typeof(Excel.WorkbookEvents).GUID;

            IConnectionPoint connectionPoint;

            connectionPointContainer.FindConnectionPoint(ref workbookEventsInterfaceId, out connectionPoint);

            int cookie;

            connectionPoint.Advise(workbookEventSink, out cookie);

            _workbook.Close(false, Type.Missing, Type.Missing);

            if (0 == _application.Workbooks.Count)

            {

                Console.WriteLine("Workbook closed.");

    _workbook = null;

            }

            else

            {

                Console.WriteLine("Workbook not closed.");

            }

            if (null != _workbook)

            {

                if (null != connectionPoint)

                {

                    // Unhook event sink using IConnectionPoint.

                    connectionPoint.Unadvise(cookie);

                    connectionPoint = null;

                }

            }

            CleanUp();

        }

        private static void CleanUp()

        {

            Console.WriteLine("Cleaning up...");

            if (null != _workbook)

            {

                _workbook.Close(false, Type.Missing, Type.Missing);

                _workbook = null;

            }

            if (null != _application)

            {

                _application.Quit();

                _application = null;

            }

            GC.Collect();

            GC.WaitForPendingFinalizers();

            GC.Collect();

            GC.WaitForPendingFinalizers();

        }

    }

}

Comments