I want to create and send an email from Excel but I want replies to go to a different account to the default Outlook mail account. The following code works perfectly if I do set the Object Library
Sub email()
Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Dim title As String
Dim olAccounts As Outlook.Accounts
Dim olAccount As Outlook.account
'title = Sheets("Data").Range("B124").Value & " " & Sheets("Data").Range("B4").Value
Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:\Users\Paul\AppData\Roaming\Microsoft\Templates\Police URN.oft")
With MyItem
.To = Sheets("Data").Range("B128").Value
.Subject = title
.Body = "Please confirm the outcome for" & Chr(13) & _
Chr(13) & _
"Surname - " & Sheets("Data").Range("B4").Value & Chr(13) & _
"Forenames - " & Sheets("Data").Range("B124").Value & Chr(13) & _
"Date of Birth - " & Sheets("Data").Range("B10").Value & Chr(13) & _
"Thanks"
.SentOnBehalfOfName = "accountname@aol.com"
.Send
End With
Set MyItem = Nothing
Set myOlApp = Nothing
End Sub
However, I cannot set the Microsoft Outlook object library in the "Tools" "References" as some of my users have different versions of Outlook running.
So I have developed this way instead
Dim oOutlook As Object
On Error GoTo muststart
Set oOutlook = GetObject(, "Outlook.application")
Dim title As String
title = Sheets("Data").Range("B124").Value & " " & _
Sheets("Data").Range("B4").Value & _
" / Custody Ref " & Sheets("Data").Range("B17").Value
With CreateObject("Outlook.Application").CreateItem(0)
.To = Sheets("Data").Range("B122").Value
.Subject = title
.Body = "Please confirm the outcome for" & Chr(13) & _
Chr(13) & _
"Surname - " & Sheets("Data").Range("B4").Value & Chr(13) & _
"Forenames - " & Sheets("Data").Range("B124").Value & Chr(13) & _
"Date of Birth - " & Sheets("Data").Range("B10").Value & Chr(13) & _
"Thanks"
.SentOnBehalfOfName = "accountname@aol.com"
'.Send
.Display
End With
muststart:
If oOutlook Is Nothing Then
MsgBox "Start Outlook first"
Else
End If
End Sub
The second vesrion appears to work perfectly but when I actually send the email it goes from the Default account rather than the SentOnBehalfOfName account
Any help would be much appreciated
Bookmarks