More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 27-Jul-2017 01:29

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 2 3 4 Next >> ]  
 Basic Outlook Printer Friendly Version
Working with Outlook items, folders, recipients; dealing with security; writing event handlers
Topic
imap move deleted item in inbox to trash folder
Hi

I found this .vbs here by sue
----------------------------------------------------------------------------------------
Sub purge()

Set myOlApp = CreateObject("Outlook.Application")

Dim myNameSpace As NameSpace

Set myNameSpace = myOlApp.GetNamespace("MAPI")

Dim myExplorer As Explorer

Set myExplorer = myOlApp.ActiveExplorer

'Get the folder type, expected type is 0 i.e. mail folder. If other type of folder
'being used then abort macro as it should only be used with mail folders.
folderType = myExplorer.CurrentFolder.DefaultItemType

'Check that folder is mail folder
If TypeName(myExplorer) = "Nothing" Or folderType <> 0 Then
GoTo invalidMailbox
End If

'Locate root folder for this account
Set thisFolder = myExplorer.CurrentFolder
Do Until thisFolder.Parent = myNameSpace
    Set thisFolder = thisFolder.Parent
Loop
Set accountFolder = thisFolder

'Identify selected messages
Dim selectedItems As Selection
Set selectedItems = myExplorer.Selection
Dim currentMailItem As MailItem
Dim iterator As Long

'Run loop on selected messages
For iterator = 1 To selectedItems.Count
Set currentMailItem = selectedItems.Item(iterator)

'Move messages to Deleted Items folder
Set trashFolder = accountFolder.Folders("Trash")
currentMailItem.Move (trashFolder)

Next

'Now, purge deleted messages
Dim myBar As CommandBar
Set myBar = Application.ActiveExplorer.CommandBarsaccount("Menu Bar")
Dim myButtonPopup As CommandBarPopup
Set myButtonPopup = myBar.Controls("Edit")
Dim myButton As CommandBarButton
Set myButton = myButtonPopup.Controls("Purge Deleted Messages")
myButton.Execute

Exit Sub

invalidMailbox:
MsgBox ("Macro configured only to work with mail folders! ")

Exit Sub

End Sub
---------------------------------------------------------------------------------------
When try to use it, it keeps coming back to me with error. the debug point to Set trashFolder = accountFolder.Folder("Trash") as the problem. My guess is, the script can not find my Trash folder, which I don't understand because its there under my Inbox

From what I can tell on the posting, this .vbs suppose to purge all deleted message in my Inbox and move it to my Trash folder.
Here is a visual display of my imap account

Inbox
  confirm-ham
  confirm-spam
  Drafts
  Sent Items
  Spam
  Trash

I don't know anything about vb. So if someone can point out the problem here for me, that would be great.

thanks

drluong

  14-Nov-2007  17:27
  15-Nov-2007  08:35   
If the trash folder is under the Inbox, then it's not in the accountFolder.Folders collection. Look at how the code derives accountFolder from the Parent of the currently displayed folder.
  15-Nov-2007  12:32   
Hi Sue

thanks for the reply.
Can you please offer more instruction on how to do what you suggested. This is my first time playing with Outlook and vbs coding. So i really don't know where to start. Really apprieciate the help. Thanks
 
  15-Nov-2007  12:38   
This statement returns the currently displayed folder:

    Set thisFolder = myExplorer.CurrentFolder

If that's the Inbox, then the Trash folder will be in the thisFolder.Folders collection instead of the accountFolder.Folders collection.

If there is part of the code that you don't understand, please ask specific questions -- after you have done your own homework in OUtlook's VBA Help file.
  15-Nov-2007  14:24   
Sue

thanks for the insight. I added the suggested correction and it worked. It purged the deleted items and move to the Trash folder but the problem now is it only move 1 of the deleted item to the Trash folder and purge the rest. How can I make the "Move messages to Trash folder" to loop therefore moving all deleted items to the Trash folder?

Here are the code
Sub purge()

Set myOlApp = CreateObject("Outlook.Application")

Dim myNameSpace As NameSpace

Set myNameSpace = myOlApp.GetNamespace("MAPI")

Dim myExplorer As Explorer

Set myExplorer = myOlApp.ActiveExplorer

'Get the folder type, expected type is 0 i.e. mail folder. If other type of folder
'being used then abort macro as it should only be used with mail folders.
folderType = myExplorer.CurrentFolder.DefaultItemType

'Check that folder is mail folder
If TypeName(myExplorer) = "Nothing" Or folderType <> 0 Then
GoTo invalidMailbox
End If

'Locate root folder for this account
Set thisFolder = myExplorer.CurrentFolder
Do Until thisFolder.Parent = myNameSpace
    Set thisFolder = thisFolder.Parent
Loop
Set accountFolder = thisFolder

'Identify selected messages
Dim selectedItems As Selection
Set selectedItems = myExplorer.Selection
Dim currentMailItem As MailItem
Dim iterator As Long

'Run loop on selected messages
For iterator = 1 To selectedItems.Count
Set currentMailItem = selectedItems.Item(iterator)

'Move messages to Trash folder
Set thisFolder = myExplorer.CurrentFolder
Set trashFolder = thisFolder.Folders("Trash")
currentMailItem.Move (trashFolder)

