Outlook VBscript to forward emails in a folder

流过昼夜 提交于 2020-01-24 15:12:06

问题


I have a specific user that really wants to be able 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.

We can't give them permissions to send as everyone because not all users are internal so I am trying to make a VBScript to run in Outlook that will send forward all emails in a specified folder but change the reply address so that they don't have to manually put it in every time.

I'm fairly new to VB scripts so it's probably something pretty basic that I am missing.

This is what I am trying but it doesn't work:

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("me@us.com").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 = "test@us.com"
        ObjMail.Send
    Next
End Sub

回答1:


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 = "test@us.com"
    .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("me@us.com").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 = "test@us.com"
           .Send

        End With

        notMyItems(i).Delete

    End If

Next

Set MyFolder = = Nothing
Set notMyItems = Nothing
Set notReplyingToMe = Nothing

End Sub


来源:https://stackoverflow.com/questions/20576916/outlook-vbscript-to-forward-emails-in-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!