Search code examples
vbaemailoutlook

How to forward emails in a folder and change the reply to address to the original sender?


I have a user who wants to redirect any email to other people in their department so that when that person replies to the email it will go back to the person who originally sent it.

I am trying to make VBA code to forward all emails in a specified folder and change the reply to address so that they don't have to manually put it in every time.

Sub SendFolder()
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.NameSpace
    Dim MyFolder As Outlook.MAPIFolder
    Dim ObjMail As Outlook.MailItem

    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set MyFolder = Application.Session.Folders("[email protected]").Folders("test")

    For i = MyFolder.Items.Count To 0 Step -1
        Set ObjMail.Subject = MyFolder.Itmes(i).Subject
        Set ObjMail.ReplyRecipients = MyFolder.Itmes(i).ReplyRecipients
        Set ObjMail.Body = MyFolder.Itmes(i).Body
        Set ObjMail.Attachments = MyFolder.Itmes(i).Attachments
        Set ObjMail.BodyFormat = MyFolder.Itmes(i).BodyFormat
        Set ObjMail.To = "[email protected]"
        ObjMail.Send
    Next
End Sub

Solution

  • You are missing

    Set ObjMail = Application.CreateItem(olMailItem)

    Then your code would become

    With ObjMail
        .Subject = MyFolder.Itmes(i).Subject
        .ReplyRecipients = MyFolder.Items(i).ReplyRecipients
        .Body = MyFolder.Items(i).Body
        .Attachments = MyFolder.Items(i).Attachments
        .BodyFormat = MyFolder.Items(i).BodyFormat
        .To = "[email protected]"
        .Send
    End with
    

    It it runs now, the ReplyTo does not change.

    You will want to set the ObjMail's ReplyRecipients property

    Something like .ReplyRecipients.Add MyFolder.Items(i).SenderEmailAddress

    To simplify the issue, .Forward the mail as is, and set only the ReplyRecipients property.

    Check out this alternative. The mail is sent as an attachment. The receiver automatically replies to the original sender.

    Untested

    Sub SendFolderItemsAsAttachments()
    
    ' Run this VBA code while in Outlook
    
    Dim MyFolder As MAPIFolder
    Dim notMyItems as Items
    Dim notReplyingToMe as mailitem
    
    Dim i as long
    
    Set MyFolder = Application.Session.Folders("[email protected]").Folders("test")
    
    Set notMyItems = MyFolder.Items
    
    For i = notMyItems.Count To 1 Step -1
    
       If TypeOf notMyItems(i) Is MailItem Then
    
           Set notReplyingToMe = Application.CreateItem(olMailItem)
    
           With notReplyingToMe
    
               .Subject = notMyItems(i).Subject & " - " & _
                          notMyItems(i).SenderName
               .HTMLBody = "Redirecting for your action."
               .Attachments.Add notMyItems(i), olEmbeddeditem
               .To = "[email protected]"
               .Send
    
            End With
    
            notMyItems(i).Delete
    
        End If
    
    Next
    
    Set MyFolder = = Nothing
    Set notMyItems = Nothing
    Set notReplyingToMe = Nothing
    
    End Sub