deleting all messages in a folder

G

Guest

What code can I use to delete all messages in a folder (e.g. spam mail folder)?

Thanks in advance!
 
M

Michael Bauer [MVP - Outlook]

Am Wed, 16 Aug 2006 12:38:01 -0700 schrieb Ariel:

Sample for the Junk folder:

Dim Items as OUtlook.Items
Set Items=Application.Session.GetDefaultFolder(olFolderJunk).Items
While Items.Count
Items(1).Delete
Wend
 
G

Guest

Hi Michael,

I am getting an error on the line:

Set Items = Application.Session.GetDefaultFolder(olFolderJunk).Items

Stating "Could not complete the operation. One or more parameter values are
not valid."

Help!!!

Thanks for your quick repsonse and assistance!
 
M

Michael Bauer [MVP - Outlook]

Am Thu, 17 Aug 2006 18:18:02 -0700 schrieb Ariel:

That´s a sampel for getting the Junk folder in OL 2003. If you use OL 2000
then you do not have that standard folder.

You need to know what folder you´re looking for. If it´s a subolder of the
Inbox e.g. then you could also write:

Dim Folder as Outlook.Folder
Dim Items as Outlook.Items

Set Folder=Application.Session.GetDefaultFolder(olFolderInbox)
Set Folder=Folder.Folders("name")
Set Items=Folder.Items
 
G

Guest

Guten tag, Michael.

I am using Outlook 2003. The Spam folder is a folder under Personal Folders.
This code does not appear to find that folder, and thus it ends without doing
anything.

Danke,

Ariel
 
M

Michael Bauer [MVP - Outlook]

Am Fri, 18 Aug 2006 08:24:51 -0700 schrieb Ariel:

Hallo Ariel,

does the latter sample raise the same error? Where do you insert that code,
is it the module ThisOutlookSession (VBA) or a custom form (VBS)?

In VBS the constants are unknown, instead use their values directly; for
olFolderJunk it´s 23, for olFolderInbox it´s 11.

Here´s a samle for getting folders by their path:
www.outlookcode.com/d/code/getfolder.htm
 
G

Guest

Hi Michael,

I am placing the code in the ThisOutlookSession module, so I assume that the
path is "Personal Folders\Spam."

I am not getting any error message now, but nothing is happening?

Thanks for your help.

Ariel
 
M

Michael Bauer [MVP - Outlook]

Am Sun, 20 Aug 2006 14:56:02 -0700 schrieb Ariel:

Ariel, please walk through the code execution step by step (F8) and watch
what happens. Don´t use an "On Error Resume Next" statement for debugging.

It all depends on that the folder will be found, so you don´t need to
"assume" but to know the folder :)

BTW: Any folder named "Spam" is not the default Junk Folder.
 
G

Guest

Hi Michael,

When walking through the code, it fails at that same line as before:

Set Folder = Application.Session.GetDefaultFolder(olFolderSpam)

I assume that you believe that it has to do with not finding the correct
folder?

Will the function you gave me tell me this? If so, how do I run this within
Outlook (I am an advanced programmer in VBA for access and excel, but outlook
is a different animal).

Thanks for your continued support!
 
K

Ken Slovak - [MVP - Outlook]

Take this code and place it either in ThisOutlookSession or in a code
module. Place your cursor in the procedure and press F5 to run it. Report
back what happens.

Public Sub DeleteAllJunk()
Dim oFolder As Outlook.MAPIFolder
Dim colItems As Outlook.Items
Dim oItem As Object
Dim i As Long

Set oFolder = Application.Session.GetDefaultFolder(olFolderJunk)
Set colItems = oFolder.Items
If colItems.Count > 0 Then
For i = colItems.Count To 1 Step -1
Set oItem = colItems.Item(i)
oItem.Delete
Next i
End If
End Sub
 
G

Guest

Hi Ken,

I placed the routine in ThisOutlookSession and got the same error as always

"Could not complete the operation. One or more parameter values are
not valid."

There is a Junk folder and it is sub folder under Personal Folders, so I
don't understand why this is not working :-(

Thanks for your help...

Ariel
 
M

Michael Bauer [MVP - Outlook]

Am Mon, 21 Aug 2006 21:41:02 -0700 schrieb Ariel:

That´s really weird. Just to be sure: Please replace the constant
olFolderJunk by its value (23).

Did you try the mentioned GetFolder function? What happens if you pass the
path?
 
G

Guest

I just got the same error again.

Please tell me how to run the function. Does it need to be imbedded within a
sub routine or can it be run on its own like in excel or access?
 
M

Michael Bauer [MVP - Outlook]

Am Mon, 21 Aug 2006 22:51:02 -0700 schrieb Ariel:

Please copy the GetFolder function into the module ThisOutlookSession. Then
you can call it from within the sample Ken gave you with:

Set objFolder = GetFolder("Personal Folders/Inbox")

Please ensure that the path exists respectively edit the path.
 
K

Ken Slovak - [MVP - Outlook]

Michael,

It almost sounds like something is buggered in her PST file. Maybe running
ScanPST.exe a couple of times with Outlook closed and then opening Outlook
with the /resetfolders switch (Start, Run dialog: outlook.exe /resetfolders)
might be useful. It can't hurt and may help.

Other than that it's off to OutlookSpy to see if the default Store has the
correct EntryID references for the default folders.

This is for the default PST file?
 
M

Michael Bauer [MVP - Outlook]

Am Tue, 22 Aug 2006 10:10:07 -0400 schrieb Ken Slovak - [MVP - Outlook]:

Good point, Ken. I never experienced that a default folder can´t be found.
 
G

Guest

Michael and Ken,

I tried the function but it errored out on a type mismatch.

I will run the scanPST as Michael suggests and try this again. I have
Outlook Spy loaded, so if you tell me where to look for the folder code, I
can try and retrieve it

Thanks again, guys

Ariel

Michael Bauer said:
Am Tue, 22 Aug 2006 10:10:07 -0400 schrieb Ken Slovak - [MVP - Outlook]:

Good point, Ken. I never experienced that a default folder can´t be found.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --

Michael,

It almost sounds like something is buggered in her PST file. Maybe running
ScanPST.exe a couple of times with Outlook closed and then opening Outlook
with the /resetfolders switch (Start, Run dialog: outlook.exe /resetfolders)
might be useful. It can't hurt and may help.

Other than that it's off to OutlookSpy to see if the default Store has the
correct EntryID references for the default folders.

This is for the default PST file?
 

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