Custom Action or Script to Purge Deleted Messages on IMAP account

F

Frank M.

I have searched throughout the forums to find out how to automatically purge
deleted messages in an IMAP account but only found a lot of talk of how
Microsoft has not made that option part of Outlook.

I have created three rules (in succession) that first copy the mail from
the IMAP INBOX to the PERSONAL FOLDERS INBOX (so my PDA can see it), then
copy the message to my IMAP DELETED FOLDER (so I have a copy to look back on
if necessary) and then to permanently DELETE it. This however does not remove
it from my IMAP server INBOX unless I click on PURGE DELETED MESSAGES.

I see there are options in MAIL RULES to either: RUN SCRIPT or PERFORM
CUSTOM ACTION. Unfortunately, when I searched HELP ONLINE there is very
little about either of these (well, there is a lot on using VB Scripting, but
that is beyond my capabilities / no good examples). If OUTLOOK had a MACRO
CREATOR (like EXCEL), I could probably do it, but I do not see that option.

Has any one written a script or custom action that could be used? In
essence, all it needs to do is select the INBOX folder of the IMAP account
and do a 'click' on the PURGE MESSAGES icon. Anyone who wrote such a script
(or Custom Action) would get a LOT of thanks from dozens of people who have
the same need as me.

Anyone know how to do it?

Thanks.

Frank in Michigan
 
M

Michael Bauer

Am Wed, 8 Feb 2006 11:30:19 -0800 schrieb Frank M.:

Frank, I never worked with IMAP but suppose this would work. For getting the
folder you could create a VBA method in ThisOutlookSession:

Public Sub Test()
With Application.Session.Pickfolder
Debug.Print "EntryID: " & .EntryID
Debug.Print "StoreID: " & .StoreID
End With
End Sub

Place the cursor into the method and press F5, then select the IMAP Inbox.
Open now the Direct Window (ctrl+g). It contains the folder´s IDs now.

The above method you can delete now and write the following code into
ThisOutlookSession instead:

Private Const IMAP_ENTRYID As String = "place the EntryID from the Direct
Window here"
Private Const IMAP_STOREID As String = "place the StoreID from the Direct
Window here"
Private Const DEL_BUTTON As Long = 0 ' Enter the Button´s ID here

Public Sub DeleteIMAP()
Dim oFld as Outlook.MapiFolder
Dim oCmd as Office.CommandBarButton

Set oFld = Application.Session _
.GetFolderFromID(IMAP_ENTRYID, IMAP_STOREID)
Set Application.ActiveExplorer.CurrentFolder = oFld

Set oCmd = Application.ActiveExplorer_
.CommandBars.FindControl(,DEL_BUTTON)
oCmd.Execute
End Sub

The only thing left for you is to get the Button´s ID (const DEL_BUTTON).
You could download OutlookSpy (www.dimastr.com) and search for the ID
comfortably.
 
F

Frank M.

Hi Michael.

Thank you for the information and programming. I almost got it…

I was able to find the ENTRYID, STOREID with your script, and BUTTON ID with
OUTSPY (although when I tried to get rid of OUTSPY.EXE, it corrupted Outlook;
I was not very pleased with what that programmer did but luckily I always
make a backup image before installing a shareware/freeware program and was
able to restore back before its installation).

I was able to create the following VB script:

---------
Private Const IMAP_ENTRYID As String = "00### DELETED FOR BREVITY 7F82800000"
Private Const IMAP_STOREID As String = "StoreID: 0000### DELETED FOR
BREVITY 400"
Private Const DEL_BUTTON As Long = 5583

Public Sub DeleteIMAP()
Dim oFld As Outlook.MAPIFolder
Dim oCmd As Office.CommandBarButton

Set oFld = Application.Session _
..GetFolderFromID(IMAP_ENTRYID, IMAP_STOREID)
Set Application.ActiveExplorer.CurrentFolder = oFld

Set oCmd = Application.ActiveExplorer_
..CommandBars.FindControl(,DEL_BUTTON)
oCmd.Execute
End Sub

-----

The first time I tried to run it, I received a security message about macros
being disabled. After changing the level to low and restarting OUTLOOK, I
tried to run it again, but it stopped at the following line with a yellow
line/arrow:

Public Sub DeleteIMAP()

