[AccessD] issues automating outlook

John Colby jwcolby at gmail.com
Tue Jul 27 10:02:46 CDT 2021

I'm trying to automate outlook.  In fact I am more or less successfully
doing so.  However, while passing an email object around I run into the

Private Sub mOLInboxItems_ItemAdd(ByVal item As Object)

This is an event that outlook raises, passing in an object ByVal, i.e.not a
pointer to the object.

As it happens, the item can be n email, or a calender event or... I know
not what all.  But I do know that one of the things that can be passed in
is an email.  So... the following works:

Dim Msg As Outlook.MailItem
    If TypeName(item) = "MailItem" Then
        'Msg = ctype(item, outlook.MailItem)
        MessageInfo = "" & _
            "Sender : " & item.SenderEmailAddress & vbCrLf & _
            "Sent : " & item.SentOn & vbCrLf & _
            "Received : " & item.ReceivedTime & vbCrLf & _
            "Subject : " & item.Subject & vbCrLf & _
            "Size : " & item.Size & vbCrLf & _
            "Message Body : " & vbCrLf & item.Body
            Debug.Print MessageInfo
        'Result = MsgBox(MessageInfo, vbOKOnly, "New Message Received")
        'ExtractDataFromMsgHeader item

        mProcessOrders item
    End If

Notice that trying to coerce Msg = ctype(item, outlook.MailItem) fails.  In
fact VBA doesn't even know what outlook.MailItem means, it gives a compile
error if I uncomment that line.  So I am unable to coerce item into a type
outlook.MailItem at least using ctype.  I'm assuming that ctype only works
with built-in vba types which outlook.MailItem obviously is not.

However... I then call mProcessOrders, passing in item

Private Function mProcessOrders(ByRef lOLMailItem As Outlook.MailItem)

In the function definition I cause a coercion to happen, no compile error.
Notice I also converted from ByVal to ByRef. Inside of mProcessOrders I
suddenly have a *pointer *to object  lOLMailItem of type Outlook.MailItem.
Or at least a pointer to the *ByVal copy* of the mail item.

Which I think is pretty cool.

Now inside of mProcessOrders I can see the properties of lOLMailItem ...

    Select Case *lOLMailItem.Subject*
    Case "AN Initial Order"
        Debug.Print "This email is an order" & vbCrLf & vbCrLf

        strEmailBody = *lOLMailItem.Body*

        Debug.Print *lOLMailItem.Body*

All of this works...until...

I dimension a class (to hold the email item and parse the body)

        Set lclsANEmailInitialPurchase = New clsANEmailInitialPurchase

and I pass my pointer to lOLMailItem into

fInit(ByRef lOLMailItem As Outlook.MailItem) of the class.

        lclsANEmailInitialPurchase.fInit lOLMailItem

 On the other side of that call I get  <automation error system call failed>

Damned inconvenient.  Not only does the system call fail but it corrupts
the mail item such that any reference to it now fails, even back in the
calling function.


So I have been passing the mailItem around functions of this class, but as
soon as I pass it off to another class it fails.

I don't really have to do things this way of course.  I really only care
about the values of the properties of the mail object which I can get and
pass those values into the class instead of the mailItem but still...  What
I was hoping was to create a class to hold each email, and all the stuff I
do to process an email (encapsulation)  and be able to refer back to these
emails later.

The vagaries of Windows...
John W. Colby
Colby Consulting

More information about the AccessD mailing list