VBA to send attachments from multiple email addresses

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello folks:

I have written the code below, which works great. However, my Outlook has multiple email addresses and the code below keeps selecting my default address as a sender. I tried setting my other account as the new default, but my code kept sending from the original address. My question is, how can the code below be changed to email from a specific email address?

VBA Code:
Sub SendEmailAttachment()


Dim outlookApp As Object
Dim outMail As Object
Dim myAttachments As Object
Dim emailAddress As String
Dim emailAddressCC As String
Dim emailSubject As String
Dim fileName As String
Dim filePath As String
Dim attachment As String
Dim attachment2 As String
Dim signature As String
Dim lastrow As Integer
Dim x As Integer

 x = 2
 
 

Do While Sheet1.Cells(x, 1) <> ""
   
    Set outlookApp = CreateObject("Outlook.Application")
    Set outMail = outlookApp.CreateItem(0)
    Set myAttachments = outMail.Attachments
     
    
    filePath = ThisWorkbook.Path & "\"
    
    emailAddress = Sheet1.Cells(x, 3)
    emailAddressCC = Sheet1.Cells(x, 13)
    emailSubject = Sheet1.Cells(x, 17)
    fileName = Sheet1.Cells(x, 16)
    
    attachment = filePath + fileName
    attachment2 = filePath + "CEO Letter to Employees.Pdf"
    
    With outMail
    .Display

End With

signature = outMail.HTMLbody


With outMail
    
        .To = emailAddress
        .cc = emailAddressCC
        .bcc = ""
        .Subject = emailSubject
        .HTMLbody = "Please find your statement attached" & vbCrLf & "Best Regards" & signature
           
            
        myAttachments.Add (attachment2)
        myAttachments.Add (attachment)
        .Display
   
            
        lastrow = lastrow + 1
        emailAddress = ""
        
    x = x + 1

Set outlookApp = Nothing
Set outMail = Nothing

End With

Loop

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,214,892
Messages
6,122,112
Members
449,066
Latest member
Andyg666

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