我找到了这个 VBA 代码来自动转发特定文件夹中的邮件,它很棒,但每次都会发送所有邮件,我只需要发送该文件夹中的新邮件...有什么想法吗?这是我找到的代码:
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
End If
Next
Set MyFolder = Nothing
Set notMyItems = Nothing
Set notReplyingToMe = Nothing
End Sub
答案1
您应该将此代码定义为 ItemAdd 事件,它会针对所有新邮件触发。您的例程会对文件夹中的所有邮件起作用。
此示例取自帖子 如何为共享收件箱中的新邮件触发 Outlook 宏 (并且回复而不是转发):
在此 Outlook 会话中
Option Explicit ' Consider this mandatory ' Tools | Options | Editor tab ' Require Variable Declaration ' If desperate declare as Variant Public WithEvents olItems As Items Private Sub Application_Startup() Set olItems = Session.Folders("[email protected]").Folders("Inbox").Items End Sub Private Sub olItems_ItemAdd(ByVal Item As Object) Dim olReply As MailItem If Item.Class = olMail Then If Len(Item.ConversationIndex) > 44 Then Exit Sub Else Set olReply = Item.reply With olReply .Body = "Reply to first email." .Display End With End If End If End Sub
答案2
您可以在代码中使用上面 harrymc 指示的“读取”状态。
仅当邮件状态为“未读”时才转发该邮件。将状态从“未读”更改为“已读”添加到处理代码中。
由于状态的变化,下次运行代码时,您转发的电子邮件项目将显示为“已读”,因此将被排除在转发之外。
为此目的,更改的程度是对一行进行一处修改并添加一行代码。
第一次运行代码之前,根据需要更改目标文件夹中每封邮件的状态。