【问题标题】:VBA save email attachments with pdf extension to folderVBA将带有pdf扩展名的电子邮件附件保存到文件夹
【发布时间】:2014-11-26 06:08:15
【问题描述】:

我正在使用以下代码将电子邮件中的附件保存到文件夹中,现在我想添加一个 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

【问题讨论】:

    标签: vba attachment


    【解决方案1】:

    您需要遍历 objMsg 上的 attachments 集合以查找 PDF。

    这看起来像:

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

    只要确保在顶部贴上 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
    

    【讨论】:

    • 谢谢,但你知道我的代码中的什么地方吗?我会把这个放在哪里,因为我在没有 for 的情况下不断收到编译错误
    • 你应该把它放在你的For Each objMsg...循环中
    • 我正在这样做,但 stile 出现编译错误
    • 确保您的两个For Each... 循环的末尾都有一个Next。此外,确保任何If 语句都以End If 结尾。
    • 我已经添加了您示例中的完整代码以及额外的 For 循环,以遍历附件并在保存前测试它们是否为“PDF”。
    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    相关资源
    最近更新 更多