I've added some VBA code to my outlook application to run some clean-up when I close the program. Specifically, I delete any notification emails automatically generated by my test environment at work.
Then I try to empty my junk folder, mark emails in a specific folder as read, and then permanently delete all the items from my "Deleted Items" folder. Here is the code:
Private Sub Application_Quit()
On Error Resume Next
Call delete_LV_emails
Call mark_JIRA_read
Call empty_junk
Call empty_deleted
End Sub
The subs that I am calling are in a module named "Cleanup", and I know they all work when I run them on their own. However, only the "delete_LV_emails" sub gets called. That is, when I close/re-open outlook. The only thing that has occurred is that the automatically generated emails are moved to the "Deleted Items" folder. I can't figure out why only one of the subs is being called.
If it matters, the code for each of the subs is below:
Sub delete_LV_emails()
On Error Resume Next
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.Folder
Dim olItem As Object
Dim arrKeys(0 To 1) As String
Set olNS = Application.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
arrKeys(0) = "LabVIEW Error"
arrKeys(1) = "Test Complete"
iItemCount = olFolder.Items.Count
sDate = Split(Str(Now), " ")(0)
For iItemInd = iItemCount To 1 Step -1
Set olItem = olFolder.Items(iItemInd)
If Not Split(Str(olItem.CreationTime), " ")(0) = sDate Then GoTo NEXTITEM
iKeyInd = 0
While Not iKeyInd > 1
If InStr(olItem.Subject, arrKeys(iKeyInd)) Then olItem.Delete
iKeyInd = iKeyInd + 1
Wend
NEXTITEM:
Next
Set olNS = Nothing
Set olFolder = Nothing
Set olItem = Nothing
End Sub
Sub empty_deleted()
On Error Resume Next
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.Folder
Dim olItem As Object
Set olNS = Application.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderDeletedItems)
iItemCount = olFolder.Items.Count
For iItemInd = iItemCount To 1 Step -1
Set olItem = olFolder.Items(iItemInd)
olItem.Delete
Next
Set olNS = Nothing
Set olFolder = Nothing
Set olItem = Nothing
End Sub
Sub empty_junk()
On Error Resume Next
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.Folder
Dim olItem As Object
Set olNS = Application.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderJunk)
iItemCount = olFolder.Items.Count
For iItemInd = iItemCount To 1 Step -1
Set olItem = olFolder.Items(iItemInd)
olItem.Delete
Next
Set olNS = Nothing
Set olFolder = Nothing
Set olItem = Nothing
End Sub
Sub mark_JIRA_read()
On Error Resume Next
Dim olNS As Outlook.NameSpace
Dim olFolder As Outlook.Folder
Dim olItem As Object
Set olNS = Application.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Jira")
iItemCount = olFolder.Items.Count
For iItemInd = iItemCount To 1 Step -1
Set olItem = olFolder.Items(iItemInd)
If olItem.UnRead Then olItem.UnRead = False
Next
Set olNS = Nothing
Set olFolder = Nothing
Set olItem = Nothing
End Sub
I realize that this is an extremely long-winded question, but if anyone has any insight I would greatly appreciate it.
Remove On Error Resume Next
from your code then run it again
On Error Resume Next
you are basically instructing VBA to essentially ignore the error and resume execution on the next line of code.
It is very important to remember that On Error Resume Next
does not in any way "fix" the error. It simply instructs VBA to continue as if no error occurred.
See more info on http://www.cpearson.com/excel/ErrorHandling.htm