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
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