For Each loop: Just deletes the very first attachment

前端 未结 2 1365
独厮守ぢ
独厮守ぢ 2021-01-28 06:47

I\'ve been trying to delete the attachments in Outlook after copying them using for each loop. It just deletes the very first attachment after copying it but does not go for the

2条回答
  •  暖寄归人
    2021-01-28 07:39

    Try this. I added code/comments to iterate through and remove all the attachments after you do your saving. The reasons you should do this are explained very well here by David Zemens.

    You also should get in the habit of saving messages you modify in Outlook VBA as sometimes this is important, sometimes it's not, but it can confuse the heck out of you if you don't use Save when you need to.

     'location to save in.  Can be root drive or mapped network drive.
        Const attPath As String = "C:\Users\pkshahbazi\Documents\EmailAttachments\"
        Set myAttachments = Msg.Attachments
            For Each olAttch In myAttachments
                Att = olAttch.DisplayName
                If Right(olAttch.FileName, 3) = "zip" Then
                olAttch.SaveAsFile attPath & Att
                'olAttch.Delete
                End If
            Next olAttch
            'iterate through all attachments, going backwards
            dim j as integer
            For j = Msg.Attachments.Count To 1 Step -1
                Msg.Attachments.Remove (j)
            Next j
    
            'make sure to save your message after this
            Msg.save
        Msg.UnRead = False
    
    
    
    
    End If
    

提交回复
热议问题