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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

Paul Banbury

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
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.
 

Paul Banbury

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Paul Banbury

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
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.
 
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
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
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
C Outlook 2016 cannot delete, email showing in root Exchange Server Administration 5
Z Can't delete IMAP folder Using Outlook 2
J Delete old emails Exchange Server Administration 1
T Delete Emails from search results Outlook VBA and Custom Forms 1
I Outlook 2013 How do I delete a Calendar? Using Outlook.com accounts in Outlook 1
N How to delete default folder "Contacts" (olFolderContacts) in a .pst file Using Outlook 8
O How to delete posts Using Outlook 1
B How to 'really' delete IMAP emails? Using Outlook 6
H Search and delete part of a link inside HTML body message Outlook VBA and Custom Forms 2
B Delete/replace old files and save new attachments Using Outlook 1
D Delete selected text from incoming emails Outlook VBA and Custom Forms 25
mikolajek Random message selected after hard delete Using Outlook 4
Jeanne Goodman Delete Calendar Item Bypassing Deleted Items folder Outlook VBA and Custom Forms 2
C Delete Subject-Add Organizer to all Reservations in Default Calendar. Using Outlook 0
B Outlook 2016: Can't delete default calendar but I don't use it Using Outlook 7
O How to delete duplicate RSS messages? Using Outlook 6

Similar threads

Top