首页 > 解决方案 > Outlook 规则 + VBA:保存附件,然后将邮件移动到所选文件夹

问题描述

我需要对我收到的一些特定邮件使用规则。我必须将附件保存到特定文件夹 IE D:\TEST) 然后将邮件移动到附加到 Outlook 的另一个 FILE 邮箱文件中的特定子文件夹。

我找到并调整了代码以满足我的需要,但 Outlook 规则“麻木”,无法更改规则步骤执行的顺序:

如果味精命中规则:

将 MSG 移动到特定文件夹

然后运行脚本

所以总而言之,脚本没有找到味精,因为它是按规则移动的(我找不到反转顺序的选项并得到:

如果味精命中规则:

运行脚本

然后将 MSG 移动到特定文件夹因此解决方案是编写脚本并在触发规则时附加。

Public Sub SaveAttach(Item As Outlook.MailItem)

If Item.Attachments.Count > 0 Then

 Dim objAttachments As Outlook.Attachments
 Dim lngCount As Long
 Dim strFile As String
 Dim sFileType As String
 Dim strFolderpath As String
 Dim i As Long
 Dim myDestFolder As Outlook.Folder
 Set myDestFolder = myInbox.Folders("Mails").Folders("Exported")

 Set objAttachments = Item.Attachments
     lngCount = objAttachments.Count
   For i = lngCount To 1 Step -1
     strFile = objAttachments.Item(i).FileName
     strFolderpath = "D:\TEMP\"
     strFile = strFolderpath & strFile
     objAttachments.Item(i).SaveAsFile strFile
   Next i

Item.Move myDestFolder

End If
End Sub

将上面的代码放入 ThisOutlookSession 然后连接到规则“运行脚本”工作,但是如何扩展上面的代码以将我提取的附件的 MSG 移动到 IE Mails\Exported\ 中的另一个 Outlook 子文件夹?

编辑:

添加的行

将 myDestFolder 调暗为 Outlook.Folder

设置 myDestFolder = myInbox.Folders("Mails").Folders("Exported")

Item.Move myDestFolder

但是仍然有问题,并且在此模式下的 VBA(运行脚本作为规则的一部分)不运行调试并且看不到错误消息,它只是不起作用(我只能设置 msgbox“我在这里”来跟踪脚本的哪一部分不起作用: /

文件树看起来像这样

1 收件箱(DefaultIncomingMailsFile.ost (IMAP))

2 封邮件(LocalFile.pst)

2a 已导出(附件提取后应将邮件移动到的邮件中的子文件夹)

标签: vbaoutlookmoveattachmentrules

解决方案


但是如何扩展上面的代码以将我提取的附件的 MSG 移动到 IE Mailbox\Done\ 中的另一个 Outlook 子文件夹?

您需要使用将Microsoft Outlook 项目移动到新文件夹的类的Move方法。MailItem

Public Sub SaveAttach(Item As Outlook.MailItem)

  If Item.Attachments.Count > 0 Then
   Dim objAttachments As Outlook.Attachments
   Dim lngCount As Long
   Dim strFile As String
   Dim sFileType As String
   Dim strFolderpath As String
   Dim i As Long

   Set objAttachments = Item.Attachments
   lngCount = objAttachments.Count
   For i = lngCount To 1 Step -1
     strFile = objAttachments.Item(i).FileName
     strFolderpath = "D:\TEMP\"
     strFile = strFolderpath & strFile
     objAttachments.Item(i).SaveAsFile strFile
   Next i

   Item.Move yourTargetFolder

  End If
End Sub

推荐阅读