首页 > 解决方案 > 只想保存 .pdf 附件

问题描述

我只是想将所选电子邮件附件中的 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

标签: pdfoutlookemail-attachments

解决方案


请参考以下代码:

 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 将带有 pdf 扩展名的电子邮件附件保存到文件夹


推荐阅读