PDA

View Full Version : [SOLVED:] VBA Email - Reply to Address



PsYiOn
06-05-2008, 10:06 AM
I have setup a small email tool as part of my VBA program, here is the code.


Sub MailTool()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"

On Error Resume Next
With OutMail
.to = "Test@test.com"
.CC = ""
.BCC = ""
.Subject = "Test Subject"
.Body = strbody
.Send 'or use .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

The email sends fine it opens outlook etc but i need to be able to set the return address to somthing different. so wehn people reply it goes to a set address or even 2 set addresses. I can do this with outlook and since vba uses outlook is there a way for me to do it in VBA?

Kind Regards

PsYiOn

Oorang
06-05-2008, 10:13 AM
Public Sub TestMailTool()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
'On Error Resume Next
With OutMail
.To = "Test@test.com"
.ReplyRecipients.Add "foo@example.com"
.CC = ""
.BCC = ""
.Subject = "Test Subject"
.Body = strbody
.Send 'or use .Display
End With
'On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

lucas
06-05-2008, 10:30 AM
This seems to work:

Option Explicit
Sub MailTool()
' Is working in Office 2000-2007
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.SentOnBehalfOfName = "Joe@aol.com"
.to = "Test@test.com"
.CC = ""
.BCC = ""
.Subject = "Test Subject"
.Body = strbody
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Oorang
06-05-2008, 11:12 AM
Hey lucas,
In my company if you try to send on behalf of someone and you are not designated as someone who can do, the exchange server will slap your hands :) (So that may not work for everyone :))

lucas
06-05-2008, 11:16 AM
Good point Aaron but I can't get yours to fill in the from field...

Oorang
06-05-2008, 11:21 AM
Send it to yourself and then click reply :)

lucas
06-05-2008, 11:28 AM
ah...:thumb

Oorang
06-05-2008, 11:36 AM
The only thing I don't like about that approach is that if you click reply all it will send to the reply-recipient and the sender both.

PsYiOn
06-05-2008, 11:46 AM
Works very well, i did not use the send on behalf of, but i think my whole team has access to do so but ideally i would need to be having the replies come back to more than 1 person so its perfect for my needs

thanks very much guys

how do i mark the thread as closed? I just read your sig

Oorang
06-05-2008, 11:59 AM
At the very top you should see a menu called "Thread Tools". One of it's options is to "Mark Thread as Solved".

PsYiOn
06-05-2008, 12:38 PM
Done :D and next time i will use the vba tags :D