View Single Post
 
Old 07-11-2022, 11:58 AM
RMittelman RMittelman is offline Windows 10 Office 2016
Novice
 
Join Date: Dec 2010
Posts: 19
RMittelman is on a distinguished road
Default Issue Automating an Outlook Email from Access

I have the following code in an Outlook class in my Access database:

Code:
Public Sub CreateEMail(strETo As String, strECC As String, strESubject As String, strEBody As String, _
                       ParamArray attachFiles() As Variant)

    Dim attachFile  As Variant
    
    Set olEmail = olApp.CreateItem(olMailItem)
    With olEmail
        
        .To = strETo
        .CC = strECC
        If Not IsNull(olAccount) Then
            .SendUsingAccount = olAccount
            .Sender = olAccount
        End If
        .Subject = strESubject
        .HTMLBody = strEBody & "<br>" & .HTMLBody
        
'        .body = Application.plainText(strEBody) & vbCrLf & .body
        
        For Each attachFile In attachFiles
            If attachFile <> "" Then .Attachments.Add attachFile
        Next
        
        If m_autoSend Then
            .Send
        Else
            .Display
        End If
        
    End With

End Sub
Since I happen to have 2 different email addresses, I've previously set the variable olAccount to the preferred address. This variable is declared as Variant.

When the code is executed, I end up with an open Outlook email message ready to complete and send. This is good.

What's not good is the wrong email address (number 1 in my account list) is shown in the message, and I need to click the drop-down and choose the correct one (number 2 in my account list) in the message UI.

If I single step the code, the correct Outlook account is being chosen, but when the outgoing message is displayed, it's the wrong account.

Any ideas to fix this? Thanks...
Reply With Quote