rule to move sent mail to the deleted folder in outlook

Status
Not open for further replies.

NBSTL68

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hi,

I have processes that automate outgoing emails. These can clog my sent mail folder and at some point reach a limit.

I need a good process that would instruct outlook after each mail item is sent, (with a specific character value in the subject or specific sender as I use the 'send as' feature), to then move the sent mail out of the SENT folder and into the Deleted folder.

I can find no way to set this up with the standard rules wizard so a friend helped me write some VBA.

Here is what I am doing now, however this apparently just stops working daily....

Basically if I reboot the computer or shut down and restart Outlook this will for for the day or the afternoon, but then stops later...

Any advice on a more reliable method or a way to auto trigger the Application_Startup throughout the day or something would be appreciated.

'-------Option Explicit

Dim WithEvents objInbox As Outlook.Items

Dim AppNS As NameSpace

Private WithEvents olSentItems As Items

Private WithEvents olSent As Folders

'---Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set olSentItems = objNS.GetDefaultFolder(olFolderSentMail).Items
'Set olDeletedItems = objNS.GetDefaultFolder(olFolderDeletedItems).Items
Set objNS = Nothing

End Sub

'-----Private Sub olSentItems_ItemAdd(ByVal Item As Object)
Dim objPF As MAPIFolder
Dim objIC As MAPIFolder
Dim objSent As MAPIFolder
Dim objDestination As MAPIFolder

Set AppNS = Application.GetNamespace("MAPI")
If Item.Class = olMail And InStr(1, Trim(Item.Subject), "***", vbTextCompare) > 0 _
Or InStr(1, Trim(Item.Subject), "TBD", vbTextCompare) > 0 _
Then
Item.Delete
'ElseIf Item.Class = olMail And InStr(1, Trim(Item.Subject), "TBD", vbTextCompare) > 0 _
'Then
'Item.Delete

End If

End Sub

'-----------Regards,

Nick
 

Forum Admin

Senior Member
I'm not sure it's more reliable, but you can use two 'after sending' rules - first checks the subject and moves, second one saves all sent items to the sent folder.

I'm not sure this is a factor, but you are dimming a bunch of junk and not using it - you are only setting one and not using it, so it shouldn't be typing up the objects in memory... but its better to remove them.

Dim objPF As MAPIFolder

Dim objIC As MAPIFolder

Dim objSent As MAPIFolder

Dim objDestination As MAPIFolder

Set AppNS = Application.GetNamespace("MAPI")

You also don't need to set the inbox in the app startup, unless it's used in another macro.

Assuming no other macros, this should be all you need.

Code:
Option Explicit 
 
Private WithEvents olSentItems As Items 
 
'---Private Sub Application_Startup() 
 
' may not be any more efficient than declaring the namspace 
 
Set olSentItems = Session.GetDefaultFolder(olFolderSentMail).Items 
 
End Sub 
 
'-----Private Sub olSentItems_ItemAdd(ByVal Item As Object) 
 
If Item.Class = olMail And InStr(1, Trim(Item.Subject), "***", vbTextCompare) > 0 _ 
 
Or InStr(1, Trim(Item.Subject), "TBD", vbTextCompare) > 0 _ 
 
Then 
 
Item.Delete 
 
'ElseIf Item.Class = olMail And InStr(1, Trim(Item.Subject), "TBD", vbTextCompare) > 0 _ 
 
'Then 
 
'Item.Delete 
 
End If 
 
End Sub
 

Forum Admin

Senior Member
Oh, and you need to disable the option to save sent items automatically if you use those two after sending rules, otherwise you'll get duplicates.
 

NBSTL68

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Oh, and you need to disable the option to save sent items automatically if you use those two after sending rules, otherwise you'll get duplicates.

Thanks

