2013-08-26 28 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,这是一种收集类型,但不是唯一的收集类型。