Microsoft KB Archive/316983

= OL: A Sample COM Add-in That Uses the Visual Basic 6.0 Add-in Template =

Article ID: 316983

Article Last Modified on 2/27/2004

-

APPLIES TO


 * Microsoft Outlook 2000 Standard Edition
 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q316983



SUMMARY
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.



MORE INFORMATION
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 &quot;OnConnection&quot; 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 &quot;OnDisconnection&quot; 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 Explicit

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

End Sub

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(&quot;MAPI&quot;)

'This is the Inbox folder. Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)

' TODO: Change &quot;Other&quot; to any subfolder of Outlook Today. Set objSentFolder = objInbox.Parent.Folders(&quot;Other&quot;) strLeftSubject = Left(Item.Subject, 14)

' TODO: Change &quot;My Test String&quot; to the beginning search ' string that you want to use. The condition returns true ' if the subject of the message contains ' &quot;My Test String&quot; before any other text. The subject ' &quot;My Test String Here&quot; returns true. The subject ' &quot;Here is My Test String&quot; returns false. The subject ' &quot;my test string&quot; returns false.

If strLeftSubject = &quot;My Test String&quot; Then Set Item.SaveSentMessageFolder = objSentFolder End If

' Clean up objects that are created in this subroutine.

Set objInbox = Nothing Set objSentFolder = Nothing Set objNamespace = Nothing

End Sub

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

End Sub  Change the lines of code as specified in the lines that contain TODO.</li> On the File menu, click Make MyAddIn.dll. Type a name and location for the .dll file, and then click OK.</li></ol>

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.

<div class="references_section">