Welcome to Outlook2CRM Sign in | How to Register?
  The Official Outlook2CRM Development blog. Hosted by Ryan Farley, Director of Development for Customer FX Corporation and developer of Outlook2CRM.

 Accessing the Native Outlook MailItem in Outlook2CRM Code


There will likely be times when you need to access additional information about the selected e-mail in your Outlook2CRM scripts and forms than what is provided by the API include script. For this very reason, the API does expose the native Outlook MailItem for you to query for additional properties, or even manipulate if needed.

The API exposes two objects for the Outlook MailItem.

  1. Addin_MailItem
  2. Addin_NativeMailItem

There are some differences between the two.

Addin_MailItem

The Addin_MailItem object is a wrapped MailItem object. The custom wrapper is used for late-bound calls to the Outlook object model making Outlook2CRM version independent when it comes to the version of Outlook installed on the machine. It also avoids any security prompts with some direct MAPI calls. There will be more about this wrapped MailItem forthcoming in the documentation area.

Addin_NativeMailItem

This object is the native Outlook MailItem. Any documentation about the Outlook object model applies to this object. One thing to be aware of, this object will differ based on the version of Outlook installed on the pc since this object is dynamically created at runtime based on what it receives from Outlook. If the user is using Outlook 2000, then the MailItem object model for Outlook 2000 will apply.

One of the current Outlook2CRM beta testers, Alexander Pfingstl, wanted to be able to determine if the selected e-mail in Outlook was in HTML format or not. Using the exposed Addin_NativeMailItem this is certainly possible. Let's put a sample together to demonstrate.

'Including Script - Outlook2CRM:Outlook2CRM Addin Client API
Option Explicit

Sub AXFormOpen(Sender)
Dim mail

    Set mail = Addin_NativeMailItem

    Select Case mail.BodyFormat
    Case 0
         MsgBox "Mail Format: Unspecified"
    Case 1
         MsgBox "Mail Format: Plain Text"
    Case 2
         MsgBox "Mail Format: HTML"
    Case 3
         MsgBox "Mail Format: Rich Text"
    End Select

    Set mail = Nothing
End Sub

You can see how easily we can query properties, such as the BodyFormat property to determine the format of the e-mail body, from the native Outlook MailItem object. Outlook2CRM will always give you the message body in plain text. If the body of the message is in HTML, you won't get all the HTML tags, just the text - as close to what the user would see when viewing the e-mail in Outlook. However, if you wanted to get the HTML from the e-mail, you could do the following:

'Including Script - Outlook2CRM:Outlook2CRM Addin Client API
Option Explicit

Sub AXFormOpen(Sender)
Dim mail
Dim html

    Set mail = Addin_NativeMailItem

    If mail.BodyFormat = 2 Then ' 2=HTML
        html = mail.HTMLBody
        ' do something with the HTML...
    End If

    Set mail = Nothing
End Sub

Just make sure you always dereference the MailItem object by setting it to Nothing when you're done.


Published Wednesday, March 07, 2007 12:57 PM by Ryan Farley

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit