VBA /规则更改传入电子邮件的主题行,然后移动到文件夹



我想编写一个脚本,该脚本将接收特定的传入电子邮件,更改其主题行,然后将该电子邮件移动到特定文件夹。

我可以找到在邮件本身打开时更改主题行的脚本,但是当绑定到"移动到文件夹"规则时,主题不会更改。

我已经尝试过此代码,它将文本添加到我打开的消息的主题行中。

Sub myRuleMacro(Item As Outlook.MailItem)
Sub AddToEndOfSubjectLine()
If ActiveInspector Is Nothing Then Exit Sub
ActiveInspector.CurrentItem.Save
ActiveInspector.CurrentItem.subject = ActiveInspector.CurrentItem.subject & " HELLO!"
End Sub
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
End Sub

这里有一个例子,试试看

Public Sub Example(Item As Outlook.MailItem)
Debug.Print Item.Subject ' Print on Immediate Window
Item.Subject = Item.Subject & " HELLO!"
Item.Save
End Sub

若要测试脚本,请尝试选择"电子邮件"并运行以下代码

Private Sub TestMsg()
Dim olMsg As Outlook.MailItem
Set olMsg = ActiveExplorer.Selection.Item(1)
Example olMsg
End Sub

项目是传入的邮件。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
Sub myRuleMacro(Item As Outlook.MailItem)
' Rules Wizard is misleading
' The move has to be in the code, not in the rules
Dim inboxFolder As folder
Dim mailboxFolder As folder
Dim destFolder As folder
Dim destFolderStr As String
Item.Subject = Item.Subject & " HELLO!"
Debug.Print Item.Subject
' One way to reference a folder
'  is to walk the folder tree from a known folder
Set inboxFolder = Session.GetDefaultFolder(olFolderInbox)
Set mailboxFolder = inboxFolder.Parent
Debug.Print "Mailbox associated with the default inbox: " & mailboxFolder
destFolderStr = "name of folder at same level as inbox"
Set destFolder = mailboxFolder.folders(destFolderStr)
Item.Move destFolder
End Sub

您可以使用打开的邮件进行测试。

Private Sub simulateIncomingMail_BeforeTrueTest()
myRuleMacro ActiveInspector.CurrentItem
End Sub

相关内容

最新更新