自动保存 Outlook 邮件的附件
在工作中定期或不定期会收到一些数据文件,然后要将它们的附件保存到自己
的电脑上,下面演示如何让 Outlook 自动做这件事情。
首先,下面的 SaveAttach 函数可以保存附件中的 docx 文档到 D 盘根目录下。
用 ALT+F11 打开 VBA 编辑器(注意:是打开 Outlook 的 VBE 编辑窗口),
插入下述代码:
Public Sub SaveAttach(Item As Outlook.MailItem)
SaveAttachment Item, "D:\", "*.docx"
' MsgBox "附件已保存"
End Sub
' 保存附件
' path 为保存路径,condition 为附件名匹配条件
Private Sub SaveAttachment(ByVal Item As Object, path$,
Optional condition$ = "*")
Dim olAtt As Attachment
Dim i As Integer
If Item.Attachments.Count > 0 Then
For i = 1 To Item.Attachments.Count
Set olAtt = Item.Attachments(i)
' save the attachment
If olAtt.FileName Like condition Then
olAtt.SaveAsFile path & olAtt.FileName
End If
Next
End If
Set olAtt = Nothing
End Sub