Expand VBA Permanent Delete Code

Post number 2 has been selected as the best answer.

Status
Not open for further replies.

RayB

New 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!
 
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
 
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
 
Lewis, it's not EXCEL but OUTLOOK
 
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!
 
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!
 
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
efire9207 VBA Outlook Contacts Outlook VBA and Custom Forms 6
B Requesting VBA code to make Outlook prompt for confirmation when deleting a task? Outlook VBA and Custom Forms 4
M Outlook 365 VBA Auto-Forward Only the first of Duplicate Emails Outlook VBA and Custom Forms 2
N VBA Code Not Working correctly Outlook VBA and Custom Forms 1
L VBA to Triage Incoming Email Outlook VBA and Custom Forms 0
J Outlook VBA to send from Non-default Account & Data Files Outlook VBA and Custom Forms 4
H using VBA to edit subject line Outlook VBA and Custom Forms 0
G Get current open draft message body from VBA Outlook VBA and Custom Forms 1
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
M Outlook 2016 outlook vba to look into shared mailbox Outlook VBA and Custom Forms 0
V VBA Categories unrelated to visible calendar and Visual appointment Categories Outlook VBA and Custom Forms 2
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 2
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
R Outlook 2019 VBA to List Meetings in Rooms Outlook VBA and Custom Forms 0
geoffnoakes Counting and/or listing fired reminders via VBA Using Outlook 1
O VBA - Regex - remove double line spacing Outlook VBA and Custom Forms 1
D.Moore Strange VBA error Outlook VBA and Custom Forms 4
B Modify VBA to create a RULE to block multiple messages Outlook VBA and Custom Forms 0
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
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 1
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 1
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

Similar threads

Back
Top