vbaoutlookoutlook-2003

Outlook 2003 VBA to detect selected account when sending


Is it possible to detect which account an email is being sent via the Application_ItemSend VBA function of Outlook 2003? The accounts are POP3/SMTP on a standalone machine, and not MAPI or Exchange based.

I have tried using "Outlook Redemption" (http://www.dimastr.com/redemption/) but I just cannot find any property / method that will tell me which of the accounts the email is being sent through.

I don't need to be able to amend/select the account being sent from, just simply detect.


Solution

  • I have found a way of finding the account name, thanks to this link which provides the code for selecting a particular account.

    Using this code as a base, I have create a simple GetAccountName function, which is doing exactly what I need it to do.

    Edit: The below will only work if you're NOT using Word as the editor.

    Private Function GetAccountName(ByVal Item As Outlook.MailItem) As String
        Dim OLI As Outlook.Inspector
        Const ID_ACCOUNTS = 31224
    
        Dim CBP As Office.CommandBarPopup
    
        Set OLI = Item.GetInspector
        If Not OLI Is Nothing Then
            Set CBP = OLI.CommandBars.FindControl(, ID_ACCOUNTS)
            If Not CBP Is Nothing Then
                If CBP.Controls.Count > 0 Then
                    GetAccountName = CBP.Controls(1).Caption
                    GoTo Exit_Function
                End If
            End If
        End If
        GetAccountName = ""
    
    Exit_Function:
        Set CBP = Nothing
        Set OLI = Nothing
    End Function