lrobbo314
Well-known Member
- Joined
- Jul 14, 2008
- Messages
- 3,957
- Office Version
- 365
- Platform
- Windows
I have a couple of accounts in Outlook, and I have been trying to change which account emails are being sent from when creating the emails in Access. I found a way, but it only seems to work with the early binding method.
That does work, but this is going to be used by people on different versions of Access, so I have to use late binding. I used the same method as above, but it still creates the email from my primary account and not the account I'm going for. I've debugged it and made sure that the account variable, acct, is the correct account.
Any idea as to why this might be and how to fix it?
Code:
Sub CreateEmail(rName As String, Brass As String, Agents As String)Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim tMail As Outlook.MailItem
Dim objFolder As Outlook.MAPIFolder
Dim fName As String
Dim acct As Outlook.Account
Set olApp = New Outlook.Application
Set objns = Outlook.GetNamespace("MAPI")
Set objInbox = objns.GetDefaultFolder(olFolderInbox)
Set objFolder = objns.Folders("xxxx@cbp.dhs.gov")
Set tMail = Outlook.CreateItem(olMailItem)
fName = Environ("UserProfile") & "\Desktop\" & "Past Due Vehicles.pdf"
For Each a In olApp.Session.Accounts
If a.SmtpAddress = "xxxx@cbp.dhs.gov" Then
Set acct = a
Exit For
End If
Next a
DoCmd.OutputTo acOutputReport, rName, acFormatPDF, fName, False
With tMail
.SendUsingAccount = acct
.Subject = "Vehicles Out Past Due Report"
.Body = "You are receiving this email because you have a vehicle that has been checked out too long." & vbCrLf & "Vehicles Must be turned in at the end of every shift." & vbCrLf & vbCrLf & "See Attachment."
.Attachments.Add fName
.Display
End With
Kill fName
End Sub
That does work, but this is going to be used by people on different versions of Access, so I have to use late binding. I used the same method as above, but it still creates the email from my primary account and not the account I'm going for. I've debugged it and made sure that the account variable, acct, is the correct account.
Code:
Sub CreateEmailLB(rName As String, Brass As String, Agents As String)Dim olApp As Object
Dim tMail As Object 'Outlook.MailItem
Dim objFolder As Object 'Outlook.MAPIFolder
Dim acct As Object
Dim fName As String
Set olApp = CreateObject("Outlook.Application")
Set objns = olApp.GetNamespace("MAPI")
Set objInbox = objns.GetDefaultFolder(olFolderInbox)
Set objFolder = objns.Folders("xxxx@cbp.dhs.gov")
Set tMail = olApp.CreateItem(olMailItem)
For Each a In olApp.Session.Accounts
If a.SmtpAddress = "xxxx@cbp.dhs.gov" Then
Set acct = a
Exit For
End If
Next a
fName = Environ("UserProfile") & "\Desktop\" & "Past Due Vehicles.pdf"
DoCmd.OutputTo acOutputReport, rName, acFormatPDF, fName, False
With tMail
.SendUsingAccount = acct
.Recipients.Add Agents
.CC = Brass
.Subject = "Vehicles Out Past Due Report"
.Body = "You are receiving this email because you have a vehicle that has been checked out too long." & vbCrLf & "Vehicles Must be turned in at the end of every shift." & vbCrLf & vbCrLf & "See Attachment."
.Attachments.Add fName
.Display
End With
Kill fName
End Sub
Any idea as to why this might be and how to fix it?