Mcto to close all emails (Outlook) without Saving

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have the folowing macto on an excel workbook to close all opened emails without saving

It goes iinto a llook an nonev of the opened emails are closed

It would be appreciated if someone could amend my code

Code:
 Sub OpenAndCloseOutlookEmails()
   

    Dim olApp As Object
    Dim olNamespace As Object
    Dim olFolder As Object
    Dim olItem As Object
    Dim i As Integer
    
    ' Create Outlook application object
    Set olApp = CreateObject("Outlook.Application")
    
    ' Get the MAPI namespace
    Set olNamespace = olApp.GetNamespace("MAPI")
    
    ' Get the Inbox folder
    Set olFolder = olNamespace.GetDefaultFolder(6) ' 6 represents the Inbox folder
    
    ' Loop through each item in the Inbox folder in reverse order
    For i = olFolder.Items.Count To 1 Step -1
        Set olItem = olFolder.Items(i)
        If olItem.Class = 43 Then ' 43 represents the olMail class (MailItem)
            ' Open the email
            olItem.Display
            
            ' Close the email without saving
            olApp.ActiveInspector.Close olDiscard
        End If
    Next i
    
    ' Clean up
    Set olItem = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I amended the code as folllows and it works

Code:
 Sub CloseOutlookWithoutSaving()
    Dim olApp As Object ' Outlook.Application

    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    On Error GoTo 0

    If Not olApp Is Nothing Then
        ' Close Outlook without saving files
        olApp.Quit
        Set olApp = Nothing
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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