Link to home
Start Free TrialLog in
Avatar of mindwarpltd
mindwarpltd

asked on

Is it possible and if so how, to have a rule which deletes what in my spam folder in outlook after 1 day?

I have a spam folder which my anti virus / spam program uses to put in spam emails.

I'm just wondering if its possible to have a rule in outlook which will empty that folder after a day ?

If so, how ?
Avatar of Alan Hardisty
Alan Hardisty
Flag of United Kingdom of Great Britain and Northern Ireland image

I am not aware that you can do this with a rule.  There are no options to do this even in Outlook 2010.  You may be able to setup AutoArchive to permanently delete mail after a day and only setup that folder, but that's about it that I can think of.
Hello mindwarpltd,

If macros are acceptable then you can have a macro that deletes anything in that folder older than 1 day when the application opens or closes .. and if so we can help there.

Regards,

chris_bottomley
That's cheating ;-)
Avatar of mindwarpltd
mindwarpltd

ASKER

Can you provide the macro?

I'm not really sure what to do with, so I'll need full instructions.

I'm using outlook xp
First off lets see if we establish teh correct folder.

IN outlook place the cursor on teh junk mail folder in question
In outlook press alt + f11 to open the editor, (VBE)
In the VBE press ctrl + G to open the immediate window ... it has the heading Immediate when opened!
In teh Immediate window type:
    ?application.activeexplorer.currentfolder.folderpath

Let me know what it returns.

Chris
\\Jules\Norton AntiSpam Folder
Alternatively a snapshot of the folders in the folder structure will do.

Chris
In the VBE:

Insert | Module ... YOu will now have a module under the project root in the project window, (ctrl + R).

In this module insert the following snippet, there is one more step which i'll cover in a few minutes

Chris
Sub filterJunk()
Dim fldr As Object
Dim intItem As Integer
Dim strFilter As String
Dim olmailitems As Outlook.items
    
    On Error Resume Next
    Set fldr = Application.Session.folders("Jules").folders("Norton AntiSpam Folder")
    strFilter = "[ReceivedTime] <= '" & Format(DateAdd("d", -1, Date) + TimeSerial(0, 0, 0), "mm/dd/yyyy h:nn AMPM") & "'"
    Set olmailitems = fldr.items.Restrict(strFilter)
    
    ' process all the items in this folder
    For intItem = olmailitems.count To 1 Step -1
        objItem.Delete
    Next
'    Debug.Print fldr.items.count & vbTab & olmailitems.count

End Sub

Open in new window

I would suggest you disable Norton Spam Folder and let
Outlook handle the spam.
OK Chris Done.

Senad, I'm not using norton anymore, but kept the folder Iuse kaspersky and my isp has spam prefixes on the subject lines.
Apologies i've discovered a bug during the last rewrite, Use the snippet below in teh module instead.

When that is done in the vBE thisoutlooksession~

LH dropdown select APplication
RH dropdown select startup

Simply type the one line between the sub bounds that should have been 'created as:
Private Sub Application_Startup()
End Sub

so that it becomes

Private Sub Application_Startup()
    filterJunk
End Sub

Close the VBE and check outlook to see if there are mails to be deleted in the folder.

Now close outlook >>> SAVE THE CODE <<< and after a moment or 2 re-open.  It should delete the files from midnight the day before last, i.e if today is Wed then anything before midnight Mon/Tues.

Chris
Sub filterJunk()
Dim fldr As Object
Dim intItem As Integer
Dim strFilter As String
Dim olmailitems As Outlook.items
    
    On Error Resume Next
    Set fldr = Application.Session.folders("Jules").folders("Norton AntiSpam Folder")
    strFilter = "[ReceivedTime] <= '" & Format(DateAdd("d", -1, Date) + TimeSerial(0, 0, 0), "mm/dd/yyyy h:nn AMPM") & "'"
    Set olmailitems = fldr.items.Restrict(strFilter)
    
    ' process all the items in this folder
    If olmailitems.count = 0 Then Exit Sub
    For intItem = olmailitems.count To 1 Step -1
        olmailitems(intItem).Delete
    Next
