Automatically selecting folders and deleting messages in Outlook VBA

Status
Not open for further replies.

steve tatum

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
Hello, I need a little Outlook VBA help please...I manually select a subfolder from a main folder that I created (not inbox, sent, delete, etc.) in my left-hand folder pane in Outlook and run this code to delete email messages contained in the folder. However, I have about 50 folders that I need to do this to. Can someone tell me how to change this code so it will automatically "scroll" through 50 subfolders and delete the emails in each of those folders one folder at a time? I would like to manually select the main folder and run code to automatically select each subfolder under it and then delete email messages in those folders.

Sub DeleteEmails()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim subfolder As Outlook.Folder
Dim subfolders As Outlook.folders
Dim folders As Outlook.folders
Dim namespace As Outlook.namespace

' Set Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set namespace = objOL.GetNamespace("MAPI")
Set Folder = Application.ActiveExplorer.CurrentFolder

On Error Resume Next

If Not Folder.Item(i) Is Nothing Then
For Each objMsg In Folder.Items
objMsg.Deleted
Next objMsg
End If


End Sub
 

steve tatum

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
Thx Michael. I changed my code a little and this worked. But for some reason using the "For Each objMsg In subfolder.Items" function did not delete all the email message in a particular folder. There appeared to be a network latency issue. Once I added an external For loop counter and set it to keep trying for 10 iterations, all emails in each folder were deleted as expected...weird. One issue with this is that it makes the code take a lot longer to run. Any advice on this?

One other question. How do I tell VBA to choose a particular folder that is not the inbox? The way the code works now, I have to manually select the main folder and then run the code and the code uses "Set Folder = Application.ActiveExplorer.CurrentFolder". The code then selects each subfolder and deletes the emails from each subfolder. I would like for the code to automatically select the main folder.

Sub DeleteEmails()
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim subfolder As Outlook.Folder
Dim subfolders As Outlook.folders
Dim folders As Outlook.folders
Dim namespace As Outlook.namespace

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")
' Get the collection of selected objects.
Set namespace = objOL.GetNamespace("MAPI")
Set Folder = Application.ActiveExplorer.CurrentFolder
Set subfolders = Folder.folders

For Each subfolder In subfolders
On Error Resume Next
For i = 1 To 10 'have to use this bc of network latency issues with server. code may not delete all mail items if this is not used.
If Not subfolder.Item(i) Is Nothing Then
For Each objMsg In subfolder.Items
objMsg.Delete
Next objMsg
End If
Next i
Next

End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
In order to delete items from a a collection you need to loop backwards through it. For Each doesn't work in that case. Here's a sample:
Empty a Folder - VBOffice

You can reference any folder this way:
set folder=application.session.folders("name")
set folder=folder.folders("name")
etc.
 

steve tatum

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
Thx Michael, however when I try to execute the "set folder=application.session.folders("name")" line of code I get the following error mess. Any Idea why this would be happening?

upload_2016-10-13_8-20-59.png
 

steve tatum

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
LOL...I did replace "name" with my folder name. Actually several different ones. I still got the object not found error message. Could it have something to do with my email being through an MS exchange sever?
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
No, it's just a matter of using the correct name. The first line points to a top folder like Peronal Folders, the second one then points to a subfolder of it. Select the top folder, then type this into the immediate window in the VBA environment, and press enter:
?application.activeexplorer.currentfolder.name

It prints the name that you need to use.
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
F Automatically close email after selecting mark unread Using Outlook 1
J Automatically forward email and apply template Outlook VBA and Custom Forms 0
Y Outlook 2013 Stop Outlook from automatically assigning categories to Tasks Using Outlook 0
O Forward a email with modified body Automatically. Outlook VBA and Custom Forms 0
A How to open a specific link automatically with outlook 2016 Outlook VBA and Custom Forms 5
P Automatically Categorize Meetings once they are accepted Outlook VBA and Custom Forms 5
W Automatically open attachments without automatically printing them Using Outlook 0
N How to set automatically the default or user defined Quickstyle Templates by Answer in Outlook Using Outlook 1
O Run macro automatically at sending an email Using Outlook 11
D Outlook 2016 automatically increment anniversaries Using Outlook 1
T Office 2013 no longer updating automatically Using Outlook 2
B Automatically Forward Emails and Remove/Replace All or Part of Body Outlook VBA and Custom Forms 8
D Print attachments automatically and moves the mail to a new folder Outlook VBA and Custom Forms 9
A How to open a specific link automatically with outlook Outlook VBA and Custom Forms 13
L Automatically Insert Recipient Name from To Field Outlook VBA and Custom Forms 33
N how to sync automatically when outlook opens Using Outlook 10
A Sort emails into subfolders based on sender and deleting emails automatically Outlook VBA and Custom Forms 3
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
stephen li VBA Outlook send mail automatically by specified outlook mail box Outlook VBA and Custom Forms 1
R Make Enter Network Password Prompt Go Away Automatically Using Outlook 0
I Print Automatically Attachments Outlook VBA and Custom Forms 3
M Outlook 2016 Rules Not Working Automatically Using Outlook 5
Diane Poremsky Automatically create a task when sending a message New Slipstick.com Articles 0
D Is it possible to automatically send an email when it is moved to a folder? Exchange Server Administration 1
A Automatically send email based on drop-down field? Outlook VBA and Custom Forms 2
M Automatically create event in calendar when task is created Outlook VBA and Custom Forms 1
Diane Poremsky Create Appointment From Email Automatically New Slipstick.com Articles 0
Cameron Piper Automatically update custom forms across multiple computers Outlook VBA and Custom Forms 1
T Automatically open link in email received Outlook VBA and Custom Forms 33
N Automatically file email messages Using Outlook 2
J Automatically Move Old Items from a Shared Mailbox to a .PST on a Network Drive Outlook VBA and Custom Forms 1
M Change default "automatically test account settings when Next is clicked" Using Outlook 1
D Automatically file message and reply to folder. Using Outlook 1
A Code to automatically delete message after sending Outlook VBA and Custom Forms 6
Diane Poremsky Automatically Create Contacts From Messages New Slipstick.com Articles 1
T Outlook 2010 emails do not refresh automatically Exchange Server Administration 1
L Save Mail Message to new folder created automatically Outlook VBA and Custom Forms 1
G Hae Rules NOT perform automatically? Using Outlook 1
nathandavies How to save emails to a specific folder on a network automatically using a macro Outlook VBA and Custom Forms 14
T Default inbox not automatically sending/receiving Using Outlook 1
Angger Microsoft Outlook 2010 Automatically Restarting Using Outlook 2
N Outlook Shared appointments automatically to Private Calendar Using Outlook 1
M Automatically move Outlook message to the specific folder when click on reply to all Using Outlook 4
I Automatically updates outlook 2011 address book Using Outlook 1
D Automatically save attachment to network folder Exchange Server Administration 1
E Automatically pick up Recipient's Name on the Body of the Email Message Using Outlook 1
Alex Hall When updating shared calendar, it does not automatically update personal calendar Using Outlook 3
R Email Send & Receive Turned off but sends automatically anyway Using Outlook 1
M How to automatically refresh an appointment windows after changing the MessageClass property ? Outlook VBA and Custom Forms 4
Similar threads


















































Top