Delete mail macro

Status
Not open for further replies.

Paul Banbury

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I am looking for a macro to run a script to delete emails from an inbox for a ghost user, we have an account that is used to send a PDF form into i have created the macros with the help of others to move the attached PDF to a server folder and for the user that require the forms to get a message to say that they have received a new form, the reason we have done this is to keep the size of the mail box under control but im struggling with a macro to delete them say after 12 hours or even a day i would like to try both. I am using Outlook 2013 and this is what i have so far but it doea not seem to work, i was given the code by a collegue and advised to just adjust the days to what i need i have tried 1 and .5 for 12 hours.

Public Sub RemoveEmail5(Item As Outlook.MailItem)

Dim olSession As Outlook.Application, olNamespace As NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim i As Integer
Set olSession = New Outlook.Application
Set olNamespace = olSession.GetNamespace("MAPI")
Set olInbox = olNamespace.GetDefaultFolder(olFolderInbox)
Set Delete_Items = olInbox.Items

For i = Delete_Items.Count To 1 Step -1
If TypeName(Delete_Items.Item(i)) = "MailItem" Then
If DateDiff("d", Now, Delete_Items.Item(i).ReceivedTime) > 0.5 Then Delete_Items.Item(i).Delete
End If
Next

Set olSession = Nothing
Set olNamespace = Nothing
Set olInbox = Nothing
End Sub
 
Does the macro work on your inbox or is the mailbox open in a profile as the default mailbox?

this macro works on the default inbox in the outlook profile - if you are running it on a shared mailbox in your profile, you need to change this line to look at it.
Set olInbox = olNamespace.GetDefaultFolder(olFolderInbox)
Working with VBA and non-default Outlook Folders

But... if its an exchange mailbox, the admin could set up a policy to delete the mail in that mailbox after a period of time. it wouldn't work on hours, only full days.
 
Hi
This is and exchange mailbox and an additional mail account on my email so i have the account as an additional one so that i can choose what email to open from the popup dialogue box when i open outlook.
 
Hi
This is and exchange mailbox and an additional mail account on my email so i have the account as an additional one so that i can choose what email to open from the popup dialogue box when i open outlook.
Hi Diane
Just wanted to add if i am selecting the mail account as i go into outlook the account being warehouse.forms@*********.com for the email address as an example, and i open that account does that not make the inbox the default, as the macro is written for that account in the VBA screen.
 
if the warehouse.forms email account is added to the profile as an account (with it's own data file), you'd use the GetFolderPath function

and use this line:
Set olInbox = GetFolderPath("warehouse.forms@*********.com\Inbox")

if it's opened as a shared mailbox, either via automapping or because you added as a secondary mailbox in more settings, then you need to use the method to get the shared mailbox



You'd use this -
Dim objOwner As Outlook.Recipient
Set objOwner = olNamespace.CreateRecipient("warehouse.forms")
objOwner.Resolve
If objOwner.Resolved Then
'MsgBox objOwner.Name
Set olInbox= NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If
 
Hi Diane
Thanks for all your help, i appreciate it i am still unable to get the code to work not sure to be honest as this is my first time with this coding, as i had help with the rest but that help is no longer available to me, the mail account is the default account and has its own data file, but still no luck. I did try the other coding but i am not sure where i would need to place it and if i need to change any of the data. It is not as easy as i thought it might be being new to this.
 
post your code here and i'll take a look at it.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
S Macro to delete e-mail containing specific texts in the subject Outlook VBA and Custom Forms 3
K How can I delete an e-mail from Outlook Using Outlook 1
D after delete mail, open the next one Outlook VBA and Custom Forms 0
C How to replace or delete first instance of sentence in mail body? Outlook VBA and Custom Forms 1
undercover_smother Automatically Forward All Sent Mail and Delete After Send Outlook VBA and Custom Forms 10
M Floating Delete Icon in mail list Using Outlook 1
B Problem with new imap mail account with delete not working as I would like Using Outlook 2
V Clicking box to delete junk mail doesn't stay Using Outlook 2
R OWA can't delete mail Exchange Server Administration 8
L How can I delete stationery from Actions, New Mail Message Using in outlook 2007? Using Outlook 6
D Delete Outlook emails from MS server Using Outlook 12
D Delete selected text in outgoing email body Outlook VBA and Custom Forms 0
E Outlook 365 Save Selected Email Message as .msg File - oMail.Delete not working when SEARCH Outlook VBA and Custom Forms 0
G Automatically delete email when a condition is met Outlook VBA and Custom Forms 1
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
M Shift Delete doesn't delete email from server Using Outlook 3
J Unable to delete folders in Outlook 2019 / Windows Using Outlook 0
Horsepower Cannot delete gmail in iPhone Outlook outbox Using Outlook 1
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
N How to add or delete items to Move dropdown Menu Using Outlook 0
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 3
O How to delete a forum post? Using Outlook 4
T Outlook 2010 Errore run-time -2147417851 (80010105) Metodo delete ContactItem non riuscito Outlook VBA and Custom Forms 0
C Outlook 2016 Unable to delete Gmail IMAP calendar Using Outlook 2
S Outlook 2016 dont delete inbox item Using Outlook 0
F Wishlist Outlook suddenly began synchronizing deleted items every time I delete a single email. Using Outlook 2
O Outlook 2010 Add delete button to the side of the message list Using Outlook 1
R Outlook 365 update sets delete from server flag Using Outlook 1
S Outlook 2016 A Shortcut to recall and delete and sent message. Using Outlook 1
T Outlook 2010 How to Delete “Orphaned” TO DO? ( Not on Exchange Server) Using Outlook 6
P How did hacker cause link to delete and archive containing email? Using Outlook 6
Z Outlook 365 delete reminder you can’t make change to contents of this-read only folder Using Outlook 4
J To delete the draft email Using Outlook 2
W Recurrence: delete older occurrences / change earliest start time Outlook VBA and Custom Forms 0
J Moved many emails to Outlook external folder, need to delete on Gmail server Using Outlook 14
A Attachments.Delete Outlook VBA and Custom Forms 3
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
M Delete headers in Inline reply 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 How can I delete all copies of a given companies logo graphic from all my emails at once? Using Outlook 3
9 Outlook 2016 How to save an Outlook attachment to a specific folder then delete the email it came from? Using Outlook 1
O Delete duplicate emails - subscription notifications Using Outlook 5
F Delete/create/reset Exchange mailbox on Outlook.com Using Outlook.com accounts in Outlook 3
L Email Read and Delete Outlook VBA and Custom Forms 4
H Delete Calendar Entries in Using Outlook 2
D Delete Emails from Senders in Shared Mailbox Outlook VBA and Custom Forms 1
R How to delete Facebook Contacts folder in Outlook 365 Using Outlook 7
R VBA Code to permanently delete selected email Outlook VBA and Custom Forms 10
JoeG Appointment Delete/Change Recurrence Outlook VBA and Custom Forms 0

Similar threads

Back
Top