遍历以复制多个Outlook附件类型不匹配错误 [英] Loop through to copy multiple outlook attachments type mismatch error
问题描述
我试图遍历消息以复制多个附件,但没有成功,我得到了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:\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
推荐答案
您正在对不需要的可迭代集合(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屋!