[AccessD] issues automating outlook

John Colby jwcolby at gmail.com
Wed Jul 28 11:19:20 CDT 2021


Hey Daniel, I'm there.  My objective however is to use early binding which
means we need to get rid of the "as object".  Using early binding means
hover display, auto complete and intellisense all work.

https://docs.microsoft.com/en-us/previous-versions/office/troubleshoot/office-developer/binding-type-available-to-automation-clients

On Tue, Jul 27, 2021 at 5:40 PM Daniel Waters <df.waters at outlook.com> wrote:

> Hey John!
>
> My App first defines a variable for the Outlook Application, then for the
> Appointment Item.
> Also, code below is written in a Module instead of a Class.
>
>
> Dim objOL As Object
> Dim objAppt As Object
>
>     '--    Dim objOL As Outlook.Application  '-- TEST
>     '--    Dim objAppt As Outlook.AppointmentItem  '-- TEST
>
> Const olAppointmentItem As Integer = 1
> Const olMeeting As Integer = 1
>
> Set objOL = CreateObject("Outlook.Application")
> Set objAppt = objOL.CreateItem(olAppointmentItem)
>
>
> HTH!
> Dan
>
> -----Original Message-----
> From: AccessD [mailto:accessd-bounces+df.waters=
> outlook.com at databaseadvisors.com] On Behalf Of John Colby
> Sent: 27 July, 2021 10:03
> To: Access Developers discussion and problem solving; John W Colby
> Subject: [AccessD] issues automating outlook
>
> 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
> following:
>
> 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.
>
> Sigh.
>
> 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
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


-- 
John W. Colby
Colby Consulting


More information about the AccessD mailing list