After resetting and re-running it, it failed, placing the following line in
red and still pointing to the line above with a: “syntax errorâ€
message box

..CommandBars.FindControl(,DEL_BUTTON)

Can you see what I did wrong? And is there a way to identify this marco as
‘trusted’ or signed so I can reset security to HIGH?

Thanks for your patience with me.

Frank
 
M

Michael Bauer

Am Wed, 8 Feb 2006 11:30:19 -0800 schrieb Frank M.:

Frank, I don´t have your latest post here and don´t want to use the web
interface. Anyway, here´s your solution:

Set oCmd = Application.ActiveExplorer _
..CommandBars.FindControl(,DEL_BUTTON)

Between the first row´s last word and the underline must be one blank space.
Instead you could also delete the underline and merge both rows into one
row.
 
F

Frank M.

Michael,

That fixed it -- the macro runs and does the same thing as if I clicked on
the INBOX for IMAP.NET and then clicked the PURGE DELETED MESSAGES. It does
however have one undesrable behavior. After running it, it 'stays' in the
wrong Inbox folder (the one for the IMAP account). Is there a simple command
to tell it to come back to the primary INBOX folder, the one I have selected
as the default folder at startup?

Also, do you know how to make a macro a script or a custom action? In the
TOOLS, RULES and ALERTS, I do not have an option to execute a macro for when
mail arrives, only to 'run a script' or 'perform a custom action'. When I
go to either of these 'choices', they are blank.

Thanks again for your help.

Frank
 
M

Michael Bauer

Am Thu, 2 Mar 2006 17:29:28 -0800 schrieb Frank M.:

1)
With Application
Set .ActiveExplorer.CurrentFolder=.Session.GetdefaultFolder(olFolderInbox)
End With

2) For the run-a-script rule the declaration must look like this:

Public Sub AnyName(oMail As Outlook.MailItem)
'...
End Sub
 
J

John

Knowing that this post is old, I am posting my version of this macro. I know
I came accross this thread months later, and it helped me..... so to expand
on this:

This macro will save you all the trouble of the above discoveries. It will
step through all your MAPI folders and purge all deleted email from the
Inboxes.

This is the first macro I wrote for Outlook, and I just pieced it together
from the previous posters, and the help texts. It's really dirty, in that I
haven't done the usual declarations, but it works well.

Things to do:
1. I need to clean it up (a lot!)
2. I want to make it remember which folder I started out, and return me to
that same folder. Currently, it returns you to the default folder.

Public Sub PurgeAllInboxes()
Dim PURGE_BUTTON As Long
PURGE_BUTTON = 5583

'Create a pointer to the application
Set myOlApp = CreateObject("Outlook.Application")
'Get the root namespace
Set myNameSpace = myOlApp.GetNamespace("MAPI")

'How many top level folders? (accounts plus Personal Folder)
FolderCount = myNameSpace.Folders.Count

'Step through the folders
For i = 1 To FolderCount
'Select the folder
Set myFolder = myNameSpace.Folders(i)
'Select the Inbox of this top level folder
Set myNewFolder = myFolder.Folders("Inbox")
'Make this the active folder to execute a command upon
Set Application.ActiveExplorer.CurrentFolder = myNewFolder
'Setup the command
Set oCmd = Application.ActiveExplorer.CommandBars.FindControl(,
PURGE_BUTTON)
oCmd.Execute
Next i

'Return to the default folder when finished
With Application
Set .ActiveExplorer.CurrentFolder =
..Session.GetDefaultFolder(olFolderInbox)
End With

End Sub
 
J

John

Ok, here's the update where the macro returns to the folder you were in when
you ran the macro.

Still have to clean this up, of course.
--------------------------------------------------------------
Public Sub PurgeAllInboxes()
Dim PURGE_BUTTON As Long
PURGE_BUTTON = 5583

'Create a pointer to the application
Set myOlApp = CreateObject("Outlook.Application")
'Get the root namespace
Set myNameSpace = myOlApp.GetNamespace("MAPI")

'Remember which folder we stared out in
CurrentFolder = Application.ActiveExplorer.CurrentFolder

'How many top level folders? (accounts plus Personal Folder)
FolderCount = myNameSpace.Folders.Count

