希望仅保存.pdf附件

时间:2018-12-12 23:34:57

标签: pdf outlook email-attachments

我只是想将选择的电子邮件附件中的PDF保存到计算机上的文件夹中。现在,下面的代码将保存所有附件,例如JPG和htm项目。是否在错误的位置选择了PDF?不管我在哪里放置用于选择PDF的代码,到处玩耍后似乎并没有真正选择PDF

 Sub SavePDFAttachments()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim l As Long
Dim lngCount As Long
Dim tlngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim finalpath As String

    On Error Resume Next

    ' Instantiate an Outlook Application object.
    Set objOL = Application

    ' Get the collection of selected objects.
    Set objSelection = objOL.ActiveExplorer.Selection
    ' Set the Attachment folder.
    strFolderpath = "T:"

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

    Set objAttachments = objMsg.Attachments

    ' Pull PDFs only
    For Each objAttachment In objMsg.Attachments
    If Right(objAttachment.FileName, 3) = "pdf" Then
    objAttachment.SaveAsFile strFolderpath & strFile
    End If

    Next objAttachment

    lngCount = objAttachments.count

    If lngCount > 0 Then

    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 :(得分:0)

请参考以下代码:

 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

保存PDF代码:

if Right(objAttachment.FileName, 3) = "pdf" then

有关更多信息,请参见链接:VBA save email attachments with pdf extension to folder