COM Add-in EventsIn most Outlook add-ins, you use the following two key events.
The "OnConnection" EventThe OnConnection event starts when the COM add-in is connected. The add-in can be connected when Outlook starts, by the user, or by using automation in Outlook. If the OnConnection event returns successfully, the add-in is considered loaded. Otherwise, the reference to the add-in is released and destroyed by the host application.
The OnConnection event has the following four parameters:
- Application. A reference to the host application object.
- ConnectMode. A constant that specifies how the add-in is connected.
- AddInInst. A reference to the add-in object.
- custom(). Arguments to the add-in.
The "OnDisconnection" EventThe OnDisconnection event starts after the COM add-in is disconnected and before the COM add-in unloads from memory. This event contains all of the cleanup that is required and restores any changes that are made to the host application.
The OnDisconnection event has the following two parameters:
- RemoveMode. A constant that specifies how the add-in is removed.
- custom(). Arguments to the add-in.
Creating the COM Add-inThis section describes how to create a COM add-in that moves sent mail to a folder other than the default Sent Items folder. The COM add-in moves sent mail based on subject of the message. You cannot use the Outlook rules wizard to implement this type of rule.
To create a COM add-in that moves sent mail to a folder other than the default Sent Items folder:
- In Visual Basic 6.0, create a new project that is based on the AddIn template.
- Remove the added form frmAddIn from the AddIn project. To do so, right-click frmAddIn (frmAddIn) in the Project Explorer, and then click Remove frmAddIn.
- In the Project Explorer, expand Designers, right-click Connect (Connect), and then click View Object.
- In the Addin Designer view of the Connect file, set the application to Microsoft Outlook, and then make sure that the initial load behavior is set to Startup.
- On the Project menu, click References. If you are using Outlook 2000, reference the Microsoft Outlook 9.0 Object Library. If you are using Outlook 2002, reference the Microsoft Outlook 10.0 Object Library. Click OK.
- In the Project Explorer, right-click the Connect designer, and then click View Code. Delete all of the code in the code window. Type or paste the following code in the empty code window:
Private WithEvents objOLApp As Outlook.Application
Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
'Set my object to the host application.
Set objOLApp = Application
Private Sub objOLApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objNamespace As Outlook.Namespace
Dim objInbox As Outlook.MAPIFolder
Dim objSentFolder As Outlook.MAPIFolder
Dim strLeftSubject As String
Set objNamespace = objOLApp.GetNamespace("MAPI")
'This is the Inbox folder.
Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)
' TODO: Change "Other" to any subfolder of Outlook Today.
Set objSentFolder = objInbox.Parent.Folders("Other")
strLeftSubject = Left(Item.Subject, 14)
' TODO: Change "My Test String" to the beginning search
' string that you want to use. The condition returns true
' if the subject of the message contains
' "My Test String" before any other text. The subject
' "My Test String Here" returns true. The subject
' "Here is My Test String" returns false. The subject
' "my test string" returns false.
If strLeftSubject = "My Test String" Then
Set Item.SaveSentMessageFolder = objSentFolder
' Clean up objects that are created in this subroutine.
Set objInbox = Nothing
Set objSentFolder = Nothing
Set objNamespace = Nothing
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _
AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
'Clean up objects that are created during the OnConnection event.
Set objOLApp = Nothing
- Change the lines of code as specified in the lines that contain TODO.
- On the File menu, click Make MyAddIn.dll. Type a name and location for the .dll file, and then click OK.
Loading the COM Add-inTo load the COM add-in in Outlook, click Options on the Tools menu. Click the Other tab, click Advanced Options, and then click COM Add-Ins. Click Add, locate the .dll file that you compiled, and then click OK.
Distributing the COM Add-inA COM add-in has to be registered on every computer on which the COM add-in will be used. You can typically distribute the add-in one of two ways:
- Create a Setup program for your .dll file, and then have the users install the add-in.
- In an organization, use a logon script to copy the .dll file to the local computer, and then use the Regsvr32.dll file to manually register the .dll file.
Article ID: 316983 - Last Review: Mar 23, 2009 - Revision: 1