Outlook 2013 Delete mail macro

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.
 
Top