Blog - Title

Brijs Blogging... Looking Beyond the Obvious

  • Brijs Blogging... Looking Beyond the Obvious

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

    • 0 Comments

    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

     

  • Brijs Blogging... Looking Beyond the Obvious

    How to send SMTP Mail via Port using CDOSYS with Basic Authentication

    • 0 Comments
    Here is a sample script to send SMTP mail via Port using CDOSYS with Basic Authentication.
     
     
       1:  'Sending SMTP Mail by Port 25 Using CDOSYS with Basic Authentication
       2:  'Refer the following article for sending mail with out using authentication : http://msdn.microsoft.com/en-us/library/ms992546(EXCHG.65).aspx  
       3:  'IMP: Please update the TODO: marked line as per the information required earlier than using the script
       4:   
       5:  Dim iMsg
       6:  Dim iConf
       7:  Dim Flds
       8:  Dim strHTML
       9:  Dim strSmartHost
      10:   
      11:  Const cdoSendUsingPort = 2
      12:  Const cdoBasic = 1
      13:   
      14:  strSMTPServer = "mail.example.com" 'TODO:
      15:  username = "user" 'TODO:
      16:  password = "password" 'TODO:
      17:   
      18:   
      19:  set iMsg = CreateObject("CDO.Message")
      20:  set iConf = CreateObject("CDO.Configuration")
      21:   
      22:  Set Flds = iConf.Fields
      23:   
      24:  ' set the CDOSYS configuration fields to use port 25 on the SMTP server
      25:   
      26:  With Flds
      27:  .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
      28:  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
      29:  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
      30:  .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic 
      31:  .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = username     
      32:  .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password
      33:  .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
      34:  .Update
      35:  End With
      36:   
      37:  ' build HTML for message body
      38:  strHTML = "<HTML>"
      39:  strHTML = strHTML & "<HEAD>"
      40:  strHTML = strHTML & "<BODY>"
      41:  strHTML = strHTML & "<b> This is the test HTML message body</b></br>"
      42:  strHTML = strHTML & "</BODY>"
      43:  strHTML = strHTML & "</HTML>"
      44:   
      45:  ' apply the settings to the message
      46:  With iMsg
      47:  Set .Configuration = iConf
      48:  .To = "nrnoble@example.com" 'TODO: 
      49:  .From = "nrnoble@example.com" 'TODO:
      50:  .Subject = "This is a test CDOSYS message (Sent via Port 25) with Basic Authentication"
      51:  .HTMLBody = strHTML
      52:  .Send
      53:  End With
      54:   
      55:  ' cleanup of variables
      56:  Set iMsg = Nothing
      57:  Set iConf = Nothing
      58:  Set Flds = Nothing
      59:   
      60:   
     
    Please feel free to post your comments for the posted contents or request regarding Microsoft Messaging Development issues. I would like to explore the messaging world further with you.
  • Brijs Blogging... Looking Beyond the Obvious

    Tips and Tricks for Troubleshooting Office Add-ins Part - 1

    • 2 Comments

    We can enable error logging and displaying of error message for the VSTO add-in by setting environment variables VSTO_SUPPRESSDISPLAYALERTS and VSTO_LOGALERTS.

    To display each error in a message box, set the VSTO_SUPPRESSDISPLAYALERTS variable to 0 (zero). You can suppress the messages by setting the variable to 1 (one). To write the errors to a log file, set the VSTO_LOGALERTS variable to 1 (one). Visual Studio Tools for Office creates the log file in the folder that contains the application manifest. The default name is <Manifestname>.manifest.log.

    We can refer to the article mentioned below:

    Visual Studio Tools for the Microsoft Office System : Debugging in Application-Level Projects
    http://msdn.microsoft.com/en-us/library/ms269003(VS.80).aspx


    Starting in Visual Studio 2008 Service Pack 1 (SP1), you can use the event viewer in Windows to see error messages that are captured by the Visual Studio Tools for Office runtime when you install or uninstall Visual Studio Tools for Office solutions. You can use these messages from the event logger to resolve installation and deployment problems.

    You can enable the event logger by setting the VSTO_EVENTLOGDISABLED environment variable to "0".

    Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0): Event Logging (2007 System)
    http://msdn.microsoft.com/en-us/library/cc442816.aspx


    We can also use AddinSpy to gather information for troubleshooting

    Using AddinSpy and AS Diagnostic Tools for Microsoft Office Add-ins
    http://msdn.microsoft.com/en-us/library/cc984533.aspx

    We can also use VSTO Troubleshooter which is part of VSTO Power Tools. It is a diagnostic tool that examines a computer looking for the necessary prerequisites for running Visual Studio Tools for Office solutions.

    Microsoft Visual Studio Tools for the Office System Power Tools v1.0.0.0
    http://www.microsoft.com/downloads/details.aspx?FamilyId=46B6BF86-E35D-4870-B214-4D7B72B02BF9&displaylang=en


    We can also have look at some troubleshooting techniques posted by my colleagues:

    Dave @ http://blogs.msdn.com/dvespa/archive/2008/09/30/troubleshooting-outlook-com-addins-introduction.aspx

    Praveen @ http://blogs.msdn.com/vsod/archive/2008/04/22/Troubleshooting-com-add-in-load-failures.aspx

Page 4 of 4 (78 items) 1234