'    Debug.Print fldr.items.count & vbTab & olmailitems.count

End Sub

Open in new window

Hmmm I don't think its doing anything.

I dragged lots of emails from the last few days into my spam folder and restarted outlook at the count of emails is the same..
I think macros must be turned off or not running as I put...

msbox "hello" in application_startup and its didn't show a message box.
Check Security as appropriate:
------------------------------

In the application select Tools | Macro | Security
2003 and Earlier : Select Medium
2007 : Warnings for all Macros
Select OK

Chris
Ahh it was set to high...

It also deleted everything in the folder :(

I'm in the UK, with the date format make a difference in your code?
First up my apologies I re-used some code so didn't test in depth but whilst I cannot yet see why I have the same experience when I test so I am looking into it.

Chris
Not surprisingly most of solutions goto the US but i'm also UK.  I have tested a mod as follows which works fine for me in the UK

Chris
Sub filterJunk()
Dim fldr As Object
Dim intItem As Integer
Dim strFilter As String
Dim olmailitems As Outlook.items
    
    On Error Resume Next
'    Set fldr = Application.Session.folders("Jules").folders("Norton AntiSpam Folder")
    Set fldr = Application.Session.GetDefaultFolder(olFolderInbox).folders("CBOTTOM1")
    strFilter = "[ReceivedTime] <= '" & Format(DateAdd("d", -1, Date) + TimeSerial(0, 0, 0), "mmm/dd/yyyy h:nn AMPM#") & "'"
    Set olmailitems = fldr.items.Restrict(strFilter)
    
    ' process all the items in this folder
    If olmailitems.count = 0 Then Exit Sub
    For intItem = olmailitems.count To 1 Step -1
        olmailitems(intItem).Delete
    Next
'    Debug.Print fldr.items.count & vbTab & olmailitems.count

End Sub

Open in new window

I set security to medium earlier and I get a message each time do I want to enable macros etc.

I don't know if this is stopping the startup macro running.
But nothing deleted at the moment.

Ahh no setting it to low doesn't make a difference.

I take it, I'd have to set the security to low to stop the message appearing.
Is it dangerous to do that?

Kind of putting me off the macro route a bit.
There are ways and means ... what version of outlook are you using?

Chris
xp
Try:

MapiLab Outlook security, (http://www.mapilab.com/outlook/security) ... The one I use routinely
Alternatively  clickyes, (http://www.contextmagic.com/)

To use mapilab, download, install then the first time you do something click do this every time and accept it then the next time the warning is no more.  SO set the macro warning back to medium and run the script ... you'll get a warning, as long as you agree and always take this action is selected then that will be the last time ... HE said!

Chris
OK, I use ClickYes at the moment.

Anyway, the macro isn't deleting.
Since it should be deleting on startup, have you restored mails to that folder i.e. are any of them older than midnight Mon/Tues?

Chris
Yes
OK is it being called at all? ... in the VBE goto the sub and modify the line:

    On Error Resume Next
to 2 lines as
    Stop
    On Error Resume Next

The macro if it runs should now stop on that line ... press F5 and it should run to completion ... if it stops I will mofify the routine to provide some data for fault finding.

Chris
Its Stops if I add that line and doesn't delete the emails.
It is possible I need to sneak into the corner and by sleight of hand change the code over while you aren't looking.

Or to explain it is possible you didn't see how I tested it because I changed a line for that purpose ... and corrected it below.  Please replace the sub with this correction.

Chris
Sub filterJunk() 
Dim fldr As Object 
Dim intItem As Integer 
Dim strFilter As String 
Dim olmailitems As Outlook.items 
     
    On Error Resume Next 
    Set fldr = Application.Session.folders("Jules").folders("Norton AntiSpam Folder") 
    strFilter = "[ReceivedTime] <= '" & Format(DateAdd("d", -1, Date) + TimeSerial(0, 0, 0), "mmm/dd/yyyy h:nn AMPM#") & "'" 
    Set olmailitems = fldr.items.Restrict(strFilter) 
     
    ' process all the items in this folder 
    If olmailitems.count = 0 Then Exit Sub 
    For intItem = olmailitems.count To 1 Step -1 
        olmailitems(intItem).Delete 
    Next 
'    Debug.Print fldr.items.count & vbTab & olmailitems.count 
 
End Sub

Open in new window

Ahhh cool that worked.

I've installed MapiLab but it didn't give me the option to do this everytime as you mentioned.

Its not a yes no box.

Its enable macros, disable macros and I think help or cancel.
I'm using 2007 so my days with such are much reduced and besides every use is already captured by MAPILAB on my work PC so I simply don't see it.

Just checked the web page and it's allow access after first selecting the always do this action, (at the bottom of the advanced security pop up).

Chris
Hmmm
?

Are you getting the mapilab prompts as sampled on their web site?

Chris
Its appears on the oulook menus but its invisible when starting outlook.

I've just uninstalled and re-installed and nothing changed.

The only thing which concerns me, is that when I first installed yesterday there was a big messagebox with do you want o see this again, I checked it to say no I didn't.

:(
If you did the always accept then all should be well ... it is 'sticky' and you can for example check teh action list to see that everything is selected as allow, (assuming you haven't deliberately disallowede anything since installing.  See the mapilab actionlist from teh start menu and make sure all entries are allowed, (assuming as I said you have not yet specifically used mapilab to disallow anything so far).

Chris
I have been reviewing somewhat and in teh case of MAPILAB it is about allowing other applications to use outlook!

When running outlook you should simply be ok so the question is does the code as supplied strip out the old mails from teh junk folder.

CHris
Theres nothing in my action list.

I take it, thats bad.

Nope ... see my previous 'apology'
I'm confused...

Are you saying its not possible to do this ?
I humbly apologise for confusing matters:

1. No I am NOT saying it's impossible quite the opposite.
2. Outlook should simply work in itself happily doing what you want, therefore the question is ... is it working today and if not can we work back from what is happening because it ought to be working as is with the code I posted in http:Q_25050821.html#a26307931

Chris
OK.

Thanks for your continued interest with such little reward.

Since uninstalling and re-installing didn't work, I'm not sure what to do next.
1. Ignore MAPILAB it was a red herring, does no harm to leave it installed as it is a good program if you want to use outlook features in other applications at some later date.

2. Look to the code itself, does it work, does it get called ... repeat the add stop command from earlier to ensure it is being called.  Let me know.

>>> Thanks for your continued interest with such little reward.
I made it difficult due to my recent lack of familiarity with outlook 2003 security therefore thank you for giving me the time to overcome that failing!

Chris
Yes the code works.
>>> Yes the code works

As in all is well or is there some issue that needs to be worked through?

Chris
Would have liked the enable macros dialog not to appear
Are you referring to the dialog on outlook start up if so try a self cert of the vba, (and I unset everything in my outlook to test this properly!):

Note: Signatures created with SelfCert program are personal not commercial.  i.e. The certificate does not verify your identity, for commercial certificates see in the first place: http://officeupdate.microsoft.com/office/redirect/fromOffice9/cert.htm       
      
To create a 2000-2003 selfcert certificate &      
      
      1. Start menu | Programs | Windows Explorer
      2. Navigate to the "Program Files\Microsoft Office\Office" folder
      3. ExecuteSelfCert.exe
      NOTE: If not found then rerun Setup for Office CD1 and click Add or Remove Features. Click the plus sign (+) next to Office Tools; click Digital Signature for VBA Projects and then click Run from My Computer. Click Update Now.

In outlook VBA:
Tools | digital signature | choose ... the name you created above and select saving VBA project.
close then reopen outlook and always trust macros from your certificate.
close and reopen ought not get the prompt

Chris
Lets just clarify this screen, here...
enablemacros.jpg
This is the startup screen and you need to enable macros
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent :)
Phew!, of course any other outlook macros in the future should be much easier to support.

Chris