You can leverage COM add-ins from Outlook forms in many ways. One of the best ways is to use them to add functionality that isn't easy to implement in Microsoft Visual Basic Scripting Edition (VBScript) or that might be very expensive to create. For example, you can create a CDO session in a COM add-in and then share that CDO session across multiple Outlook applications so each application does not have to create and destroy a CDO session. With VBScript, you can access the collections of COM add-ins available on your local machine and call public methods or set public properties on these add-ins. Furthermore, a COM add-in can provide a library of functions that you can use in all your custom Outlook forms.
Figure 7-6 shows a sample Outlook form that uses a COM add-in to launch other executable programs. Although VBScript doesn't support the Shell function, you can make use of the Shell function in your COM add-in.
Take a look at the following code from the Outlook form shown in Figure 7-6. Notice that you can access the COMAddins collection to retrieve a list of the add-ins on your machine. You can then check whether the COM add-in you're interested in is loaded and connected in Outlook. To retrieve a COM add-in, you use the Item method on the collection and either pass in the index of your add-in in the collection or pass a string that specifies the ProgID of the add-in. Notice how I use the GetObject method with the ProgID of the COM add-in.
You might think that I could simply use the Object property of the COM add-in object that corresponds to my add-in. However, there is a known bug in Outlook that does not allow you to use this technique to get the COM add-in object. You should use the workaround in the code to make the COM add-in library work.
Dim oCOMAddinObj Dim oCOMAddin Sub cmdLaunchWord_Click Launch "winword" End Sub Sub cmdLaunchCalc_Click Launch "calc" end sub Sub cmdLaunchApp_Click Launch item.userproperties.find("strAppPath").value end sub Function Item_Open() 'On error resume next Err.Clear 'Try to get a reference to the COM add-in Set oCOMAddin = Application.COMAddIns.Item("OutlookHelper.Library") If err.number <> 0 Then MsgBox "There was an error retrieving a reference to the COM " _ & "Add-in Helper Library! Closing form!" Item_Open = False Exit Function End If 'Check to see whether the COM add-in is connected If oCOMAddin.Connect = False Then MsgBox "You must connect the COM Add-in before using this app! " Item_Open = False Exit Function End If 'Get the real COM add-in object 'This doesn't work in Outlook! Set oCOMAddinObj = _ Application.COMAddins.Item("OutlookHelper.Library").object 'Workaround: use GetObject Set oCOMAddinObj = GetObject("","OutlookHelper.Library") End Function Sub Launch(strAppPath) 'Get the Windows style iStyle = item.UserProperties.Find("strWindowsStyle").Value iError = oCOMAddinObj.CustomShell(strAppPath, iStyle) If iError = 0 then MsgBox "Error launching application!" End If End Sub
In the next example, the add-in doesn't do much besides add a single public function named CustomShell that the user can call. This function leverages the Shell function in Visual Basic and allows you to shell out to another program. The function also provides a bit of error checking in case some bogus values get past the Outlook test form. If the add-in's Shell call is successful, it will return the ID of the shelled out executable; if not, it will return 0.
Implements IDTExtensibility2 Dim oApp As Outlook.Application 'Global Outlook Application Object Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant) 'Not used, but must be defined. End Sub Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant) Set oApp = Nothing End Sub Private Sub IDTExtensibility2_OnConnection( _ ByVal Application As Object, _ ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _ ByVal AddInInst As Object, custom() As Variant) Set oApp = Application End Sub Private Sub IDTExtensibility2_OnDisconnection( _ ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _ custom() As Variant) 'Not used, but must be defined. End Sub Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant) 'Not used, but must be defined. End Sub Public Function CustomShell(strAppPath, iWindowStyle) If strAppPath = "" Then 'Return back an error Err.Raise vbObjectError + 513, "Shell Function", "A blank " _ & "pathname was passed!" Exit Function Else 'Check iWindowStyle If CInt(iWindowStyle) < 0 Or CInt(iWindowStyle) > 6 Then 'Make it normal with focus iWindowStyle = vbNormalFocus End If 'Try to execute the command and return the value iReturn = Shell(strAppPath, CInt(iWindowStyle)) CustomShell = iReturn End If End Function
Another way to fix this problem is to explicitly set the object reference for your COM add-in in Outlook's OnConnection event. The following is an example of how to use this workaround. You replace YourAddin.Connect with the ProgID of your COM add-in.
Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, _ ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _ ByVal AddInInst As Object, custom() As Variant) Application.COMAddIns.Item("YourAddin.Connect").Object = Me End Sub
To reference the COM add-in object in your Outlook form, you use the following code:
Set myObj = Application.COMAddIns.Item("YourAddin.Connect").Object