选择多封电子邮件并保存特定的附件

时间:2019-01-14 04:10:26

标签: vba outlook outlook-vba

我拥有在Internet上找到的这段代码,该代码将附件从选定的电子邮件保存到特定的文件夹。我收到的发送到该特定邮箱的所有电子邮件均为PDF。

问题:只要电子邮件签名中有徽标或电子邮件正文上的任何图像,它还会将所有这些图像另存为单独的文件。 实际上有没有办法更改此代码,以便仅保存所选电子邮件中的PDF文件?

Sub SaveAttachments()
Dim olSelection As Selection
Dim olMail As Object
Dim olAttachments As Attachments
Dim FileCount As Long, i As Long
Dim SaveFolderPath As String

On Error GoTo errHandle
SaveFolderPath = "path"
Set olSelection = ActiveExplorer.Selection

For Each olMail In olSelection
    If TypeName(olMail) = "MailItem" Then
        Set olAttachments = olMail.Attachments
        FileCount = olAttachments.Count

        If FileCount > 0 Then
            For i = FileCount To 1 Step -1
                olAttachments.item(i).SaveAsFile SaveFolderPath & olAttachments.item(i).FileName
            Next i
        End If

        Set olAttachments = Nothing
    End If
Next olMail

Exit Sub
   errHandle:
   MsgBox "Error: " & Err.Description, vbExclamation
End Sub

1 个答案:

答案 0 :(得分:1)

只需查看文件名中的最后4个字符

示例

        If LCase$(Right$(olAttachments.Item(i).FileName, 4)) = ".pdf" Then
            olAttachments.Item(i).SaveAsFile SaveFolderPath & olAttachments.Item(i).FileName
        End If