Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

vba - Loop through to copy multiple outlook attachments type mismatch error

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

Any suggestions will be appreciated.

my code is as follows,

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You seem to be confusing two different kinds of loops. For Each...Next and For...Next statements have different structures. You may want to read the documentation and examples in the above linked reference.

In a For Each loop you don't have a counter variable tracking the index. It automatically retrieves each item from the collection for you.

In a For loop you have a counter variable that increases for you, you don't have to increment it.
(e.g. i = i + 1)
However if you are accessing a collection you must retrieve the current item yourself.
(e.g. myAttachments.Item(i), or you can use the shorter equivalent syntax of myAttachments(i) as that the .Item is implied in VBA)

Here is a working example that prints the file names for the currently active message's attachments to the Immediate window using each type of for loop.

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

As you can see, the For Each loop is far simpler. However the For loop can give you a bit more control and information when accessing indexed collections. Usually you will want to use the For Each loop.

Also note, there is some confusion in the terminology used for collections in VBA. There are multiple different kinds of collections. There are also Collection objects, which are a type of collection but not the only type of collection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...