Sending emails in VBA is not using the specified Mailbox

Txusmah

New Member
Joined
May 3, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,

My macro selects a specific Mailbox account to send the emails but Outlook sends them from another mailbox.

The code is set to send a specific email to different recipients (each email attaches a specific PDF). The problem is the part of the code where I select the mailbox that sends the account:

VBA Code:
Sub Send_Emails()


Dim objOutlook As Object
Dim objEmail As Object
Dim oAccount As Outlook.Account
Dim outapp As Outlook.Application


Set objOutlook = New Outlook.Application
Set objEmail = objOutlook.CreateItem(olMailItem)


'Set the text of the email and send it
    With objEmail

                .To = test_email
        
        Select Case Language
            Case "ES"
                .Subject = Range("TitleES") & " " & CustomerNum
                .Body = Range("BodyES")
              
                
                .Attachments.Add (Current_Folder & PDF_Letter)
                
            Case "PT"
                .Subject = Range("TitlePT") & " " & CustomerNum
                .Body = Range("BodyPT")
                                
                .Attachments.Add (Current_Folder & PDF_Letter)

        End Select
        
        Set oAccount = objOutlook.Session.Accounts.Item(1)
        objEmail.SendUsingAccount = oAccount
               
        .Send       ' SEND THE MESSAGE.
    
    End With

End Sub

I have confirmed that the oAccount it selects is the right one, every time. The email is always sent from another account (out of 5 that the user has).
The user has the right oAccount set as default (in position 1) and if I delete the mailbox that is actually sends the emails, it will use another one but not the one selected by the macro.

I have also used the code .sentonbehalfof but it doesn't work either.

thanks in advance,

Jesús
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try declaring objEmail as Outlook.MailItem instead of Object . . .

VBA Code:
Dim objEmail As Outlook.MailItem

Also, since you using early binding, might as well declare objOutlook as Outlook.Application instead of Object. And lastly, it looks like you're not using the variable outapp. If so, you can remove that declaration from your code.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top