Outlook

Delete Junk and Deleted Items with Progress Box

Ease of Use

Intermediate

Version tested with

2007 

Submitted by:

Zack Barresse

Description:

This code allows for the quick removal of all Junk mail items and then, in succession, the Deleted Items folder. A progress box is used to determine the total progress. 

Discussion:

In order to keep the Junk and Deleted Items folder down in size, especially with today's increasing amount of junk mail, this code was designed to empty these things in one swift button click. This code can be added to a command button on the toolbar for a one-click step. ProgressBox code originally found by Steve Bateman (here: http://www.outlookcode.com/codedetail.aspx?id=1077), functions slightly altered for efficiency. 

Code:

instructions for use

			

'-------------------------------------------------------------------------------------------------- 'IN A STANDARD MODULE Option Explicit 'Use the Sleep API call for the last portion of the userform, shown below Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long) Sub DeleteJunkAndDeletedItems() 'Dimension variables Dim flDeleted As Folder, flJunk As Folder, itDel As Object Dim i As Long, iTotal As Long 'Set folder objects Set flDeleted = Application.ActiveExplorer.Session.GetDefaultFolder(olFolderDeletedItems) Set flJunk = Application.ActiveExplorer.Session.GetDefaultFolder(olFolderJunk) 'Get a total count of items in both folders iTotal = flJunk.Items.Count + flDeleted.Items.Count 'Start the ProgressBox userform ProgressBox.Show 'If there is initial lag, set a message so the user doesn't get antsy ProgressBox.Increment 1, "Calculating..." 'First, loop through the Junk folder and delete ' You MUST do this one first because if you delete the ' deleted items first, then the junk, the junk will go ' right back into the deleted items folder! For Each itDel In flJunk.Items 'Set our increment for the progress box i = i + 1 'Update the progress box percentage and caption ProgressBox.Increment (i / iTotal) * 100, "Deleting 'Junk'..." 'Perform the actual delete itDel.Delete Next itDel 'Second, loop through the Deleted Items folder ' You MUST do this last as stated above For Each itDel In flDeleted.Items 'Set our increment for the progress box i = i + 1 'Update the progress box percentage and caption ProgressBox.Increment (i / iTotal) * 100, "Deleting 'Deleted'..." 'Perform the actual delete itDel.Delete Next itDel 'Show the Complete message ProgressBox.Increment 100, "Complete!" 'Hange for 250 ms so the user can actually read the complete portion Sleep 250 'Hide the ProgressBox userform ProgressBox.Hide End Sub '-------------------------------------------------------------------------------------------------- 'IN ANY USERFORM WITH THE NAME OF 'ProgressBox' Option Explicit Private Const DefaultTitle = "Progress" Private myText As String Private myPercent As Single ' Text property shows user-defined text above the progress bar Public Property Let Text(newText As String) If newText <> myText Then myText = newText Me.Controls("UserText").Caption = myText Call sizeToFit End If End Property Public Property Get Text() As String Text = myText End Property ' Percent property alters the progress bar Public Property Let Percent(newPercent As Single) If newPercent <> myPercent Then ' limit percent to between 0 and 100 myPercent = Min(Max(newPercent, 0#), 100#) Call updateProgress End If End Property Public Property Get Percent() As Single Percent = myPercent End Property ' Increment method enables the percent and optionally the text to be updated at same time Public Sub Increment(ByVal newPercent As Single, Optional ByVal newText As String) Me.Percent = newPercent If newText <> "" Then Me.Text = newText Call updateTitle Me.Repaint End Sub ' Setup the progress dialog - title, control layout/size etc. Private Sub UserForm_Initialize() Call setupControls Call updateTitle End Sub ' Prevents use of the Close button Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = vbFormControlMenu Then End 'Cancel = True End Sub ' Removes any current controls, add the needed controls ... Private Sub setupControls() Dim i As Integer Dim aControl As Label ' remove existing controls For i = Me.Controls.Count To 1 Step -1 Me.Controls(i).Remove Next i ' add user text - don't worry about positioning as "sizeToFit" takes care of this Set aControl = Me.Controls.Add("Forms.Label.1", "UserText", True) aControl.Caption = "" aControl.AutoSize = True aControl.WordWrap = True aControl.Font.Size = 8 ' add progressFrame - don't worry about positioning as "sizeToFit" takes care of this Set aControl = Me.Controls.Add("Forms.Label.1", "ProgressFrame", True) aControl.Caption = "" aControl.Height = 16 aControl.SpecialEffect = fmSpecialEffectSunken ' add user text - don't worry about positioning as "sizeToFit" takes care of this Set aControl = Me.Controls.Add("Forms.Label.1", "ProgressBar", True) aControl.Caption = "" aControl.Height = 14 aControl.BackStyle = fmBackStyleOpaque aControl.BackColor = &HFF0000 ' Blue ' position the controls and size the progressBox Call sizeToFit End Sub ' Adjusts positioning of controls/size of form depending on size of user text Private Sub sizeToFit() ' setup width of progress box Me.Width = 240 ' user-supplied text should be topmost, taking up the appropriate size ... Me.Controls("UserText").Top = 6 Me.Controls("UserText").Left = 6 Me.Controls("UserText").AutoSize = False Me.Controls("UserText").Font.Size = 8 Me.Controls("UserText").Width = Me.InsideWidth - 12 Me.Controls("UserText").AutoSize = True ' progress frame/bar should be below user text Me.Controls("ProgressFrame").Top = Int(Me.Controls("UserText").Top + Me.Controls("UserText").Height) + 6 Me.Controls("ProgressFrame").Left = 6 Me.Controls("ProgressFrame").Width = Me.InsideWidth - 12 Me.Controls("ProgressBar").Top = Me.Controls("ProgressFrame").Top + 1 Me.Controls("ProgressBar").Left = Me.Controls("ProgressFrame").Left + 1 Call updateProgress ' update ProgressBar width ' finally, height of progress box should fit around text and progress bar & allow for title/box frame Me.Height = Me.Controls("ProgressFrame").Top + Me.Controls("ProgressFrame").Height + 6 + (Me.Height - Me.InsideHeight) End Sub ' updates the caption of the progress box to keep track of progress Private Sub updateTitle() If (Int(myPercent) Mod 5) = 0 Then Me.Caption = DefaultTitle & " - " & Format(Int(myPercent), "0") & "% Complete" End If End Sub ' updates the width of the progress bar to match the current percentage Private Sub updateProgress() If myPercent = 0 Then Me.Controls("ProgressBar").Visible = False Else Me.Controls("ProgressBar").Visible = True Me.Controls("ProgressBar").Width = Int((Me.Controls("ProgressFrame").Width - 2) * myPercent / 100) End If End Sub ' Min and Max functions Function Min(number1 As Single, number2 As Single) As Single If number1 < number2 Then Min = number1 Else Min = number2 End Function Function Max(number1 As Single, number2 As Single) As Single If number1 > number2 Then Max = number1 Else Max = number2 End Function '--------------------------------------------------------------------------------------------------

