Qiuzman
Qiuzman

Reputation: 1761

How to handle missing MAPI properties?

The following code throws an error for a missing MAPI property. Some emails have it because I am able to Debug.Print but then one email triggers the error.

-2147221233 : The property "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" is unknown or cannot be found.

What should I do to catch these errors and keep moving forward rather than going to my error handler?

My code does an advance search then loops through a table to print everything:

Public Sub SearchOutlook()

'Create Email
'Generate Outlook Email for L&E
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim OutRecip As Outlook.Recipient
Dim QuitNewOutlook As Boolean
Dim Session As Outlook.Namespace
Dim ExchangeStatus  As OlExchangeConnectionMode
Dim objExUser As Outlook.ExchangeUser
Dim objExDisUser As Outlook.ExchangeDistributionList
Dim Scope As String
Dim Filter As String
Dim MySearch As Outlook.Search
Dim MyTable As Outlook.Table
Dim nextRow As Outlook.row
m_SearchComplete = False

On Error Resume Next
    Set OutApp = GetObject(, "Outlook.Application")
On Error GoTo OutlookErrors

If OutApp Is Nothing Then
    Set OutApp = CreateObject("Outlook.Application")
    QuitNewOutlook = True
End If

Set Session = OutApp.GetNamespace("MAPI")
Session.Logon

'We need to ensure outlook is fully connected
ExchangeStatus = Session.ExchangeConnectionMode
If ExchangeStatus <> 700 Then GoTo OutlookErrors

Set OutlookEventClass.oOutlookApp = OutApp

'set scope
Scope = "'" & OutApp.Session.Folders("email@something.com").FolderPath & "'"

'Establish filter - DASL schemas below:
'Message ID http://schemas.microsoft.com/mapi/proptag/0x1035001E = <blah.blah@blah.com>
'Subject urn:schemas:httpmail:subject ci_phrasematch 'blah' - Our store uses instant search
'Body urn:schemas:httpmail:textdescription ci_phrasematch 'blah'
'From urn:schemas:httpmail:fromemail
'To urn:schemas:httpmail:to
'cc urn:schemas:httpmail:cc

Dim SubjectsAndBodyToSearch() As String
Dim IDsToNotSearch() As String

Dim IDString As String

'SubjectsAndBodyToSearch = ActiveRecordset.GetRows(ActiveRecordset.RecordCount, "field")
SubjectsAndBodyToSearch = Split("cat,dog", ",")

Filter = SubjectSearchSchema(SubjectsAndBodyToSearch, OutApp.Session.DefaultStore.IsInstantSearchEnabled) & " OR " & _
         BodySearchSchema(SubjectsAndBodyToSearch, OutApp.Session.DefaultStore.IsInstantSearchEnabled)

If IDString <> "" Then
    Filter = Filter & " OR " & _
         " NOT ( " & MessageIDSearchSchema(IDsToNotSearch) & ")"
End If

Set MySearch = OutApp.AdvancedSearch(Scope, Filter, True, "MySearch")

'loop until event triggers that search is complete
While m_SearchComplete <> True
    DoEvents
Wend

Set MyTable = MySearch.GetTable
MyTable.Columns.Add ("http://schemas.microsoft.com/mapi/proptag/0x1035001E") 'messageID
MyTable.Columns.Add ("http://schemas.microsoft.com/mapi/proptag/0x00710102") 'conversationID
MyTable.Columns.Add ("urn:schemas:httpmail:textdescription") 'messagebody, outmail.Body

Dim SenderInfo As String
Dim RecipientsTo As String
Dim RecipientsCC As String
Dim RecipientsBCC As String
Dim MessageBody As String
Dim MessageID As String
Dim ConversationID As String

Do Until MyTable.EndOfTable

    Set nextRow = MyTable.GetNextRow()
    Set OutMail = Session.GetItemFromID(nextRow("EntryID"))

    MessageID = nextRow("http://schemas.microsoft.com/mapi/proptag/0x1035001E")
    ConversationID = nextRow("http://schemas.microsoft.com/mapi/proptag/0x00710102") 'outmail.conversationID
    MessageBody = nextRow("urn:schemas:httpmail:textdescription") 'outmail.Body

    'Sender Info
    If OutMail.SenderEmailType = "EX" Then
        SenderInfo = OutMail.Sender.GetExchangeUser.PrimarySmtpAddress
    Else
        SenderInfo = OutMail.SenderEmailAddress
    End If

    If SenderInfo <> "" Then

        RecipientsTo = ""
        RecipientsCC = ""
        RecipientsBCC = ""

        For Each OutRecip In Session.GetItemFromID(nextRow("EntryID")).Recipients
            'Debug.Print OutRecip.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
            'Debug.Print OutRecip.Address & " Type=" & OutRecip.Type & " " & OutMail.PropertyAccessor.GetProperty("urn:schemas:httpmail:fromemail")
            If OutRecip.Type = 1 Then
                RecipientsTo = RecipientsTo & ";" & OutRecip.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
            ElseIf OutRecip.Type = 2 Then
                RecipientsCC = RecipientsCC & ";" & OutRecip.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
            ElseIf OutRecip.Type = 3 Then
                RecipientsBCC = RecipientsBCC & ";" & OutRecip.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
            End If
        Next

        Debug.Print "Subject:" & nextRow("Subject") & " EntryID:" & nextRow("EntryID") & " From:" & SenderInfo & " To:" & RecipientsTo & " CC:" & RecipientsCC & " BCC:" & RecipientsBCC & " MessageID:" & MessageID & " ConversationID: " & ConversationID & "Body: " '& MessageBody

    End If
Loop

If QuitNewOutlook Then
    OutApp.Quit
End If

Set OutMail = Nothing
Set OutApp = Nothing
'Set ExchangeStatus = Nothing Possible Memory Leak?

'QueryRunning = False
Exit Sub

OutlookErrors:

    Debug.Print Err.Number & " : " & Err.Description
    Call ActivateUniversalSplashScreen("Outlook Error! Either restart or try again later.", MMCARMS.UploadBlurrImage, True, "Error")
    If DatabaseMethods.SQLIsConnectionOpen Then
        DatabaseMethods.SQLCloseDatabaseConnection
    End If

    Set OutMail = Nothing
    'Set ExchangeStatus = Nothing Possible Memory Leak?
    If Not OutApp Is Nothing And QuitNewOutlook Then
        OutApp.Quit
    End If
    Set OutApp = Nothing

End Sub

Upvotes: 0

Views: 923

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66306

That exception is by design - you must handle it. It is no doubt easier in languages other than VBA that support structured exception handling.

In VBA the best you can do is call On Error Resume Next / Err.Clear / call code that raises an exception / check Err.Number and Err.Description.

See https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/err-object for more details.

Upvotes: 2

Related Questions