2014-12-07 4 views
0

Excel에서받은 편지함의 하위 폴더에서 첨부 파일을 다운로드하려면 다음 코딩을 사용하십시오. 제대로 작동하지만 읽을 수없는 전자 메일에서만 attachemnts를 다운로드 할 수 있습니까?코드를 변경하여 첨부 파일을 다운로드하십시오.

나에게 줄 수있는 조언이나 도움을 주시면 감사하겠습니다.

내가 그렇게 생각하는 경우 objItem.unread Then ...하지만 코딩에 구현하는 방법이 확실하지 않습니까? 내가 UnreadMailItem의의 속성이 있는지 확실하지 않습니다하지만이 같은

' public objects moved from Userform code module 
Public OutlookApp As New Outlook.Application 
Public oNameSpace As Namespace 
Public oFldrList  As Outlook.MAPIFolder 
Public objItem  As Outlook.MAPIFolder 
Public oSubFldrList As Outlook.MAPIFolder 
Public oSubFldritem As Outlook.MAPIFolder 


Sub GetAttachments(Name As String) 
     On Error GoTo GetAttachments_err 
     Dim MyMail As MailItem 
     Dim ns As Namespace 
     Dim Inbox As MAPIFolder 
     Dim SubFolder As MAPIFolder 
     Dim Item As Object 
     Dim Atmt As Attachment 
     Dim FileName As String 
     Dim i As Integer 
     Dim olItem As MailItem 
     Dim olAtt As Outlook.Attachment 

    i = 0 
     If oFldrList.Folders.Count = 0 Then 
      MsgBox oFldrList.Name & " has no sub folders" 
      MsgBox "There are " & oFldrList.Items.Count & " items in folder" 
     Else 
      Set SubFolder = oFldrList.Folders(Name) 
      ' MsgBox SubFolder.Name & " has " & SubFolder.Items.Count & " items folders" 
     End If 

     For Each olItem In SubFolder.Items 
      ' MsgBox olItem.Subject & vbLf & "has " & olItem.Attachments.Count & " attachements" 
      For Each olAtt In olItem.Attachments 
Select Case Right(olAtt.FileName, 4) 
Case ".xls" 
    FileName = frmdownloadattchmts.TextBox1.Value & olAtt.FileName 
     olAtt.SaveAsFile FileName 
    i = i + 1 
Case ".csv" 
    FileName = frmdownloadattchmts.TextBox1.Value & olAtt.FileName 
     olAtt.SaveAsFile FileName 
    i = i + 1 
Case ".txt" 
    FileName = frmdownloadattchmts.TextBox1.Value & olAtt.FileName 
     olAtt.SaveAsFile FileName 
    i = i + 1 
Case ".mp3" 
     FileName = frmdownloadattchmts.TextBox1.Value & olAtt.FileName 
     olAtt.SaveAsFile FileName 
    i = i + 1 
Case ".jpg" 
     FileName = frmdownloadattchmts.TextBox1.Value & olAtt.FileName 
     olAtt.SaveAsFile FileName 
    i = i + 1 
Case Else 
    Select Case Right(olAtt.FileName, 5) 
    Case ".xlsx" 
     FileName = frmdownloadattchmts.TextBox1.Value & olAtt.FileName 
     olAtt.SaveAsFile FileName 
    i = i + 1 
Case ".alnk" 
     FileName = frmdownloadattchmts.TextBox1.Value & olAtt.FileName 
     olAtt.SaveAsFile FileName 
    i = i + 1 
    End Select 
End Select 
      Next 
     Next 

    If i > 0 Then 
      MsgBox "I found " & i & " attached files." _ 
      & vbCrLf & "I have saved them on the" & frmdownloadattchmts.TextBox1.Value & " Path." _ 
      & vbCrLf & vbCrLf & " ", vbInformation, "Download Finished!" 
      Unload Me 
     Else 
      MsgBox "I didn't find any attached files in your mail.", vbInformation, _ 
      "Finished!" 
     End If 
GetAttachments_exit: 
     Set Atmt = Nothing 
     Set Item = Nothing 
     Set ns = Nothing 
     Exit Sub 
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

뭔가 작동한다, 그래서 당신은 또한 Unread 값을 읽하기 전에 그것이 개체의 유형을 확인해야 할 수 있습니다

Dim fn 

For Each olItem In SubFolder.Items 
    ' MsgBox olItem.Subject & vbLf & "has " & olItem.Attachments.Count & " attachements" 
    If olItem.Unread Then 
     For Each olAtt In olItem.Attachments 

      fn = olAtt.Filename 

      If fn Like "*.xls" Or fn Like "*.csv" Or fn Like "*.txt" Or _ 
       fn Like "*.mp3" Or fn Like "*.jpg" Or fn Like "*.xlsx" Or _ 
       fn Like "*.alnk" Then 

        Filename = frmdownloadattchmts.TextBox1.Value & olAtt.Filename 
        olAtt.SaveAsFile Filename 
        i = i + 1 

      End If 

     Next 'attachment 
    End If 'unread 
Next 
관련 문제