PDA

View Full Version : [SOLVED:] Set expiry date for all sent and deleted mail items bigger than 1MB with attachment



MadeofStone
03-06-2015, 09:46 AM
Hi,

I have simple script set up to run as part of a rule against all incoming messages. The script basically checks if the mail has an attachment and if the mail is bigger than 1MB. If these conditions are true then an expiry date is set equal to the date the mail item was created + 30 days. Code below:-


Sub SetExpire(item As Outlook.MailItem)

If item.Attachments.Count > 0 And item.Size > 1048576 Then 'checks if e-mail has an attachment and checks if e-mail size if greater that 1MB (expressed in Bytes)


item.ExpiryTime = item.CreationTime + 30 'sets expiry date as 30 days from date the mail item was created
item.Save 'saves the e-mail with expiry date set
End If

End Sub

How can I tweak this code so Outlook recognises the macro in the Developer tab allowing me to assign it to a custom button in a new tab on the ribbon? I would then like the macro to run for all mail items in the Sent Items and Deleted items folders

Help would be greatly appreciated.

Cheers

gmayor
03-13-2015, 12:55 AM
You would need to setup a macro to call your sub for the two folders e.g.


Sub ProcessMessages()
Dim olItems As Items
Dim olMsg As MailItem
Dim i As Long
On Error Resume Next
Set olItems = Session.GetDefaultFolder(olFolderSentMail).Items
For i = 1 To olItems.Count
Set olMsg = olItems(i)
SetExpire olMsg
Next i
Set olItems = Session.GetDefaultFolder(olFolderDeletedItems).Items
For i = 1 To olItems.Count
Set olMsg = olItems(i)
SetExpire olMsg
Next i
lbl_Exit:
Set olMsg = Nothing
Set olItems = Nothing
Exit Sub
End Sub

MadeofStone
03-13-2015, 04:15 AM
Thanks very much! Works perfectly. Much appreciated.