This article was previously published under Q316983
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how to use Microsoft Visual Basic 6.0 and its standard add-in template to create an Outlook Component Object Model (COM) add-in. The sample add-in implements the Outlook ItemSend event, so that you can save specific sent-mail messages to a folder other than the Sent Items folder.
Microsoft Office 2000 (and later) supports COM add-ins. Developers can use COM add-ins to create add-ins that enhance and control Office programs. One of the key advantages of COM add-ins is that they provide a uniform design architecture to build Office add-ins.
COM Add-in Events
In most Outlook add-ins, you use the following two key events.
The "OnConnection" Event
The 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" Event
The 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-in
This 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:
Option ExplicitPrivate WithEvents objOLApp As Outlook.ApplicationPrivate 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 = ApplicationEnd SubPrivate 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 End If ' Clean up objects that are created in this subroutine. Set objInbox = Nothing Set objSentFolder = Nothing Set objNamespace = NothingEnd SubPrivate Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _ AddInDesignerObjects.ext_DisconnectMode, custom() As Variant) 'Clean up objects that are created during the OnConnection event. Set objOLApp = NothingEnd Sub
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-in
To 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-in
A 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.
For additional information about how to write COM add-ins, click the article numbers below to view the articles in the Microsoft Knowledge Base:
291163 OL2002: How to Create a COM Add-in for Outlook
230225 OL2000: How to Create a COM Add-in for Outlook
238228 HOWTO: Build an Office 2000 COM Add-In in Visual Basic
302896 HOW TO: Build an Office COM Add-in With Visual Basic .NET
For additional information about available resources and answersto frequently asked questions about Microsoft Outlook solutions, click the article numbers below to view the articles in the Microsoft Knowledge Base:
287530 OL2002: Questions About Custom Forms and Outlook Solutions
146636 OL2000: Questions About Custom Forms and Outlook Solutions
182349 OL98: Questions About Custom Forms and Outlook Solutions
170783 OL97: Questions About Customizing or Programming Outlook