2013-08-26 29 views
1

我試圖通過消息來循環複製多個附件,但沒有成功,我得到13 - 類型不匹配錯誤!循環複製多個outlook附件類型不匹配錯誤

任何建議,將不勝感激。

我的代碼如下,

Private Sub Items_ItemAdd(ByVal item As Object) 

On Error GoTo ErrorHandler 

    'Only act if it's a MailItem 
    Dim Msg As Outlook.MailItem 
    If TypeName(item) = "MailItem" Then 
     Set Msg = item 


     'Set folder to save in. 
     Dim olDestFldr As Outlook.MAPIFolder 
     Dim myAttachments As Outlook.Attachments 
     Dim Att As String 
     Dim i As Integer 
     'location to save in. Can be root drive or mapped network drive. 
     Const attPath As String = "C:\Users\pkshahbazi\Documents\EmailAttachments\" 
     i = 0 
     'save attachment 
     Set myAttachments = item.Attachments 
     If Msg.Attachments.Count <> 0 Then 
      For Each myAttachments In Msg.Attachments 
       Att = myAttachments.item(i).DisplayName 
       myAttachments.item(i).SaveAsFile attPath & Att 
       'mark as read 
       i = i + 1 
      Next myAttachments 
      Msg.UnRead = False 
     End If 
    End If 

ProgramExit: 
    Exit Sub 

ErrorHandler: 
    MsgBox Err.Number & " - " & Err.Description 
    Resume ProgramExit 
End Sub 
+1

哪一行會引發錯誤? –

回答

1

您使用的索引遍歷可迭代的集合(myAttachments),這是不必要的。這不是錯誤的來源,但我看到的錯誤與您的迭代:

你得到的不匹配錯誤,因爲你正在做的:

For each myAttachments in Msg.Attachments 
    '... 
Next 

您已經分配myAttachmentsMsg.Attachments,它是Outlook.Attachments。這是一個可迭代的集合,但您需要使用Outlook.Attachment類型變量進行迭代。

Dim olAttch as Outlook.Attachment 

然後,修改你的循環一樣(未經測試):

Set myAttachments = Msg.Attachments 
     For Each olAttch In myAttachments 
      Att = olAttch.DisplayName 
      olAttch.SaveAsFile attPath & Att 
     Next olAttch 
     Msg.UnRead = False 
+0

這是有幫助的!我現在可以複製多個附件。如果我想在保存附件後刪除附件,我會在下一個OlAttch語句之前放置olAttch.delete行,但在處理僅一個附件後不會得到期望的結果。 – user1765608

+0

你是什麼意思「it break」?是否有錯誤訊息?如果是這樣,有什麼錯誤?如果沒有,請描述它在做什麼以及如何不令人滿意。 –

+0

沒有任何錯誤消息,它只是刪除複製後的第一個附件,但不去第二個附件的工作!它只是去年底子 下面是摘錄 設置myAttachments = Msg.Attachments 對於每個olAttch在myAttachments ATT = olAttch.DisplayName 如果右鍵(olAttch.FileName,3)= 「拉鍊」 然後 olAttch。 SaveAsFile attPath與ATT olAttch.Delete 結束如果 接下來olAttch Msg.UnRead =假 我想通了什麼,它olAttch.delete說法混淆了每個循環。任何想法!!! – user1765608

2

你似乎混淆了兩個不同類型的循環。 For Each...NextFor...Next語句具有不同的結構。您可能需要閱讀上述鏈接參考中的文檔和示例。

在For Each循環中,您沒有跟蹤索引的計數器變量。它會自動從您的集合中檢索每個項目。

在For循環中,您有一個計數器變量可以爲您增加,您不必增加它。
(例如i = i + 1
但是,如果您要訪問集合,則必須自己檢索當前項目。
(如myAttachments.Item(i),或者您可以使用myAttachments(i)較短的等效語法的.Item在VBA是暗示)

這裏是一個工作的例子,使用打印當前激活的郵件的附件的文件名立即窗口每種類型的for循環。

Public Sub TestAttachments() 
    Dim message As MailItem 
    Dim myAttachment As Attachment 
    Dim i As Long 

    Set message = ThisOutlookSession.ActiveInspector.CurrentItem 

    Debug.Print "For Each...Next" 
    For Each myAttachment In message.Attachments 
     Debug.Print "Filename: " & myAttachment.FileName 
    Next myAttachment 

    Debug.Print "For...Next Statement" 
    For i = 1 To message.Attachments.Count 
     Set myAttachment = message.Attachments(i) 
     Debug.Print "Index: " & i & " Filename: " & myAttachment.FileName 
    Next i 
End Sub 

正如你所看到的,For Each循環要簡單得多。但是,當訪問索引集合時,For循環可以爲您提供更多的控制和信息。通常你會想要使用For Each循環。

另請注意,VBA中用於集合的術語存在一些混淆。有多種不同的集合。還有Collection objects,這是一種收集類型,但不是唯一的收集類型。