VBA使用pdf扩展名将电子邮件附件保存到文件夹

时间:2014-10-01 11:50:21

标签: vba attachment

我使用以下代码将电子邮件中的附件保存到文件夹中,现在我想添加一个if子句或条件,表示只保存带有.pdf扩展名的附件。

有人可以告诉我如何更改我的代码以实现此目的,提前感谢

Public Sub SaveAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String

    ' Get the path to your My Documents folder
    On Error Resume Next

    ' Instantiate an Outlook Application object.
    Set objOL = CreateObject("Outlook.Application")

    ' Get the collection of selected objects.
    Set objSelection = objOL.ActiveExplorer.Selection

' The attachment folder needs to exist
' You can change this to another folder name of your choice

    ' Set the Attachment folder.
    strFolderpath = "\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\TEST\"

    ' Check each selected item for attachments.
    For Each objMsg In objSelection

    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count

    If lngCount > 0 Then

    ' Use a count down loop for removing items
    ' from a collection. Otherwise, the loop counter gets
    ' confused and only every other item is removed.

    For i = lngCount To 1 Step -1

    ' Get the file name.
    strFile = objAttachments.Item(i).FileName

    ' Combine with the path to the Temp folder.
    strFile = strFolderpath & strFile

    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strFile

    Next i
    End If

    Next

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

您希望遍历attachments上的objMsg集合以查找PDF。

这看起来像是:

For each objAttachment in objMsg.Attachments
     if Right(objAttachment.FileName, 3) = "pdf" then
          objAttachment.SaveAsFile strFolderPath & strFile
     end if
Next objAttachment

请确保您在顶部使用以下内容进行decalre objAttachment: Dim objAttachment as Attachment

更新了您示例中的完整代码:

Public Sub SaveAttachments()
    Dim objOL As Outlook.Application
    Dim objMsg As Outlook.MailItem 'Object          
    Dim strFile As String
    Dim strFolderpath As String
    Dim strDeletedFiles As String

    ' Get the path to your My Documents folder
    On Error Resume Next

    ' Instantiate an Outlook Application object.
    Set objOL = CreateObject("Outlook.Application")

    ' Get the collection of selected objects.
    Set objSelection = objOL.ActiveExplorer.Selection

    ' The attachment folder needs to exist
    ' You can change this to another folder name of your choice
    ' Set the Attachment folder.
    strFolderpath = "\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\TEST\"

    ' Check each selected item for attachments.
    For Each objMsg In objSelection
        For each objAttachment in objMsg.Attachments
            if Right(objAttachment.FileName, 3) = "pdf" then                

                    ' Append the file name to the folder.
                    strFile = strFolderpath & objAttachment.FileName

                    ' Save it
                    objAttachments.Item(i).SaveAsFile strFile                   
            end if
        Next objAttachment
    Next objMsg

ExitSub:
    Set objAttachments = Nothing
    Set objMsg = Nothing
    Set objSelection = Nothing
    Set objOL = Nothing
End Sub