How to Capture Outlook Custom Form's Command Button Click event in COM Add-in using C#?

How to Capture Outlook Custom Form's Command Button Click event in COM Add-in using C#?

  • Comments 0

Use the following code snippets to wire an event in C# to capture the click event of the command button on the Outlook Custom Form.

   1:  /*NOTE: Following programming examples is for illustration only, without warranty either expressed or implied, 
   2:  including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. 
   3:  This sample code assumes that you are familiar with the programming language being demonstrated and the tools used 
   4:  to create and debug procedures.*/
   5:   
   6:   namespace MyOLAddin
   7:  {
   8:      using System.Windows.Forms;
   9:      using System;
  10:      using Microsoft.Office.Core;
  11:      using Extensibility;
  12:      using System.Runtime.InteropServices;
  13:      using Outlook= Microsoft.Office.Interop.Outlook ;
  14:      using Microsoft.Vbe.Interop.Forms;
  15:   
  16:      /// <summary>
  17:      ///   The object for implementing an Add-in.
  18:      /// </summary>
  19:      /// <seealso class='IDTExtensibility2' />
  20:      [GuidAttribute("406F2E78-C45C-4C12-AEB6-244837293650"), ProgId("MyOLAddin.Connect")]
  21:      public class Connect : Object, Extensibility.IDTExtensibility2
  22:      {
  23:          /// <summary>
  24:          ///        Implements the constructor for the Add-in object.
  25:          ///        Place your initialization code within this method.
  26:          /// </summary>
  27:          /// 
  28:          
  29:          Outlook.Application m_outlook;
  30:          Outlook.Inspectors m_inspectors;
  31:          public Connect()
  32:          {
  33:          
  34:          }
  35:   
  36:          /// <summary>
  37:          ///      Implements the OnConnection method of the IDTExtensibility2 interface.
  38:          ///      Receives notification that the Add-in is being loaded.
  39:          /// </summary>
  40:          /// <param term='application'>
  41:          ///      Root object of the host application.
  42:          /// </param>
  43:          /// <param term='connectMode'>
  44:          ///      Describes how the Add-in is being loaded.
  45:          /// </param>
  46:          /// <param term='addInInst'>
  47:          ///      Object representing this Add-in.
  48:          /// </param>
  49:          /// <seealso class='IDTExtensibility2' />
  50:          public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
  51:          {
  52:              m_outlook = application as Outlook.Application;
  53:              m_inspectors = m_outlook.Inspectors;
  54:              m_inspectors.NewInspector +=new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(myInspColl_NewInspector);
  55:          }
  56:   
  57:          /// <summary>
  58:          ///     Implements the OnDisconnection method of the IDTExtensibility2 interface.
  59:          ///     Receives notification that the Add-in is being unloaded.
  60:          /// </summary>
  61:          /// <param term='disconnectMode'>
  62:          ///      Describes how the Add-in is being unloaded.
  63:          /// </param>
  64:          /// <param term='custom'>
  65:          ///      Array of parameters that are host application specific.
  66:          /// </param>
  67:          /// <seealso class='IDTExtensibility2' />
  68:          public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
  69:          {
  70:          }
  71:   
  72:          /// <summary>
  73:          ///      Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.
  74:          ///      Receives notification that the collection of Add-ins has changed.
  75:          /// </summary>
  76:          /// <param term='custom'>
  77:          ///      Array of parameters that are host application specific.
  78:          /// </param>
  79:          /// <seealso class='IDTExtensibility2' />
  80:          public void OnAddInsUpdate(ref System.Array custom)
  81:          {
  82:          }
  83:   
  84:          /// <summary>
  85:          ///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.
  86:          ///      Receives notification that the host application has completed loading.
  87:          /// </summary>
  88:          /// <param term='custom'>
  89:          ///      Array of parameters that are host application specific.
  90:          /// </param>
  91:          /// <seealso class='IDTExtensibility2' />
  92:          public void OnStartupComplete(ref System.Array custom)
  93:          {
  94:          }
  95:   
  96:          /// <summary>
  97:          ///      Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
  98:          ///      Receives notification that the host application is being unloaded.
  99:          /// </summary>
 100:          /// <param term='custom'>
 101:          ///      Array of parameters that are host application specific.
 102:          /// </param>
 103:          /// <seealso class='IDTExtensibility2' />
 104:          public void OnBeginShutdown(ref System.Array custom)
 105:          {
 106:          
 107:          }
 108:          
 109:          private void myInspColl_NewInspector(Outlook.Inspector inspector)
 110:          {
 111:              Microsoft.Vbe.Interop.Forms.ControlClass mc;
 112:              Microsoft.Vbe.Interop.Forms.CommandButton cb;
 113:  // Here we wire up the click event of the CommandButton1 on the "P.2" of the Outlook Custom Form    
 114:              try
 115:              {
 116:                  Microsoft.Vbe.Interop.Forms.UserForm userform;
 117:                  
 118:                  object obTemp = ((Outlook.Pages) (Inspector.ModifiedFormPages))["P.2"];
 119:                  
 120:                  userform = (Microsoft.Vbe.Interop.Forms.UserForm) obTemp;
 121:                  
 122:                  mc = (ControlClass) userform.Controls._GetItemByName("CommandButton1");
 123:                  
 124:                  cb = (CommandButton) mc;
 125:                  
 126:                  cb.Click+=new CommandButtonEvents_ClickEventHandler(cb_Click);
 127:                  
 128:              }
 129:              catch (Exception)
 130:              {
 131:                  //MessageBox.Show(e.ToString());
 132:                  mc = null;
 133:              }
 134:          }
 135:      
 136:  //This is event handler of the CommandButton1 Click's event
 137:          private void cb_Click()
 138:          {
 139:              MessageBox.Show("cb");
 140:          }
 141:      }
 142:  }

We can refer to the following KB as in case of some issue related to using Microsoft.Vbe.Introp.Forms :
BUG: "Attempt to Access the Method Failed" Error Message When You Use a Reference to the Wrong Microsoft Forms 2.0 Object Library
http://support.microsoft.com/kb/824009

 

Leave a Comment
  • Please add 8 and 6 and type the answer here:
  • Post