Expand VBA Permanent Delete Code

Post number 2 has been selected as the best answer.

RayB

Member
Outlook version
Outlook 2007
Email Account
POP3
Hello,

I am using Outlook 2007 and really understand Excel VB, however, my Outlook VB capability is as a novice.

I have a VBA macro that moves email messages to a folder called rb Auto Deletes positioned as shown below:
Capture.JPG


After hours of searching and trying different approaches I have written this code which will permanently delete messages without going through the Outbox:

Sub AutoPermDelete()

' Usage: program mimics the entry of Shift + Delete to permanently delete an email
' How to use: select the mail item, execute this macro

Dim myOlApp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim Sel As Selection

Set myOlApp = Outlook.Application
Set myNameSpace = myOlApp.GetNamespace("MAPI")

Set Sel = Application.ActiveExplorer.Selection

SendKeys ("+{DELETE}")

SendKeys ("{ENTER}")

End Sub

The code works perfectly but I have to select each email. So what I would like to have help with expanding the code to do the following:

1. 'Look' at the rb Auto Deletes folder.
2. Determine which messages are older than 1 calendar year from the current date and permanently delete them using my code.

I am totally lost as to how to make this happen and all my attempts have not worked so I am asking for help.

Merry Christmas and Thanks in Advance for your help!
 

oliv-

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Hi,
try this
Code:
Function Nettoyage_Inbox()
'---------------------------------------------------------------------------------------
' Procedure : Nettoyage_Inbox
' Author    : OCTU
' Date      : 11/01/2015
' Purpose   : permanently delete emails
'---------------------------------------------------------------------------------------
'
    Dim Dossier As Folder
    Dim NS As NameSpace
    Set NS = application.session
    Set Dossier = NS.GetDefaultFolder(olFolderInbox).Parent.Folders("rb Auto deletes")
dim myItems 
 madate = DateAdd("yyyy", -1, Date)
    Set myItems = Dossier.items.Restrict("[LastModificationTime] < '" & madate & "'")
    While myItems.count > 0
       myItems.Remove 1
        'Dossier.Items(1).Delete 'garde historique Exchange
    Wend

End Function
 

Lewis-H

Senior Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
To delete a row using VBA, use a statement with the following structure:
1Worksheets.Rows(Row#).Delete
Worksheets.Rows(Row#).Delete

  1. Item: Worksheets.
    • VBA Construct: Workbook.Worksheets property.

    • Description: Returns a Worksheet object representing the worksheet you work with.
  2. Item: Rows(Row#).
    • VBA Construct: Worksheet.Rows property.

    • Description: Returns a Range object representing row number Row# of the worksheet returned by item #1 above.

      If you explicitly declare a variable to represent Row#, use the Long data type.
  3. Item: Delete.
    • VBA Construct: Range.Delete method.

    • Description: Deletes the Range object returned by item #2 above.
Macro Example
The following macro deletes row 6 of the worksheet named “Delete row”.
view source

print?
1Sub deleteRow()
2 Worksheets("Delete row").Rows(6).Delete
3
4End Sub
Hope this is helpful!

Regards,
Lewis
 

oliv-

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Lewis, it's not EXCEL but OUTLOOK
 

RayB

Member
Outlook version
Outlook 2007
Email Account
POP3
Hi,
try this
Code:
Function Nettoyage_Inbox()
'---------------------------------------------------------------------------------------
' Procedure : Nettoyage_Inbox
' Author    : OCTU
' Date      : 11/01/2015
' Purpose   : permanently delete emails
'---------------------------------------------------------------------------------------
'
    Dim Dossier As Folder
    Dim NS As NameSpace
    Set NS = application.session
    Set Dossier = NS.GetDefaultFolder(olFolderInbox).Parent.Folders("rb Auto deletes")
dim myItems
madate = DateAdd("yyyy", -1, Date)
    Set myItems = Dossier.items.Restrict("[LastModificationTime] < '" & madate & "'")
    While myItems.count > 0
       myItems.Remove 1
        'Dossier.Items(1).Delete 'garde historique Exchange
    Wend

End Function

Thank you so much it works perfectly!
 

RayB

Member
Outlook version
Outlook 2007
Email Account
POP3
One other question is do you know a way to add this function to run from a button on the a toolbar? From what I have read, the classical way to add a button to control a macro will not work because Functions do not show up in the Macro list.

Again, Merry Christmas and Thanks in Advance for your help!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It should work if you change it to a sub or use a simple macro to call it:

sub Deletemail()
Nettoyage_Inbox
end sub

I have a macro here that empties all mail in folder (it may not work in 2007 though)


This shows how to work with selected items:
 
Thread starter Similar threads Forum Replies Date
R The way expand/collapse folders is actually meant to work? Using Outlook 2
M Auto expand Distribution List Before Sending Email Outlook VBA and Custom Forms 1
K cannot expand the folder outlook 2007 pst Using Outlook 1
S Automatically expand a folder when new mail arrives Using Outlook 7
C Unable to expand ANY folders after adding another gmail account. 2007. Using Outlook 4
A expand/collapse group under view tab is grayed out in outlook 2007 Using Outlook 2
B OL 2010: Move to/copy to folder: how to expand list of options? Using Outlook 2
K How to expand the recipients field on an outgoing appointment? Using Outlook 1
S New-MailboxExportRequest - EXPAND 10 item import limit (PST IMPORTS) Exchange Server Administration 3
K Outlook 2010: Remembering expand/collapse views Using Outlook 2
K Automatically expand Gmail mail account Using Outlook 1
N auto expand all folders and subfolders in the inbox Using Outlook 3
M Expand query builder permanently - outlook 2007 Using Outlook 3
S cannot expand folder. in use by other application__ Using Outlook 2
S Cannot expand public folders with Administrator account in Outlook Exchange Server Administration 3
W Upgraded to Outlook 2010 - rules are working but mail folders don't expand when emails are transferred. Using Outlook 1
V Cannot expand folder, not enough memory. Using Outlook 5
R How do I expand all distribution lists in the To: section? Using Outlook 4
M How to resize the instant search pane on click on the icon to expand/ Outlook VBA and Custom Forms 1
J Expand Distribution List on Send Outlook VBA and Custom Forms 1
D.Moore VBA script fail after Office 365 update Using Outlook 8
R Limiting length of saved attachment in VBA Outlook VBA and Custom Forms 2
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
C How to use VBA to show only items x days old or more Outlook VBA and Custom Forms 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Outlook VBA error extracting property data from GetRules collection Outlook VBA and Custom Forms 10
S Reference Custom Fields with VBA Outlook VBA and Custom Forms 2
PGSystemTester VBA To Change AppointmentItem.BusyStatus From MeetingItem Before Send Using Outlook 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
G VBA Macro Calendar Printing Assistant 3
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
B VBA to Collapse Task Folder Groups Outlook VBA and Custom Forms 1
shrydvd vba to secure zip attachments Outlook VBA and Custom Forms 3
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
N VBA to delete duplicates by message-id on common pst for 2 or more emails Outlook VBA and Custom Forms 0
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
A Custom VBA to sort emails into folders Outlook VBA and Custom Forms 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
R VBA for copying sent email to current folder under a shared mailbox Outlook VBA and Custom Forms 17
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
N Outlook Email Rule execution through shortcut keys (VBA codes) Using Outlook 1
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
B Clear Offline Items (Mail Folder) via VBA Outlook VBA and Custom Forms 1
dweller Outlook 2010 Rule Ignores VBA Script Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
Similar threads


















































Top