'Step through the folders
For i = 1 To FolderCount
'Select the folder
Set myFolder = myNameSpace.Folders(i)
'Select the Inbox of this top level folder
Set myNewFolder = myFolder.Folders("Inbox")
'Make this the active folder to execute a command upon
Set Application.ActiveExplorer.CurrentFolder = myNewFolder
'Setup the command
Set oCmd = Application.ActiveExplorer.CommandBars.FindControl(,
PURGE_BUTTON)
oCmd.Execute
Next i

'Return to the default folder when finished
With Application
Set .ActiveExplorer.CurrentFolder = CurrentFolder
End With

End Sub
 
D

Dirk

Michael,

Because I had the same problem I used your code and adapted if for my use.
This is the code I used

Public Const DIRK_IMAP_ENTRYID As String =
"00000000BB9DBA9F97320D489CC84636406E960A82800000"
Public Const DIRK_IMAP_STOREID As String =
"0000000038A1BB1005E5101AA1BB08002B2A56C200007073747072782E646C6C00000000000000004E495441F9BFB80100AA0037D96E000000443A5C446F63756D656E747320616E642053657474696E67735C64766572736C79705C4C6F63616C2053657474696E67735C4170706C69636174696F6E20446174615C4D6963726F736F66745C4F75746C6F6F6B5C4F75746C6F6F6B31302E322E33302E3230332D30303030303031362E70737400"
Public Const KANTOOR_IMAP_ENTRYID As String =
"00000000C4BB3B626D71B343A78BAE204409B51482800000"
Public Const KANTOOR_IMAP_STOREID As String =
"0000000038A1BB1005E5101AA1BB08002B2A56C200007073747072782E646C6C00000000000000004E495441F9BFB80100AA0037D96E000000443A5C446F63756D656E747320616E642053657474696E67735C64766572736C79705C4C6F63616C2053657474696E67735C4170706C69636174696F6E20446174615C4D6963726F736F66745C4F75746C6F6F6B5C4F75746C6F6F6B4F6E7476616E676B616E746F6F7220576576656C67656D2D30303030303031352E70737400"
Public Const DEL_BUTTON As Long = 5583 ' Enter the Button´s ID here

Public Sub DeleteIMAPDIRK()
Dim oFld As Outlook.MAPIFolder
Dim oCmd As Office.CommandBarButton

Set oFld = Application.Session.GetFolderFromID(DIRK_IMAP_ENTRYID,
DIRK_IMAP_STOREID)
Set Application.ActiveExplorer.currentFolder = oFld

Set oCmd = Application.ActiveExplorer.CommandBars.FindControl(, DEL_BUTTON)
oCmd.Execute
MsgBox "dirk uitgevoerd"
End Sub


Public Sub DeleteIMAPKANTOOR()
Dim oFld As Outlook.MAPIFolder
Dim oCmd As Office.CommandBarButton

Set oFld = Application.Session.GetFolderFromID(KANTOOR_IMAP_ENTRYID,
KANTOOR_IMAP_STOREID)
Set Application.ActiveExplorer.currentFolder = oFld

Set oCmd = Application.ActiveExplorer.CommandBars.FindControl(, DEL_BUTTON)
oCmd.Execute
MsgBox "kantoor uitgevoerd"
End Sub


Public Sub PurgeDeletedItemsIMAPDIRK(oItem As Outlook.MailItem)
Call DeleteIMAPDIRK
End Sub


Public Sub PurgeDeletedItemsIMAPKANTOOR(oItem As Outlook.MailItem)
Call DeleteIMAPKANTOOR
End Sub

I used the last two subs in rules, in the following way :
when the email is received, the rule moves a copy of it in a folder I
created and after that I let the script run (using one of the two last
subroutines).

When I do this the code in these subs is effectively run (the messagebox is
displayed) but the items marked for deletion or NOT purged. However when I
run the macro that is called in these subroutines - outside of the rules - it
does work fine. But why are these items not deleted when running the script
from within the rules ? Have You got any idea ? i'm working with outlook 2003

PS : excuse me if my english isn't all that good, because it's not my
language (I'm from Belgium). I hope you understand my problem
 
B

BP1973

Hello Frank,
THis is a very old posting but I though I would check to see if you can help
since I see you found a solution for one of my questions.
Can you share the steps t create a rule to copy a message to the IMAP
DELETED FOLDER?

I would really really appreciate it!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top