This project has moved. For the latest updates, please go here.
/* taken from Tutorial04.csproj
Sometimes it is very useful to know the count of open COM Proxies in your application.
NetOffice offers you a static property and notification event for this.

   int LateBindingApi.Core.Factory.ProxyCount;
   event LateBindingApi.Core.Factory.ProxyCountChanged;

This example shows you how to use them.

See chapter5 in technical documentation for further info.

   Excel.Application _application;

   public Form1()

      // Initialize Api COMObject Support 

      LateBindingApi.Core.Factory.ProxyCountChanged += new Factory.ProxyCountChangedHandler(Factory_ProxyCountChanged);
   void Factory_ProxyCountChanged(int proxyCount)
       if (labelProxyCount.InvokeRequired)
           labelProxyCount.Tag = proxyCount.ToString();
	  labelProxyCount.Invoke(new MethodInvoker(UpdateLabel));
           labelProxyCount.Text = proxyCount.ToString();

   private void buttonExcel_Click(object sender, EventArgs e)
       if (null == _application)
           // start application
           _application = new Excel.Application();
           _application.DisplayAlerts = false;
           buttonExcel.Text = "Quit Excel";
           buttonWorkbook.Enabled = true;
           buttonAddins.Enabled = true;
           buttonAddRemoveWorkbook.Enabled = true;
           _application = null;
           buttonExcel.Text = "Start Excel";
           buttonWorkbook.Enabled = false;
           buttonAddins.Enabled = false;
           buttonAddRemoveWorkbook.Enabled = false;

   private void buttonWorkbook_Click(object sender, EventArgs e)
        // 2 new proxies, the workbooks proxy(implicit) and the new workbook from Add()
        if (null != _application)

   private void buttonAddins_Click(object sender, EventArgs e)
       if (null != _application)
           // 1 new enumerator proxy and 1 new proxy for any Addin
           foreach (Excel.AddIn item in _application.AddIns)

   private void buttonAddRemoveWorkbook_Click(object sender, EventArgs e)
       // add a new worbook and a new worksheet to the workbook
       // the worksheet is a child proxy from worbook, after dispose the workbook
       // creates 4 new proxies
       // the open proxy count is the same as before

       int proxyCount = LateBindingApi.Core.Factory.ProxyCount;

       Excel.Workbook book = _application.Workbooks.Add();

       int proxyCountAfterCreate = LateBindingApi.Core.Factory.ProxyCount;

       // dispose all child instances from application

       int proxyCountAfterDispose = LateBindingApi.Core.Factory.ProxyCount;

       string message = string.Format("Method creates a new Workbook with 1 new Worksheet\r\n" +
                                           "ProxyCount before create is {0}\r\n" +
                                           "ProxyCount after create is {1}\r\n" +
                                           "ProxyCount after dispose Workbook is {2}", proxyCount, proxyCountAfterCreate, proxyCountAfterDispose);

       MessageBox.Show(message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

   private void UpdateLabel()
       labelProxyCount.Text = labelProxyCount.Tag as string; 

See next: Tutorial05 - Understanding COMObject

Last edited Jul 20, 2011 at 12:21 AM by SebastianDotNet, version 31


No comments yet.