0

At our company, we use Outlook Exchange Desktop edition. Some of us have multiple accounts to send/receive emails from. I have created a VBA macro to check for each email when pressing the Send button what account they are sending the mail from, and then to create a handler that checks if this mail arrives in the "Sent items" folder. After arriving it takes this mail and saves it to a predefined folder.

At first I created this macro to only work with the default account and default folder for sent mail. It worked perfectly. Now I added some code to check the account it is sent from in the correct "Sent items" folder (the one of the correct account). Therefore I used the MailItem.SendUsingAccount property.

When applying this macro, 8 out of 10 times, I get the correct account and the macro works fine. The other 2 times, the SendUsingAccount property return "Null" or "Nothing" (I don't know the difference between these two). I found an other thread here where another user suggests the assignment of accounts to Mailitems is not always reliable, but it doesn't state a proper solution to my problem. Why do I sometimes get nothing as a returnvalue and other times it works perfectly fine? When it's not working, it's always about the code line: ZendAcc = Item.SendUsingAccount. Here the ZendAcc variable cannot store the empty SendUsingAccount return.

VBA:

Public WithEvents myOlItems As Outlook.Items

'Sub triggered when pressing the send button in outlook email
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim ZendAcc As String
    'Checking for multiple accounts
    If Application.Session.Accounts.Count > 1 Then
        'Check if the itemtype is MailItem. (normally it will always be correct)
        If TypeName(Item) <> "MailItem" Then
            MsgBox "There is no MailItem"
            Exit Sub
        Else
            'Store AccountName in String
            ZendAcc = Item.SendUsingAccount
            If ZendAcc = "" Then
                Exit Sub
            End If
            'Create the handler and give it the Accountname String
            Call Initialize_handler(ZendAcc)
        End If
    Else
        'When there is only one account, the Accountname doesn't matter, but you need a String
        Call Initialize_handler("Useless")
    End If
End Sub

Public Sub Initialize_handler(ByVal zendAccount As String)
    Dim Store As Store
    Dim Folder As Folder
    'If there are multiple accounts, check for the right sent mails folder, otherwise use the default one.
    If Application.Session.Accounts.Count > 1 Then
        For Each oAccount In Application.Session.Accounts
            If oAccount.SmtpAddress = zendAccount Then
                Set Store = oAccount.DeliveryStore
                Set acFolder = Store.GetDefaultFolder(olFolderSentMail)
                Exit For
            End If
        Next
        Set myOlItems = acFolder.Items
    Else
        Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
    End If
End Sub

'Catch the added mail and save to folder
Private Sub myOlItems_ItemAdd(ByVal ObjectSent As Object)
    'Code to do something with this mail. In my case: store to defined folder.
End Sub
karel
  • 5,489
  • 46
  • 45
  • 50

1 Answers1

0

If the account was explicitly set and the message has not been saved first, you might get null (aka Nothing in VB). In that case, assume the very first account from the Application.Session.Accounts collection will be used.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Dmitry, thanks for your comment. The account to send the mail from is manually selected at the top of the mail before sending it. We normally don't save our messages before sending them. Because of the "randomness" of the used accounts, it is not really helpful to assume we use the standard account. I might try to save before sending so the account will no longer return null. I will let you know if it helps. – Fries Mourisse Sep 06 '21 at 14:08
  • The drop down has to display something, so it shows the default account by default. It is only guaranteed to be set on the message if the user explicitly sets it. – Dmitry Streblechenko Sep 06 '21 at 21:44