对于每个循环:仅删除第一个附件 [英] For Each loop: Just deletes the very first attachment

查看:68
本文介绍了对于每个循环:仅删除第一个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每次循环使用复制附件后,我一直试图在Outlook中删除附件.它只会在复制后删除第一个附件,而不会继续处理第二个附件!它只是下降到End Sub.

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 second attachment to work on! It just goes down to the End Sub.

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

    'Change variables to match need. Comment or delete any part unnecessary.
        'If (Msg.SenderName = "Name Of Person") And _
        '(Msg.Subject = "Subject to Find") And _
        '(Msg.Attachments.Count >= 1) Then

    'Set folder to save in.
    Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim olAttch As Outlook.Attachment
    Dim Att As String

    '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
    Msg.UnRead = False

End If

ProgramExit:
  Exit Sub

ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

我发现OlAttch.delete语句混淆了For Each循环.

I figured out that the OlAttch.delete statement confuses the For Each loop.

任何想法,我如何删除附件.

Any idea how I can delete the attachments.

推荐答案

尝试一下.保存后,我添加了代码/注释来遍历并删除所有附件.大卫·泽门斯(David Zemens)在此处中对此进行了很好的解释.

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.

您还应该养成在Outlook VBA中保存您修改的邮件的习惯,因为有时这很重要,有时却不重要,但是如果您在需要时不使用Save,则会使您感到困惑.

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

这篇关于对于每个循环:仅删除第一个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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