Set sending address - Excel VBA

rowbro

Board Regular
Joined
Dec 16, 2010
Messages
50
Good day, we have some code that sends a bulk email from a second email account on a PC, and for some reason we are not able to get this code to use the desired email address. Please see code below. can you see the issue as to why this is sending from the default, not second, email address?

VBA Code:
Sub Send_Email()
Dim edress As String
Dim subj As String
Dim message As String
Dim filename As String
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myAttachments As Object
Dim path1 As String
Dim path2 As String
Dim lastrow As Integer
Dim attachment1 As String
Dim attachment2 As String
Dim FirstName As String
Dim LastName As String
Dim Numshares As Integer
Dim Amountreceived As String

Dim rng As Range
Dim cell As Range


Set outlookapp = CreateObject("Outlook.Application")


Set rng = Sheet1.Range("A2:A" & Sheet1.Cells(Rows.Count, 1).End(xlUp).Row)

For Each cell In rng
    Set outlookmailitem = outlookapp.createitem(0)
    Set myAttachments = outlookmailitem.Attachments
    path1 = Sheet1.Range("K2").Value
    path2 = Sheet1.Range("K1").Value

    edress = cell.Value
    subj = cell.Offset(0, 1).Value
    filename = cell.Offset(0, 2).Value
    FirstName = cell.Offset(0, 3).Value
    LastName = cell.Offset(0, 4).Value

attachment1 = path1
attachment2 = path2 & "Application Form.docx"

    outlookmailitem.To = edress
    outlookmailitem.cc = ""
    outlookmailitem.bcc = ""

    outlookmailitem.Subject = "Test 13July"
    outlookmailitem.display
    Signature = outlookmailitem.HTMLBody
    

    outlookmailitem.SendUsingAccount = outlookapp.Session.Accounts.Item("account2@email.com")
        
    
    On Error Resume Next
    myAttachments.Add (attachment1)
    myAttachments.Add (attachment2)
    outlookmailitem.HTMLBody = "Good Afternoon " & FirstName & LastName & "," & "<br>" & "<br>" & " Please find attached for your kind attention. " & "<br>" & "<br>" & _
    "" & vbCrLf & "<br>" & outlookmailitem.HTMLBody
    'End
    'On Error GoTo 0

    outlookmailitem.display
    'outlookmailitem.Send

    lastrow = lastrow + 1
    edress = ""
    x = x + 1
  Next cell
  
    Set outlookapp = Nothing
    Set outlookmailitem = Nothing
    Set myAttachments = Nothing
  
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try the following:

Change this:
VBA Code:
outlookmailitem.SendUsingAccount = outlookapp.Session.Accounts.Item("account2@email.com")

For this:

VBA Code:
outlookmailitem.SendUsingAccount = outlookapp.Session.Accounts.Item(2)


----------------
Or this option:
VBA Code:
outlookmailitem.SentOnBehalfOfName = "account2@email.com"

🙂
 
Upvote 0
Thanks Dante. Tried both of these, but not joy on either.

I am out of ideas - can only think its something to do with the main account being an MSFT Exchange account and the other being old school POP3 + SMTP setup.

Any other ideas?
 
Upvote 0
Check if you can somehow check the ID number of the accounts.

1689605559165.png



That number is the one you should put in this line:

Rich (BB code):
outlookmailitem.SendUsingAccount = outlookapp.Session.Accounts.Item(2)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

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