Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook VBA - Button to empty folder contents 1

Status
Not open for further replies.

oquinnc

Technical User
Feb 27, 2001
38
US
Has anyone created a button in Outlook to empty the contents of a specific folder?

For example, let's say we have a folder called "Briefings" -- I would like to have a button on the toolbar that when clicked, will empty the folder "Briefings." Once I have the VBA, I know how to create the button, it's the VBA that's tripping me up.

I have fooled around using the "Kill" command in VBA, but I can't seem to get it to work. I am also curious if it is possible to create a windows that say "Are you sure you want to empty the folder?".

Any help would be greatly appreciated!
 
I assume you mean an Outlook folder, not a Windows file folder. If so, then you need to use the delete method of a mailitem. For example, the following command deletes the first item in the currently selected folder:

activeexplorer.CurrentFolder.items(1).delete

You can do the prompt using a standard msgbox call:

if msgbox("Is it OK to delete?",vbYesNo)=vbYes then
...delete me...
end if
Rob
[flowerface]
 
Rob -- That is SO close! It works well, just needs a tweak or two. You were correct about this being an Outlook folder.

3 questions:

How could I tell it to delete ALL of the items, not just the first item?

How could I tell it to always empty the folder called "Briefings" no matter wha the current folder is?

The Message Box appears after the message has been deleted. I see the vbYes tells it to delete. Does it need some kind "vbNo" or "Else" to exit and do nothing.

Sorry if this elementary and I am missing it...VERY inexperienced with VBA. you seem to be the resident VBA expert, I believe I have given one or two of your posts the "expert" mark!
 
Okay, here's a bit more flesh around things:

Sub DeleteBriefings()
Dim fld As MAPIFolder, mi As MailItem
If MsgBox(&quot;Is it OK to delete?&quot;, vbYesNo) <> vbYes Then Exit Sub

Set fld = Application.GetNamespace(&quot;MAPI&quot;).GetDefaultFolder(olFolderInbox)
Set fld = fld.Folders(&quot;Briefings&quot;)
For Each mi In fld
mi.Delete
Next mi
End Sub

This assumes that your briefings folder is a subfolder of your inbox. You'll have to adjust accordingly if it's somewhere else.
Rob
[flowerface]
 
Rob is the Man!

That worked perfectly.

Since you are on a roll, I wanted to ask you about a peice of code you helped with a week or so ago:

Sub MoveOverThere()
Dim mi As MailItem
Dim fld As MAPIFolder
Set mi = ActiveExplorer.Selection.Item(1)
Set fld = Application.GetNamespace(&quot;MAPI&quot;).GetDefaultFolder(olFolderInbox)
Set fld = fld.Folders(&quot;Briefings&quot;)
mi.Move fld
End Sub

I am trying to get this to now make a copy instead of move the message. I changed the line &quot;mi.Move fld&quot; to &quot;mi.Copy fld&quot; -- that simply makes a copy of the message into the Inbox. It seems like it needs a small change in the folder references, but I am not sure what it is.

Casey
 
I spoke too soon...the empty folder commandn is hanging on:

For Each mi In fld

 
Oops, yes, it helps if I test my code (I just didn't have a folder I cared to empty :)). Make that
for each mi in fld.items

I think to copy your item you need two steps - first the copy command (as you are using), followed by the move command.

Rob
[flowerface]
 
The &quot;.items&quot; did it.

On the &quot;Copy&quot; button, I can only get it to work with two &quot;Subs&quot;, and I am not sure how to make 1 button run 2 subs, so I guess the alternative is to figure out how to combine the 2 subs into one...

Here is what I've got:

Sub CopyOverThere()
Dim mi As MailItem
Dim fld As MAPIFolder
Set mi = ActiveExplorer.Selection.Item(1)
Set fld = Application.GetNamespace(&quot;MAPI&quot;).GetDefaultFolder(olFolderInbox)
mi.Copy fld
End Sub

Sub MoveOverThere()
Dim mi As MailItem
Dim fld As MAPIFolder
Set mi = ActiveExplorer.Selection.Item(1)
Set fld = Application.GetNamespace(&quot;MAPI&quot;).GetDefaultFolder(olFolderInbox)
Set fld = fld.Folders(&quot;Briefings&quot;)
mi.Move fld
End Sub


I am sure it's simple enough, but I can only get it to copy into the current folder (Inbox) or move the message the appropriate folder (Briefings). I think the code is close, just needs to be combined.

Casey
 
Doesn't this work?

Sub CopyOverThere()
Dim mi As MailItem
Dim fld As MAPIFolder
Set mi = ActiveExplorer.Selection.Item(1)
Set fld = Application.GetNamespace(&quot;MAPI&quot;).GetDefaultFolder(olFolderInbox)
Set fld = fld.Folders(&quot;Briefings&quot;)
mi.Copy
mi.Move fld
End Sub

Rob
[flowerface]
 
All hail the king of VBA...it worked.

I was very close to that at one point. I forgot to include the &quot;fld&quot; after mi.Move.

Thank you for your help!
 
Ahem... I learned almost everything I know about VBA right here, from the REAL kings of VBA. But thanks for the title, anyway :)
Rob
[flowerface]
 
Rob,

Everything is working great with one minor glitch. When the the code is run, it doesn't delete all of the items in the folder. It appears that when it hits a message that has been forwarded, has been replied to, or has an attachment, it stops. You can run it again and it will keep going until the message type changes.

Basically you have to run it 3-4 times to clear out the folder. Any idea what would cause that?
 
I'm not sure - I had the same problem. I think it's a problem with the for each .. next construction getting confused by the deleted items. The following code works for me (I actually tested it this time!):

Sub DeleteBriefings()
Dim fld As MAPIFolder
If MsgBox(&quot;Is it OK to delete?&quot;, vbYesNo) <> vbYes Then Exit Sub
Set fld = Application.GetNamespace(&quot;MAPI&quot;).GetDefaultFolder(olFolderInbox)
Set fld = fld.Folders(&quot;Briefings&quot;)
Do While fld.Items.Count > 0
fld.Items(1).Delete
Loop
End Sub
Rob
[flowerface]
 
Rob is on my list of people to give money to if I ever become rich :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top