Expand VBA Permanent Delete Code

Post number 2 has been selected as the best answer.

Status
Not open for further replies.

RayB

Member
Outlook version
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
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
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:
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
O Cannot expand the folder. The set of folders cannot be opened. You do not have permission to log on. Using Outlook 1
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
N auto expand all folders and subfolders in the inbox Using Outlook 3
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
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
BartH VBA no longer working in Outlook Outlook VBA and Custom Forms 1
W Can vba(for outlook) do these 2 things or not? Outlook VBA and Custom Forms 2
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 0
P MailItem.To Property with VBA not work Outlook VBA and Custom Forms 2
P Tweak vba so it can target another mailbox Outlook VBA and Custom Forms 1
A Outlook 2010 VBA fails to launch Outlook VBA and Custom Forms 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
C Outlook (desktop app for Microsoft365) restarts every time I save my VBA? Using Outlook 0
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
E Outlook 365 Outlook/VBA Outlook VBA and Custom Forms 11
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
Z VBA Forward vs manual forward Outlook VBA and Custom Forms 2
J VBA Cannot programmatically input or change Value for User Defined field Using Outlook 1
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
A Any way to force sort by/group by on search results with VBA? Outlook VBA and Custom Forms 1
E Default shape via VBA Outlook VBA and Custom Forms 4
A Change settings Send/receive VBA Outlook VBA and Custom Forms 0
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
E Outlook VBA change GetDefaultFolder dynamically Outlook VBA and Custom Forms 6
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 11
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
FryW Need help modifying a VBA script for in coming emails to auto set custom reminder time Outlook VBA and Custom Forms 0
S vba outlook search string with special characters Outlook VBA and Custom Forms 1
S VBA search string with special characters Outlook VBA and Custom Forms 1
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
K Outlook Office 365 VBA download attachment Outlook VBA and Custom Forms 2
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3

Similar threads

Top