Excel email vba question

Cooperaac

New Member
Joined
Dec 28, 2018
Messages
5
Hi,

I am trying to find some code that will loop through any open draft emails I have and use the .close function on any without an attachment. Any ideas?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi and welcome to MrExcel board!
Play with the below code:
Rich (BB code):
Sub CloseWithoutAttachments()
  Const olMail = 43, olDiscard = 1, olPromptForSave = 2
  Dim i As Long
  Dim Inspector As Object
  On Error Resume Next
  With GetObject(, "Outlook.Application")
    If Err Then
      MsgBox "Outlook not open", vbExclamation, "Exit"
      Exit Sub
    End If
    For Each Inspector In .Inspectors
      With Inspector.CurrentItem
        If .Class = olMail Then
          If .Attachments.Count = 0 Then
            i = i + 1
            Inspector.Close olDiscard ' or use olPromptForSave instead of olDiscard
          End If
        End If
      End With
    Next
  End With
  MsgBox "Closed emails without attachments: " & i, vbInformation
End Sub
Regards
 
Upvote 0
Hi and welcome to MrExcel board!
Play with the below code:
Rich (BB code):
Sub CloseWithoutAttachments()
  Const olMail = 43, olDiscard = 1, olPromptForSave = 2
  Dim i As Long
  Dim Inspector As Object
  On Error Resume Next
  With GetObject(, "Outlook.Application")
    If Err Then
      MsgBox "Outlook not open", vbExclamation, "Exit"
      Exit Sub
    End If
    For Each Inspector In .Inspectors
      With Inspector.CurrentItem
        If .Class = olMail Then
          If .Attachments.Count = 0 Then
            i = i + 1
            Inspector.Close olDiscard ' or use olPromptForSave instead of olDiscard
          End If
        End If
      End With
    Next
  End With
  MsgBox "Closed emails without attachments: " & i, vbInformation
End Sub
Regards

Hi, just a quick one is this based on using the code from excel or imbedded in outlook?
 
Upvote 0
Hi,

I have this running and have adjusted the code as below but it only ever clears half of the emails, if i have 4 open it will only close 2 windows and leave the other two open? is there a different way to make this loop?

Sub EmailClose_Loop()
Const olMail = 43, olDiscard = 1, olPromptForSave = 2
Dim i As Long
Dim Inspector As Object
On Error Resume Next
With Outlook.Application
If Err Then
MsgBox "Outlook not open", vbExclamation, "Exit"
Exit Sub
End If
For Each Inspector In .Inspectors
With Inspector.CurrentItem
If .Class = olMail Then
If .Attachments.Count = 0 Then
i = i + 1
Inspector.Close olDiscard '
End If
End If
End With
Next
End With
MsgBox "Closed emails without attachments: " & i, vbInformation
End Sub

Thanks
Adam
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top