(Solved) – Outlook VBA: save and close each open email

(solved)-–-outlook-vba:-save-and-close-each-open-email

I often have many windows open and it becomes tedious to close Outlook. I’ve written two versions of this QuickClose procedure, and them mostly work, but they tend to leave an email open.

This one throws and index array out of bounds error:

Sub QuickClose()

Dim Inspector As Outlook.Inspector
Dim InspectorCount, i As Integer
Dim myItem As Object
'On Error Resume Next
If Application.Inspectors.count > 0 Then
InspectorCount = Application.Inspectors.count

For i = 1 To InspectorCount
 Set myItem = Application.Inspectors.Item(i)

 myItem.Activate

 If TypeName(myItem) = "Message" Then

  myItem.Close olSave

 End If
 If TypeOf ActiveInspector.CurrentItem Is MailItem Then

 ActiveInspector.CurrentItem.Close olSave

 End If

Next i

    End If

End Sub

No errors from this one, but it also tends to leave an email open (and not saved; so the procedure is passing over it for some reason):

Sub QuickClose1()

Dim Inspector As Outlook.Inspector
Dim InspectorCount As Integer
Dim myItem As Object

If Application.Inspectors.count > 0 Then

 For Each Inspector In Application.Inspectors
 Inspector.Activate
 Inspector.CurrentItem.Close olSave
 Next Inspector

End If
End Sub

once I get this section working, it will go on to close Outlook.

Leave a Reply

Your email address will not be published. Required fields are marked *