Results 1 to 1 of 1
2012-04-29, 05:05 #1
- Join Date
- Apr 2012
- Southampton - UK
- Thanked 0 Times in 0 Posts
sendusingaccount from EXCEL with OUTLOOK
My first post in this forum.
I have been trying to establish exactly how to send an email from an excel application and all seems to work except how to specify the account to use.
I have used the following code (I have changed the references to the true account names in the comments).
If mail_account_nos =1 then .body line generates an item with email account 1 (say firstname.lastname@example.org)
If mail_account_nos =2 then .body line generates an item with email account 2 (say email@example.com)
BUT the email is always sent from the same OUTLOOK account - in this case the default is anne - i.e. the sendusingaccount statement appears to make no difference.
Any ideas please ?
Sub Send_Email_Using_VBA(mailaddress As String, mailcontent As String, mailsubject As String) '================================================================================== Dim mail_account_nos As Integer Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(olMailItem) On Error GoTo debugs mail_account_nos = 2 With OutMail .Subject = mailsubject '.From = "firstname.lastname@example.org" '<<<<<<<<<<<<<<<<this fails - "Object doesn't support this object or method" .To = mailaddress .Recipients.Add (mailaddress) ' same as .To (I think) .CC = mailaddress .BCC = mailaddress .Body = "===1===" + vbCrLf + mailcontent + vbCrLf + OutApp.Session.Accounts.Item(mail_account_nos) + vbCrLf + "===1===" .Display '<<<<<<<<<<<<<<<<OutApp.Session.Accounts.Item(mail_account_nos) change 2 to 1 and it changes this output but not the account sent from '.HTMLBody = strbody & "<br><br>" & Signature 'You can add files also like this '.Attachments.Add ("C:\test.txt") .SendUsingAccount = OutApp.Session.Accounts.Item(mail_account_nos) ' have 2 accounts "email@example.com" and "firstname.lastname@example.org" always send from anne - the default '.send End With debugs: If Err.Description <> "" Then MsgBox Err.Description Set OutMail = Nothing Set OutApp = Nothing End Sub