【问题标题】:Forward email based on subject line根据主题行转发电子邮件
【发布时间】:2020-12-08 23:51:57
【问题描述】:

我正在尝试将电子邮件从我公司的 Outlook 转发到我们公司以外的电子邮件帐户。我已获准这样做。

我想转发主题行中包含“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

【问题讨论】:

  • 仅供参考,您可以使用基本的 Rules & Alerts 规则来做到这一点
  • 为什么需要使用 VBA 宏? Outlook 允许手动完成工作。

标签: vba email outlook


【解决方案1】:

我想将任何主题行中包含“Excel Friday”的电子邮件转发到另一个电子邮件地址。

但是在代码中你检查主题行的完全匹配:

If Msg.Subject = "Excel Friday" Then

相反,您需要查找子字符串。要查找字符串中子字符串的位置,请使用 Instr 函数。

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

我也注意到你使用了回复方法:

Set myMail = Msg.Reply 

改用Forward 方法:

Set myMail = Msg.Forward

然后使用 Send 方法。

 myMail.Recipients.Add "Eugene Astafiev" 

 myMail.Send 

请注意,代码基于ItemAdd 事件处理程序。一次将大量项目(超过 16 个)添加到文件夹时不会触发此事件。

【讨论】:

  • 对不起,我粘贴了我的旧代码迭代之一(我猜我没有点击保存)。我确实将它更改为 msg.forward 和 mymail.send 我已经尝试过 Instr,但我仍然无法让它工作。我如何知道我搜索的“默认”收件箱是哪个文件夹?有没有可能我认为默认收件箱真的不是?抱歉,我刚刚进入了来自 excel 世界的 Outlook 宏,所以很多 Outlook 应用程序对我来说都是新的。
  • 我建议从 MSDN 中的 Getting Started with VBA in Outlook 2010 文章开始。尝试将Debug.Print 语句添加到代码中,看看幕后发生了什么。
【解决方案2】:

您可以使用运行脚本规则来做到这一点

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

如果是 vba,您可以随时对文件夹中的所有邮件运行。

粘贴到 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

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多