VBA - Outlook does not delete attachments

I am writing a macro that should remove attachments. From my debugging sessions, it seems like it should work. The breakpoint hits and recognizes the message object:

enter image description here

I know this sounds a little silly, but, oddly enough, it seems to work if I set a breakpoint and open an expression / watch, but not otherwise.

I struggled with this for quite some time; I would be grateful for any recommendations.

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

 Dim header As String
 Dim objNewMail As Outlook.MailItem
Dim Item As Object
Dim count As Integer


    Dim objInbox As Outlook.Folder
    Set objInbox = Outlook.Session.GetDefaultFolder(olFolderInbox)

    Dim entryIDs
    entryIDs = Split(EntryIDCollection, ",")
    Dim i As Integer

    For i = 0 To UBound(entryIDs)
        Set objNewMail = Application.Session.GetItemFromID(entryIDs(i))
                If objNewMail.Attachments.count > 0 Then
                    header = GetHeader(objNewMail)
                If DoesIPMatch(header) <> True Then
                   DeleteMessage (objNewMail)
                ElseIf IsAttachmentPDF(objNewMail) <> True Then
                     For count = 1 To objNewMail.Attachments.count
                       objNewMail.Attachments.Remove (count)
                    Next
                End If
            End If
    Next

End Sub
+4
source share
1 answer

, , , , , :

objNewMail (, )

For count = objNewMail.Attachments.count to 1 Step - 1
    objNewMail.Attachments.Remove count
Next
objNewMail.Save '## Not sure if this is necessary

:

With objNewMail.Attachments
    While .Count > 0
        .Remove 1
    Wend
End With
objNewMail.Save
+3

Source: https://habr.com/ru/post/1666025/


All Articles