2014-11-05 138 views
0

我正嘗試將所有xlsx擴展名文件從我的收件箱中的未讀電子郵件下載到文件夾中,並將這些電子郵件標記爲已讀,並根據時間戳記給出一個唯一的命名約定。用於提取Excel附件的Outlook中的VBA代碼

直至現在我已經實現是修改我發現了一個碼在線

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 

的代碼是給它說「無法履行對這種類型的附件此操作」錯誤下載一對夫婦後中途的文件。

我也想看看只有未讀電子郵件的代碼。

回答

2
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 
+0

VJ - 請試試這段代碼。這將下載來自未讀電子郵件的xlsx或xls附件,並且當下載完成時,它會將該電子郵件項目標記爲已讀。 HOPE這對你有幫助... – Ohw 2014-11-06 04:58:24

+0

OHWW 太棒了!非常感謝,我一直在爲此而出汗。 我也想知道是否可以根據發送郵件的時間重新命名文件,而不是根據下面的移動環境中的創建時間來重命名文件 For Each Atmt In Item.Attachments If(Right(Atmt。 FileName,4)=「xlsx」)或(Right(Atmt.FileName,4)=「.xls」)Then'This path must exist!根據需要更改文件夾名稱。 FileName =「C:\ Documents and Settings \ epadillo \ Desktop \ test \」&_ Format(Item.CreationTime,「yyyymmdd_hhnnss_」)&Atmt.FileName Atmt.SaveAsFile FileName – 2014-11-10 21:08:38

+0

如果您想使用收到電子郵件的時間,請用此格式(Item.ReceivedTime,「yyyymmdd_hhnnss _」)替換格式(Item.CreationTime,「yyyymmdd_hhnnss_」)..... – Ohw 2014-11-11 05:40:28

0

嘗試繞過不是文檔的附件。

 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