简体   繁体   English

根据主题行转发电子邮件

[英]Forward email based on subject line

I'm trying to forward emails from my company's Outlook to an email account outside of our company.我正在尝试将电子邮件从我公司的 Outlook 转发到我们公司外部的电子邮件帐户。 I have been given the ok to do this.我被允许这样做。

I'd like to forward any email that contains "Excel Friday" in the subject line.我想转发主题行中包含“Excel Friday”的任何电子邮件。

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    ' default local Inbox
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub
 
Private Sub Items_ItemAdd(ByVal Item As Object)
    On Error GoTo ErrorHandler
    Dim Msg As Outlook.MailItem
    If TypeName(Item) = "MailItem" Then
        Set Msg = Item
        If Msg.Subject = "Excel Friday" Then
            Dim myMail As Outlook.MailItem
            Set myMail = Msg.Reply
            myMail.To = "xxxxxx@fakemail.com"
            myMail.Display
        End If
    End If
ProgramExit:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit
End Sub

I'd like to forward any email that contains "Excel Friday" in the subject line to another email address.我想将主题行中包含“Excel Friday”的任何电子邮件转发到另一个电子邮件地址。

But in the code you check for the exact match of the subject line:但是在代码中,您检查主题行是否完全匹配:

If Msg.Subject = "Excel Friday" Then

Instead you need to look for a substring.相反,您需要查找子字符串。 To find the position of a substring in a string, use Instr function.要查找字符串中子字符串的位置,请使用Instr函数。

If Instr(Msg.Subject, "Excel Friday") Then

Also I have noticed that you use the Reply method:我还注意到您使用了 Reply 方法:

Set myMail = Msg.Reply 

Use the Forward method instead:请改用Forward方法:

Set myMail = Msg.Forward

And then use the Send method.然后使用发送方法。

 myMail.Recipients.Add "Eugene Astafiev" 

 myMail.Send 

Be aware, the code is based on the ItemAdd event handler.请注意,该代码基于ItemAdd事件处理程序。 This event is not fired when a large number of items are added to the folder at once (more than 16).一次将大量项目(超过 16 个)添加到文件夹时,不会触发此事件。

You can do this using a Run a Script rule您可以使用运行脚本规则执行此操作

Sub ChangeSubjectForward(Item As Outlook.MailItem)
    Item.Subject = "Test"
 Item.Save

Set olForward = Item.Forward
olForward.Recipients.Add "Jasonfish11@domain.com"

olForward.Send

End Sub

If a vba you can run on all messages in a folder at any time.如果是 vba,您可以随时对文件夹中的所有邮件运行。

Paste into ThisOutlookSession and run粘贴到 ThisOutlookSession 并运行

Sub ChangeSubjectThenSend()
Dim olApp As Outlook.Application
Dim aItem As Object

Set olApp = CreateObject("Outlook.Application")
Set mail = olApp.ActiveExplorer.CurrentFolder

For Each aItem In mail.Items
      aItem.Subject = "New Subject"
    aItem.Save

    Set olForward = aItem.Forward
    olForward.Recipients.Add "Jasonfish11@domain.com"
    olForward.Send

Next aItem
End Sub

source Link 源链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM