循环复制多个 Outlook 附件类型不匹配错误 [英] Loop through to copy multiple outlook attachments type mismatch error

查看:26
本文介绍了循环复制多个 Outlook 附件类型不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试遍历消息以复制多个附件,但没有成功,我收到 13 - 类型不匹配错误!!!

I trying to loop through the message to copy multiple attachments, but no success, i'm getting 13 - type mismatch error!!!

任何建议将不胜感激.

我的代码如下,

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:UserspkshahbaziDocumentsEmailAttachments"
        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

推荐答案

您正在对可迭代集合 (myAttachments) 使用索引循环,这是不必要的.它不是错误的来源,但我看到的错误与您的迭代有关:

You're using an indexed loop over an iterable collection (myAttachments) which is unnecessary. It is not the source of the error, but the error I see is related to your iteration:

您正在执行不匹配错误,因为:

You're getting a mismatch error because you're doing:

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

您已经将 myAttachments 指定为 Msg.Attachments,它的类型为 Outlook.Attachments.这是一个可迭代的集合,但您需要使用 Outlook.Attachment 类型变量进行迭代.

You have already assigned myAttachments as Msg.Attachments and it is of type Outlook.Attachments. This is an iterable collection, but you need to iterate using an Outlook.Attachment type variable.

Dim olAttch as Outlook.Attachment

然后,像(未经测试)一样修改你的循环:

Then, modify your loop like (untested):

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

这篇关于循环复制多个 Outlook 附件类型不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