【问题标题】:VBA code in Outlook for extracting Excel attachmentsOutlook 中用于提取 Excel 附件的 VBA 代码
【发布时间】:2014-11-05 23:12:41
【问题描述】:

我正在尝试将所有 xlsx 扩展文件从我收件箱中的未读电子邮件中下载到一个文件夹中,并将这些电子邮件标记为已读,并根据时间戳给出唯一的命名约定。

到目前为止,我所做的只是修改我在网上找到的代码

Sub GetAttachments()
' This Outlook macro checks a the Outlook Inbox for messages
' with attached files (of any type) and saves them to disk.
' NOTE: make sure the specified save folder exists before
' running the macro.
    On Error GoTo GetAttachments_err
' Declare variables
    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim i As Integer
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    i = 0
' Check Inbox for messages and exit of none found
    If Inbox.Items.Count = 0 Then
        MsgBox "There are no messages in the Inbox.", vbInformation, _
               "Nothing Found"
        Exit Sub
    End If
' Check each message for attachments
    For Each Item In Inbox.Items
' Save any attachments found
        For Each Atmt In Item.Attachments

    If Right(Atmt.FileName, 4) = "xlsx" Then
            ' This path must exist! Change folder name as necessary.
                FileName = "C:\Users\vduraiswamy\Desktop\attachments\" & _
                    Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
                Atmt.SaveAsFile FileName
                End If
                Next Atmt
    Next Item
' Check filename of each attachment and save if it has "xls" extension
            i = i + 1
' Show summary message
    If i > 0 Then
        MsgBox "I found " & i & " attached files." _
        & vbCrLf & "I have saved them into the C:\Users\vduraiswamy\Desktop\attachments." _
        & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
    Else
        MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
    End If
' Clear memory
GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub
' Handle errors
GetAttachments_err:
    MsgBox "An unexpected error has occurred." _
        & vbCrLf & "Please note and report the following information." _
        & vbCrLf & "Macro Name: GetAttachments" _
        & vbCrLf & "Error Number: " & Err.Number _
        & vbCrLf & "Error Description: " & Err.Description _
        , vbCritical, "Error!"
    Resume GetAttachments_exit
End Sub

下载几个文件后,代码中途出现错误提示“无法对此类附件执行此操作”。

我还希望代码仅查看未读电子邮件。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:
    Sub GetAttachments()
    ' This Outlook macro checks a the Outlook Inbox for messages
    ' with attached files (of any type) and saves them to disk.
    ' NOTE: make sure the specified save folder exists before
    ' running the macro.
        On Error GoTo GetAttachments_err
    ' Declare variables
        Dim ns As NameSpace
        Dim Inbox As MAPIFolder
        Dim Item As Object
        Dim Atmt As Attachment
        Dim FileName As String
        Dim i As Integer
        Set ns = GetNamespace("MAPI")
        Set Inbox = ns.GetDefaultFolder(olFolderInbox)
        i = 0
    ' Check Inbox for messages and exit of none found
        If Inbox.Items.Count = 0 Then
            MsgBox "There are no messages in the Inbox.", vbInformation, _
                   "Nothing Found"
            Exit Sub
        End If
    ' Check each message for attachments
        For Each Item In Inbox.Items
            If Item.UnRead = True Then 'Add this for checking unread emails
                ' Save any attachments found
                        For Each Atmt In Item.Attachments
                            If (Right(Atmt.FileName, 4) = "xlsx") Or (Right(Atmt.FileName, 4) = ".xls") Then
                            ' This path must exist! Change folder name as necessary.
                                FileName = "C:\Documents and Settings\epadillo\Desktop\test\" & _
                                    Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
                                Atmt.SaveAsFile FileName
                                Item.UnRead = False 'Mark email item as read
                                i = i + 1
                            End If
                    Next Atmt
            End If
        Next Item
    
    ' Show summary message
        If i > 0 Then
            MsgBox "I found " & i & " attached files." _
            & vbCrLf & "I have saved them into the C:\Users\vduraiswamy\Desktop\attachments." _
            & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
        Else
            MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
        End If
    ' Clear memory
    GetAttachments_exit:
        Set Atmt = Nothing
        Set Item = Nothing
        Set ns = Nothing
        Exit Sub
    ' Handle errors
    GetAttachments_err:
        MsgBox "An unexpected error has occurred." _
            & vbCrLf & "Please note and report the following information." _
            & vbCrLf & "Macro Name: GetAttachments" _
            & vbCrLf & "Error Number: " & Err.Number _
            & vbCrLf & "Error Description: " & Err.Description _
            , vbCritical, "Error!"
        Resume GetAttachments_exit
    End Sub
    

    【讨论】:

    • VJ - 请试试这个代码。这将下载来自未读电子邮件的 xlsx 或 xls 附件,下载完成后,它会将电子邮件项目标记为已读。希望这有帮助....
    • OHWW 太棒了!非常感谢,我一直为此汗流浃背。我还想知道我是否能够根据发送邮件的时间而不是基于在 Item.Attachments 中的每个 Atmt If (Right(Atmt.FileName, 4) 下面的循环中的创建时间来重命名文件= "xlsx") or (Right(Atmt.FileName, 4) = ".xls") Then '这个路径一定存在!根据需要更改文件夹名称。 FileName = "C:\Documents and Settings\epadillo\Desktop\test\" & _ Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName Atmt.SaveAsFile FileName
    • 如果要使用收到邮件的时间,请将Format(Item.CreationTime, "yyyymmdd_hhnnss_")替换为这个Format(Item.ReceivedTime, "yyyymmdd_hhnnss_").... .
    【解决方案2】:

    尝试绕过不是文档的附件。

            End If
    nonvalidAttachment:
        Next Atmt
    Next Item
    

    通常您会使用数字,但这里的数字不是恒定的,但描述是恒定的。

    GetAttachments_err:
    
    If Err.Description = "Outlook cannot perform this action on this type of attachment." Then
        Err.Clear
        Resume nonvalidAttachment
    End if
    
    MsgBox "An unexpected error has occurred." _
            & vbCrLf & "Please note and report the following information." _
            & vbCrLf & "Macro Name: GetAttachments" _
            & vbCrLf & "Error Number: " & Err.Number _
            & vbCrLf & "Error Description: " & Err.Description _
            , vbCritical, "Error!"
    Resume GetAttachments_exit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      相关资源
      最近更新 更多