scubadiver007
Registered User.
- Local time
- Today, 14:49
- Joined
- Nov 30, 2010
- Messages
- 317
How do I change the account from which to send the email if I have multiple accounts, or can I?
Thanks in advance
Thanks in advance
Public Sub sendEmail(eMailStr As String, subjectLine As String, bodyStr As String, _
fileToSend As String, [B]Optional fromStr As String = "someone@somedomain.co.uk"[/B])
[COLOR=Green]' Code Courtesy of
' Paul Eugin
' Working in Office 2000-2013[/COLOR]
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
[COLOR=Blue] Dim OutAccounts As Outlook.Accounts
Dim OutAccount As Outlook.Account
Dim OutAccountTemp As Outlook.Account[/COLOR]
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(0)
[COLOR=Blue]Set OutAccounts = OutApp.Application.Session.Accounts
For Each OutAccountTemp In OutAccounts
If (OutAccountTemp.SmtpAddress = fromStr) Then
Set OutAccount = OutAccountTemp
Exit For
End If
Next[/COLOR]
On Error Resume Next
With OutMail
[COLOR=Blue].SendUsingAccount = OutAccount[/COLOR]
.Display
.To = eMailStr
.Subject = subjectLine
.HTMLBody = bodyStr & .HTMLBody
.Attachments.Add fileToSend
'.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Dim email As String
Dim Kcode As String
Dim OutAccounts As Outlook.Accounts
Dim OutAccount As Outlook.Account
Dim OutAccountTemp As Outlook.Account
strsql = "Select * from Qry_reminderemail"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strsql)
rs.MoveFirst
Do While Not rs.EOF
email = rs!email
Kcode = rs!Kcode
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
[B][COLOR=red]Set OutAccounts = OutApp.Application.Session.Accounts
[/COLOR][/B] For Each OutAccountTemp In OutAccounts
If (OutAccountTemp.SmtpAddress = "[EMAIL="sendfrom@email.com"]sendfrom@email.com[/EMAIL]") Then
Set OutAccount = OutAccountTemp
Exit For
End If
Next
With objEmail
.SendUsingAccount = OutAccount
.To = email
.CC = "[EMAIL="ccemail@email.net"]ccemail@email.net[/EMAIL]"
.Subject = Kcode & " - Submission reminder"
.Body = "Hello," & vbCrLf & vbCrLf & "Please be aware that you still have outstanding submissions." & vbCrLf & vbCrLf & "Regards," & vbCrLf & vbCrLf & "Sarah"
On Error Resume Next
.send
End With
'objOutlook.Quit
'Set objEmail = Nothing
rs.MoveNext
Loop
Set OutAccounts = [COLOR=Red][B]objOutlook.[/B][/COLOR]Application.Session.Accounts
If (OutAccountTemp.SmtpAddress = "[EMAIL="sendfrom@email.com"][COLOR=#0066cc]sendfrom@email.com[/COLOR][/EMAIL]") Then
Private Sub Emails_Click()
Dim email As String
Dim Kcode As String
Dim OutAccounts As Outlook.Accounts
Dim OutAccount As Outlook.Account
Dim OutAccountTemp As Outlook.Account
StrSql = "Select * from Qry_reminderemail"
Set db = CurrentDb()
Set rs = db.OpenRecordset(StrSql)
rs.MoveFirst
Do While Not rs.EOF
mySQL = "SELECT Tble_Remainingsubmissions.Service FROM tble_practice " & _
"INNER JOIN Tble_Remainingsubmissions ON tble_practice.KCode = Tble_Remainingsubmissions.KCode " & _
"WHERE Tble_Remainingsubmissions.KCode= '" & rs("KCode") & "' ;"
db.QueryDefs("SUBMISSION_Reminder").SQL = mySQL
Kcodestr = rs!Kcode
DoCmd.OutputTo acOutputQuery, "submission_reminder", acFormatXLS, "I:\Medical\Enhanced Services\ENHANCED SERVICES\2013-2014\reminders\" & Kcodestr & " - unsubmitted services.xls"
email = rs!email
Kcode = rs!Kcode
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
[COLOR=red] Set OutAccounts = objOutlook.Application.Session.Accounts[/COLOR]
[COLOR=red] For Each OutAccountTemp In OutAccounts[/COLOR]
[COLOR=red] If (OutAccountTemp.SmtpAddress = "[/COLOR][EMAIL="email@email.com"][COLOR=red]email@email.com[/COLOR][/EMAIL][COLOR=red]") Then[/COLOR]
[COLOR=red] Set OutAccount = OutAccountTemp [/COLOR]
[COLOR=red] Exit For[/COLOR]
[COLOR=red] End If[/COLOR]
[COLOR=red] Next[/COLOR]
With objEmail
.Importance = olImportanceHigh
[COLOR=red] .SendUsingAccount = OutAccount[/COLOR]
.To = email
.Subject = Kcode & " - Submission reminder"
.Body = Me!bodytxt
.Attachments.Add "I:\Medical\Enhanced Services\ENHANCED SERVICES\2013-2014\reminders\" & Kcodestr & " - unsubmitted services.xls"
On Error Resume Next
.send
End With
'objOutlook.Quit
'Set objEmail = Nothing
rs.MoveNext
Loop
End Sub
Option Compare Database
[COLOR=Red][B]Option Explicit [/B][/COLOR]
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Dim OutAccounts As Outlook.Accounts
Dim OutAccount As Outlook.Account
Dim OutAccountTemp As Outlook.Account
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
Set OutAccounts = objOutlook.Application.Session.Accounts
For Each OutAccountTemp In OutAccounts
If (OutAccountTemp.SmtpAddress = "email@address.net") Then
Set OutAccount = OutAccountTemp
Exit For
End If
Next
With objEmail
.SendUsingAccount = OutAccount