Next

'Now, purge deleted messages
Dim myBar As CommandBar
Set myBar = Application.ActiveExplorer.CommandBars("Menu Bar")
Dim myButtonPopup As CommandBarPopup
Set myButtonPopup = myBar.Controls("Edit")
Dim myButton As CommandBarButton
Set myButton = myButtonPopup.Controls("Purge Deleted Messages")
myButton.Execute

Exit Sub

invalidMailbox:
MsgBox ("Macro configured only to work with mail folders! ")

Exit Sub

End Sub

-------------------------------------------------------------------------------
Really sorry to bother you, I know I should do my own homework and figure this out but I need to get this working for my users as soon as possible. Any help would be greatly appreciated.

Thanks
 
  15-Nov-2007  14:38   
Your For loop needs to count down, not up. Also you don't need to return the folders on every pass of the loop. Do it once, before the loop starts:

Set thisFolder = myExplorer.CurrentFolder
Set trashFolder = thisFolder.Folders("Trash")
count = selectedItems.Count
For iterator = count to 1 Step -1
    Set currentMailItem = selectedItems.Item(iterator)
    currentMailItem.Move (trashFolder)
Next
  15-Nov-2007  17:10   
Hi Sue

I added your suggestion to the code but still only getting 1 message move to the trash folder and the rest purge. Any idea what I'm doing wrong. Here are the code:
Sub DeleteMessages()

Set myOlApp = CreateObject("Outlook.Application")

Dim myNameSpace As NameSpace

Set myNameSpace = myOlApp.GetNamespace("MAPI")

Dim myExplorer As Explorer

Set myExplorer = myOlApp.ActiveExplorer

'Get the folder type, expected type is 0 i.e. mail folder. If other type of folder
'being used then abort macro as it should only be used with mail folders.
folderType = myExplorer.CurrentFolder.DefaultItemType

'Check that folder is mail folder
If TypeName(myExplorer) = "Nothing" Or folderType <> 0 Then
GoTo invalidMailbox
End If

'Locate root folder for this account
Set thisFolder = myExplorer.CurrentFolder
Do Until thisFolder.Parent = myNameSpace
Set thisFolder = thisFolder.Parent
Loop
Set accountFolder = thisFolder

'Identify selected messages
Dim selectedItems As Selection
Set selectedItems = myExplorer.Selection
Dim currentMailItem As MailItem
Dim iterator As Long

'Move messages to Deleted Items folder
Set thisFolder = myExplorer.CurrentFolder
Set trashFolder = thisFolder.Folders("Trash")
Count = selectedItems.Count
For iterator = Count To 1 Step -1
    Set currentMailItem = selectedItems.Item(iterator)
    currentMailItem.Move (trashFolder)
  

Next

'Now, purge deleted messages
Dim myBar As CommandBar
Set myBar = Application.ActiveExplorer.CommandBars("Menu Bar")
Dim myButtonPopup As CommandBarPopup
Set myButtonPopup = myBar.Controls("Edit")
Dim myButton As CommandBarButton
Set myButton = myButtonPopup.Controls("Purge Deleted Messages")
myButton.Execute

Exit Sub

invalidMailbox:
MsgBox ("Macro configured only to work with mail folders! ")

Exit Sub

End Sub
 
  15-Nov-2007  17:57   
Hi Sue

Just made a discovery, it seem the move items to trash folder section of this script is only moving items that is highlighted and not those marked for deletion. For example, if I highlight message 1 2 3 and delete message 4 and 5, when I run the script, it will only move message 1 2 3 to the trash folder and purge 4 and 5.
So my question, how can I identify the deleted items and not highlighted item to move to the trash folder?

thanks for the help
 
  15-Nov-2007  18:55   
The code is doing exactly what you designed it to do. These statements tell Outlook to process all the selected items:

    Set selectedItems = myExplorer.Selection
    < snip >
    Set currentMailItem = selectedItems.Item(iterator)
    currentMailItem.Move (trashFolder)

There is no property that programmatically distinguishes deleted IMAP items from nondeleted items. If your goal is to programmatically move deleted items from one IMAP folder to another, Outlook doesn't provide any assistance for that functionality.
  15-Nov-2007  19:39   
Sue

I got it now. it's working just like you said and I'm ok with it. Thanks for clearing it up for me. I am finally at peace with this.
One last piece of info I would like to get from you. The purge section of the script ask for a confirmation. I would like to get rid of that. I tried adding sendKeys = "Yes" at the end but it doesn't do anything. what is the proper syntax? Below is how I set the purge part of the script

Dim myBar As CommandBar
Set myBar = Application.ActiveExplorer.CommandBars("Menu Bar")
Dim myButtonPopup As CommandBarPopup
Set myButtonPopup = myBar.Controls("Edit")
Dim myButton As CommandBarButton
Set myButton = myButtonPopup.Controls("Purge Deleted Messages")
myButton.Execute
sendkeys = "Yes"

Really appreciated the help (p.s tell me where to send the coffee!)
  15-Nov-2007  20:15   
First, you're not using SendKeys correctly. Look it up in VBA Help to see exaples.

Second, are "Y" "e" "s" literally the keystrokes you want to send?
    Page [ 1 2 3 4 Next >> ]