I do not want to turn off the save to sent automatically b\c I DO want to save to sent all my other daily mail...just not the automated emails I send out, (can be a hundred or more in a day and don't need to ever reference them).

I did not see any "after sending" rules that would apply. There is one rule that allows you to make a copy to another folder, but I want to move, not copy, from sent to delete.

Thanks for the code cleanup! Yes some of it was old stuff I inhereted and some applies to another vba script.

Cleaner is better though.
 

Forum Admin

Senior Member
The second rule saves everything else in the Sent folder, so you won't lose any mail. However, I've seen it act goofy with IMAP account if the network connection is bad and the sent items don't save, so its not 100% foolproof. That behavior may only apply to IMAP accounts - but its still something that you need to consider.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
O Rule to move (specific) messages from Sent folder to Specific folder Using Outlook 1
S Send email via SMTP - use transport rules to add to senders inbox (then rule to move to sent items Exchange Server Administration 1
Nadine Rule to move attachments with specific name Outlook VBA and Custom Forms 1
K VBA BeforeItemMove event create rule to always move to its folder. Outlook VBA and Custom Forms 4
L Making rule to move email to folder from one O365 domain Using Outlook 1
S How can I create a rule to move inbound emails from any of my contacts out of Inbox? Using Outlook 1
H Rule to move emails based on subject and content Outlook VBA and Custom Forms 1
B Outlook 2000 Rule to Move txt msg Not Working if size >24KB Using Outlook 3
S Creating custom rule to move if "NAME" is in body but ignore the phrase "Hi NAME" in the search Using Outlook 2
S Rule to move messages except for replies Using Outlook 5
H move to folder rule corrupts message Using Outlook 3
A rule to move emails to a folder then want to delete them from that folder 2010 Using Outlook 5
S Rule to move BCC email to a folder Using Outlook 6
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
M How create a Rule to filter sender's email with more that one @ sign Using Outlook 1
diver864 vba for a rule to automatically accept meeting requests with 'vacation' in subject, change to all-day event, change to free, don't send reply Outlook VBA and Custom Forms 1
1 Incorrectly Setup a Rule at Domain level to not allow address from outside domain Exchange Server Administration 2
J Transport Rule to detect Keyword question.. Exchange Server Administration 2
T "Words In Recipient's Address" Rule Causes Outlook To Stop Processing Rules Using Outlook 3
R Outlook Autoforward rule do not work for NDR messages Using Outlook 1
R Setup autofoward rule on a particular folder in Outlook Using Outlook 0
N Rule for "on behalf of" - with changing names Using Outlook 2
S Unable to remove rule outlook 2010 Using Outlook 0
O Outlook 2016 This rule will only run when you check your email in Outlook.... Using Outlook 4
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
S Adding new Exchange (2016) rule very slow down Microsoft Outlook Exchange Server Administration 0
B Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1
icacream Rule based on sender / wrong sender sent to folder Using Outlook 7
Bri the Tech Guy Run Script rule not running for newly arriving messages Outlook VBA and Custom Forms 25
J Assess content of User Defined Field in Rule Using Outlook 3
A Change order of actions in one (!) rule Outlook VBA and Custom Forms 2
J HELP- Rule to auto strip prepend from external emails Using Outlook 0
A Forward Outlook Email by Filtering using Macro Rule Outlook VBA and Custom Forms 44
A rule to flag messages not working Using Outlook 5
Brian Murphy Exchange Online Everything a Transport Rule should do and cannot Exchange Server Administration 1
J Outlook Rules - Changing auto-submit address in multiple rules, according to rule name Outlook VBA and Custom Forms 0
A saving attachement to folder named the same as rule name Outlook VBA and Custom Forms 0
Jennifer Murphy Can I create a Rule with Or'd conditions? Using Outlook 1
A Rules - how to determine if all conditions in rule are true or false Outlook VBA and Custom Forms 5
Vijay Error in rule- Run a script Using Outlook 1
Ascar_CT Rule behavior analysis - any tools available? Using Outlook 0
Liza Creating a rule in outlook to filter messages Using Outlook 0
Stilgar Relsik Create a rule to copy text from an email and paste it in the subject line. Using Outlook 1
M VBA Rule for removing all body but hyperlink then forwarding Outlook VBA and Custom Forms 9
B custome rule Outlook VBA and Custom Forms 2
Diane Poremsky Mark Sent Items as Read After Copying with a Rule New Slipstick.com Articles 0
Diane Poremsky Run a Script Rule: Send a New Message when a Message Arrives New Slipstick.com Articles 2
T Create Rule For Secondary E-Mail Address In Outlook 2016 Using Outlook 4
B Macro To Create Rule To Export From Certain Folder Email Information in one workbook multiple sheets Outlook VBA and Custom Forms 0

Similar threads

Top