配置Outlook rule实现匹配[XYZ]编号的审批关联邮件批量移动的可行性咨询
Absolutely possible! Let me walk you through two solid ways to set this up in Outlook—one using built-in rules (no coding needed) and a more precise VBA script option for full control, depending on how much customization you want.
Option 1: Use Outlook's Built-in Rules (No Scripting)
This works if you’re comfortable with Outlook’s default "related messages" matching, though it’s a bit less precise than the script method:
- Open Outlook and go to File > Manage Rules & Alerts > New Rule.
- Select "Apply rule on messages I receive" and click Next.
- Under conditions, check "with specific words in the subject". Click "specific words" and add two phrases:
Approval forandhas been user approved.(make sure to include the spaces to avoid false matches). Click Next. - Now add two actions:
- Check "move it to the specified folder" and pick your target folder (create one first if you don’t have it).
- Add a second action: "find all related messages and move them to the specified folder", then select the same target folder.
- Click "related messages" to tweak the matching logic. Choose "messages with the same subject keywords" and adjust it to look for the [XYZ] number specifically. For example, set it to match any message where the subject contains the string between
Approval forandhas been user approved.from the incoming confirmation email. - Finish the rule setup, making sure to apply it to existing messages if needed.
Option 2: Use a VBA Script (Precise Control)
If you want rock-solid matching that always grabs the exact [XYZ] pair of emails, a quick VBA script is the way to go. Here’s how:
- Open Outlook and press
Alt + F11to launch the VBA Editor. - In the left pane, expand "Microsoft Outlook Objects" and double-click "ThisOutlookSession".
- Paste this script (replace
"Approval Completed"with your actual target folder name):
Sub MoveApprovalEmails(Item As Outlook.MailItem) ' Check if this is the approval confirmation email If InStr(Item.Subject, "Approval for ") > 0 And InStr(Item.Subject, " has been user approved.") > 0 Then ' Extract the [XYZ] number from the subject Dim xyzStart As Integer, xyzEnd As Integer xyzStart = InStr(Item.Subject, "Approval for ") + Len("Approval for ") xyzEnd = InStr(Item.Subject, " has been user approved.") Dim xyzNumber As String xyzNumber = Trim(Mid(Item.Subject, xyzStart, xyzEnd - xyzStart)) ' Set up the target folder (create it if it doesn't exist) Dim targetFolder As Outlook.Folder On Error Resume Next Set targetFolder = Session.GetDefaultFolder(olFolderInbox).Folders("Approval Completed") On Error GoTo 0 If targetFolder Is Nothing Then Set targetFolder = Session.GetDefaultFolder(olFolderInbox).Folders.Add("Approval Completed") End If ' Find all emails in your inbox with the matching [XYZ] number Dim objNS As Outlook.NameSpace Dim objFolder As Outlook.Folder Dim filter As String Set objNS = Session.GetNamespace("MAPI") Set objFolder = objNS.GetDefaultFolder(olFolderInbox) ' Build a filter to match any subject containing the XYZ number filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%" & xyzNumber & "%'" Dim items As Outlook.Items Set items = objFolder.Items.Restrict(filter) ' Move all matching emails to the target folder Dim mailItem As Outlook.MailItem For Each mailItem In items ' Skip the current email to avoid duplicate moves (we'll move it separately) If mailItem.EntryID <> Item.EntryID Then mailItem.Move targetFolder End If Next ' Move the confirmation email itself Item.Move targetFolder End If End Sub
- Save the script (you may need to enable macro support in Outlook: File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable all macros—just note this is for trusted scripts only).
- Go back to Rules & Alerts and create a new rule:
- Trigger: "Apply rule on messages I receive" with specific words
Approval forandhas been user approved.in the subject. - Action: "run a script" and select the
MoveApprovalEmailsscript you just created.
- Trigger: "Apply rule on messages I receive" with specific words
- Save the rule, and you’re all set!
This script will automatically extract the [XYZ] number from the confirmation email, find the original approval request with the same number, and move both to your target folder in one go. The first email stays in your inbox until the confirmation arrives—exactly what you wanted.
备注:内容来源于stack exchange,提问作者opilione

