Consulting

Results 1 to 11 of 11

Thread: VBA Email - Reply to Address

  1. #1

    VBA Email - Reply to Address

    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

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location

    Talking

    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 )
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Good point Aaron but I can't get yours to fill in the from field...
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Send it to yourself and then click reply
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    ah...
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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.
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  9. #9
    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

  10. #10
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    At the very top you should see a menu called "Thread Tools". One of it's options is to "Mark Thread as Solved".
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  11. #11
    Done and next time i will use the vba tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •