【问题标题】:Looking to only save .pdf attachments只想保存 .pdf 附件
【发布时间】:2018-12-12 23:34:57
【问题描述】:

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

【问题讨论】:

    标签: pdf outlook email-attachments


    【解决方案1】:

    请参考以下代码:

     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

    【讨论】:

      猜你喜欢
      • 2017-08-28
      • 2016-09-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      相关资源
      最近更新 更多