Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [platform-swt-dev] Automating Outlook

Title: Message
Sure. I created an "ActiveX DLL" in VB, but you could write it in C++ if you're a glutton for punishment.
 
Note that this code is specific for Outlook. However, if you're not interested in listening to events you could possibly get by with a call to GetCOMObject(), which is completely generic.
 
 
Option Explicit
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Public Properties
'
Public WithEvents Application As Application   ' (Need to reference the Outlook library)
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Public Event Declarations
'
Public Event ItemSend(ByVal Item As Object)
Public Event ItemReceived(ByVal Item As Object)
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Private Properties
'
Private WithEvents inbox As Items
 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Functions not available in SWT
'
Public Function GetCOMObject(ByVal progID As String) As Object
 
On Error GoTo createIt
    Dim obj As Object
    Set obj = GetObject(, progID)
    If obj Is Nothing Then
        ' The object wasn't running. Start it up
createIt:
        Set obj = GetObject("", progID)
    End If
 
    Set GetCOMObject = obj
End Function
 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Workaround functions for things that don't work with SWT
'
Public Function AddAttachment(Attachments As Attachments, ByVal Source As String, ByVal AttachmentType As OlAttachmentType, ByVal DisplayName As String) As Attachment
    Set AddAttachment = Attachments.Add(Source, AttachmentType, , DisplayName)
End Function
 

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Implementation
'
Private Sub UserControl_Initialize()
    Set Application = New Outlook.Application
 
    ' Set up the Inbox
    Dim nameSpace As nameSpace
    Set nameSpace = Application.GetNamespace("MAPI")
    Dim olFolder As MAPIFolder
    Set olFolder = nameSpace.GetDefaultFolder(olFolderInbox)
    Set inbox = olFolder.Items
End Sub
 
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Events listened to
'
 
Private Sub inbox_ItemAdd(ByVal Item As Object)
    If TypeOf Item Is MailItem Then
        RaiseEvent ItemReceived(Item)
    End If
End Sub
 
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If TypeOf Item Is MailItem Then
        RaiseEvent ItemSend(Item)
    End If
End Sub
-----Original Message-----
From: Ed Burnette [mailto:Ed.Burnette@xxxxxxx]
Sent: Monday, April 28, 2003 2:45 PM
To: erik.cassel@xxxxxxxxxxx
Subject: RE: [platform-swt-dev] Automating Outlook

Would you consider contributing your activex control wrapper code?
 
-----Original Message-----
From: Erik Cassel [mailto:erik.cassel@xxxxxxxxxxx]
Sent: Monday, April 28, 2003 5:37 PM
To: platform-swt-dev@xxxxxxxxxxx
Subject: [platform-swt-dev] Automating Outlook

Has anybody tried automating Outlook?
 
I can get it to work... with some workarounds (see below)
 
I'm suspecting that some or all of the problems have to do with Outlook not running in the same process as the Java code. After all, if SWT's COM support is primarily designed for in-process ActiveX controls, then I might be pushing things a bit. However, it's appears to work very well, and it is extraordinarily powerful.
 
Should I be worried about any other potential pitfalls? For example, is SWT properly adding/removing references to all automation objects, even if they are out-of-process?
 
 
Problem 1)
 
The following code only works if Outlook is already running:
 
        Shell shell = new Shell();
        OleFrame frame = new OleFrame(shell, SWT.NONE);
        OleControlSite oleControlSite = new OleControlSite(frame, SWT.NONE, "Outlook.Application");
However, if Outlook isn't already running, then OleControlSite throws an exception.
 
My workaround is to create a little ActiveX control that gets the running Outlook instance for me.  The ActiveX control declares a function called "GetApplication", which returns an OleAutomation object of the Outlook Application. Now I'm able to traverse the Outlook object model nicely.... up to a point!
 
 
Problem 2)
 
Events don't get fired back. I attach listeners to the Application object, but nothing happens.
 
I worked around the problem by declaring similar events in my custom ActiveX control. It listens to Outlook and then fires an event of its own. My Java code listens to events of the custom ActiveX control.
 
 
 
Problem 3)
 
Although I can access lots of Outlook objects, I find that none of the functions with optional parameters work. For example, Attachments.Add() returns null:
 
            int[] rgdispid = attachments.getIDsOfNames(new String[]{"Add", "Source", "Type", "Position", "DisplayName"});
            int dispIdMember = rgdispid[0];
            Variant[] rgvarg = new Variant[3];
            int[] rgdispidNamedArgs = new int[3];
 
            rgvarg[0] = new Variant("C:\\foo.txt"); // this is the Source parameter
            rgdispidNamedArgs[0] = rgdispid[1];

            rgvarg[1] = new Variant(0x1); // this is the Type parameter
            rgdispidNamedArgs[1] = rgdispid[2];

            rgvarg[2] = new Variant("Hello!"); // this is the DisplayName parameter
            rgdispidNamedArgs[2] = rgdispid[4];

            Variant result = attachments.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);
Once again, my workaround is to declare a specialized function in the custom ActiveX control.
 
 
 
 

Back to the top