How to use:

  1. Open Outlook
  2. Open the VBE (Alt + F11)
  3. Create a new UserForm ( Insert | UserForm)
  4. Press F4 to bring up the Properties window
  5. Type "ProgressBox" in the 'Name' field
  6. Change the 'ShowModal' property from True to False
  7. Copy/paste code above stating "'IN ANY USERFORM WITH THE NAME OF 'ProgressBox'" - everything below that line
  8. Insert a new code module (Insert | Module)
  9. Copy/paste code above stating "'IN A STANDARD MODULE" - everything below that line to the start of the UserForm code
  10. Save the project
  11. You'll have to ensure macros are enabled (Tools | Macro | Security) or at least with notification of macros to run this
  12. The project can be saved but will not run with the security too high
  13. If security is too high and lowered to run this code, you must close Outlook and restart it before the security changes will take affect
  14. Now close the VBE
  15. In Outlook, click Tools | Customize | Commands (tab)
  16. Scroll down the left list box and select Macros
  17. On the right list box you'll see the project name and your routine name (should be "Outlook_VBA.DeleteJunkAndDeletedItems"
  18. Click and drag it to the menu where you like (I recommend the Standard command bar somewhere as it is open in about all of the different facets of Outlook)
  19. Right click the control, change the name as desired (I recommend 'Clear Folder Space') and Image if desired
  20. Close the Customize dialog box
 

Test the code:

  1. After the directions have been followed above, click your new command button
  2. REMEMBER - there are currently no message boxes asking you if you want to do this, so if you have people who send messages which end up in your Junk mail folder you may want to check it first before you run this routine!
  3. Alternatively, open the VBE (Alt + F11), put your cursor in the 'DeleteJunkAndDeletedItems' routine and press F5
  4. NOTE - sample file attached is a ZIP file containing the userform module (frm) and standard module (bas), import both into your Outlook VBA Project, then just create your command button and voila!
 

Sample File:

ProgressBox.zip 1.28KB 

Approved by Zack Barresse


This entry has been viewed 40 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express