【问题标题】:Outlook rule + VBA: Save attachment then move message to selected folderOutlook 规则 + VBA:保存附件,然后将邮件移动到所选文件夹
【发布时间】:2019-07-16 21:25:06
【问题描述】:

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

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

如果消息命中规则:

将 MSG 移动到特定文件夹

然后运行脚本

所以总而言之,脚本没有找到 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 移动到 I.E. 中的另一个 Outlook 子文件夹。邮件\导出\?

编辑:

添加的行

将 myDestFolder 调暗为 Outlook.Folder

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

Item.Move myDestFolder

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

文件树如下所示

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

2 邮件(LocalFile.pst)

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

【问题讨论】:

标签: vba outlook move attachment rules


【解决方案1】:

但是如何扩展上面的代码以将我提取的附件的 MSG 移动到 I.E. 中的另一个 Outlook 子文件夹。邮箱\完成\?

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

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

【讨论】:

  • 您好,感谢您的回答。我尝试按照您的建议使用此代码行,但存在一个非常复杂的问题,因为未定义“yourTargetFolder”我还尝试添加 Set myDestFolder = myInbox.Folders("Recieved Mails").Folders("Exported") 所以我的代码看起来像我编辑过的。还有其他事情 - 目标文件夹不是主文件夹(outlook 文件)请检查编辑的主帖子(脚本现在看起来像这样,但只有在我评论添加的行时才有效:(
猜你喜欢
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多