Sample Outlook macros to delete or move items in bulk

Article translations Article translations
Article ID: 2008840 - View products that this article applies to.
Expand all | Collapse all

Symptoms

When you delete a meeting from your calendar you receive one of the following prompts, depending on your role in the meeting:

  • Organizer of a recurring meeting

You receive the following prompt:

Do you want to delete all occurrences of the recurring appointment "meeting subject", or just this one?"

  • Organizer of a non-recurring meeting

The meeting form opens and the InfoBar has the following text:

The cancellation has not been sent for this meeting. Click Send Cancellation to update meeting attendees.

  • Attendee of a recurring meeting

You receive the following prompt:

Do you want to delete all occurrences of the recurring appointment "meeting subject", or just this one?"

  • Attendee of a non-recurring meeting

You receive the following prompt:

The meeting "meeting subject" was already accepted.

  • Delete and send a response to the meeting organizer.
  • Delete without sending a response

If you are deleting one meeting, these prompts are not necessarily an imposition. However, these symptoms can also occur when you delete a large number of items from a calendar at one time, halting the deletion process until you process the displayed prompt for each meeting. The following example of one scenario where these prompts may prove to be problemmatic.

Someone takes over as an administrator of a calendar that contains 10,000 items. The new administrator realizes that many of the items are not necessary and could be deleted to make the calendar more manageable. No update would be needed for attendees of these deleted meetings. In this scenario, if the administrator selects a large number of meetings and presses the Delete key, they are prompted to act on each item individually. They are not allowed to delete or move a bulk selection of the items without responding to a prompt for each item.

Cause

Items in the Outlook calendar contain a variety of information that is integrated with other features. For example, appointments typically have reminders, which means they will trigger a reminder dialog. Also, meetings usually have attendees and when a cancellation is made, updates must be sent to the attendees. These “side effects” of working with calendar data are expected and part of the core design of Outlook.

Resolution

The Outlook object model offers a programmatic solution to this problem. When calendar items are deleted or moved using the object model, Outlook internally knows to suppress any user prompts.

This article explains how to use the Outlook object model to create a simple macro to delete the items, and a more flexible macro to provide a dialog box for deleting or moving the items.

More Information

Note The sample macros in this article are intended to illustrate the idea of using the Outlook object model commands to process calendar items without triggering prompts by Outlook. They are not complete, or fully tested, and should be used only as a sample for writing your own more robust macros.

When a macro is used to delete or move calendar items, Outlook internally keeps the item “open” during the whole course of the macro’s lifetime. So for example, if you select and delete 100 items with the macro, Outlook will open 100 items “under the hood” and those items will not be closed until the macro finishes running. This design leads to a limitation of processing 250 items at a time for online mode profiles using a Microsoft Exchange Server account. By default, Exchange server limits MAPI clients to 250 open messages at one time. When using these macros or developing your own, note that you will only be able to select and operate on approximately 250 items at a time. This limitation is documented along with the information about re-configuring the limit in the following article in the Microsoft Knowledge Base:

http://support.microsoft.com/kb/830836

Note If you do not want to manually enter the sample macro code provided in this article, you can download sample files containing the same code listed in this article. To access these sample files, please use the link provided in the Download sample macro and form files section of this article.

Sample macro to delete items without displaying any prompts

The Outlook object model’s Selection object gives access to the collection of selected (highlighted) items. You can loop through this collection and call the Delete method for each item. In the most basic form, this will allow you to delete all the selected items without any prompts or interruptions.

Example:

Sub DeleteAllSelected()

' Delete Selected Items with No Error Checking

Dim oneitem, onecollection As Object
Set onecollection = Application.ActiveExplorer.Selection
iTotalItems = onecollection.Count

For i = iTotalItems To 1 Step -1
Set oneitem = onecollection.Item(i)
oneitem.Delete
Set oneitem = Nothing
Next

End Sub

To see this sample macro in action, follow these steps:

  1. Set your macro security to allow macros to run:
    1. On the Tools menu click Macro and then click Security.
    2. Select Warnings for all macros and then click OK.
    3. Exit and restart Outlook.
  2. Enter the sample macro code into a module in the Visual Basic Editor.
    1. Press Alt+F11 to launch the Visual Basic Editor.
    2. Click Enable Macros in the Microsoft Office Outlook Security Notice dialog box.
    3. On the Insert menu click Module.
    4. Paste the above sample macro code into the module window.
    5. Close the Visual Basic Editor window.
  3. Test your macro:
    1. Select the Calendar module in the Navigation Pane.
      2. On the View menu point to Current View and then select By Category.
      3. Select several test meetings to be deleted.
      4. On the Tools menu point to Macro and then click Macros.
      5. In the Macros dialog box, select the DeleteAllSelected macro and then click Run.

All of the selected meetings will be deleted from the calendar without any prompts from Outlook.

Extending the Macro to handle error conditions

One common problem with macros that loop through a collection of items is dealing with unexpected errors. For example, consider that in a selected list of 100 meetings, the 50th item is damaged and cannot be deleted. In this situation, the previous macro will delete 49 items; produce an error trying to delete the 50th item, and then stop.

The addition of some basic error handling to the macro can allow the macro to continue the deletion process, even when an error occurs. If you use the folllowing macro code ins

Sub DeleteAllSelectedExtended()

' Delete Selected Items with basic Error Checking

On Error GoTo HandleIt

Dim oneitem, collection As Object
Set onecollection = Application.ActiveExplorer.Selection
iTotalItems = onecollection.Count

For i = iTotalItems To 1 Step -1
   Set oneitem = onecollection.Item(i)
   oneitem.Delete
   Set oneitem = Nothing
   TryNextItem:
Next

Exit Sub

HandleIt:
Resume TryNextItem

End Sub

To see this sample macro in action, follow these steps:

  1. Enter the sample macro code into a module in the Visual Basic Editor.
    1. Press Alt+F11 to launch the Visual Basic Editor.
    2. Click Enable Macros in the Microsoft Office Outlook Security Notice dialog box.
    3. On the Insert menu click Module.
    4. Paste the above sample macro code into the module window.
    5. Close the Visual Basic Editor window.
  2. Test your macro:
    1. Select the Calendar module in the Navigation Pane.
    2. On the View menu point to Current View and then select By Category.
    3. Select several test meetings to be deleted.
    4. On the Tools menu point to Macro and then click Macros.
    5. In the Macros dialog box, select the DeleteAllSelectedExtended macro and then click Run.

All of the selected meetings will be deleted from the calendar without any prompts from Outlook.

Download sample macro and form files

Instead of manually entering in the macro code, you could instead download sample files that contain the macro code listed in this article. Use the following steps to use these files:

  1. Click the following link to begin the download for the package containing the files:

    2008840Macros.EXE

  2. When prompted, click Save.
  3. Save the 2008840Macros.EXE file to any download location on your computer.
  4. When the download is finished doubleclick 208840Macros.EXE to extract the sample files.
  5. Click Run when prompted.
  6. Click Yes if you agree tot he end-user liscense agreement.
  7. When prompted, specify a folder location in which to extract the sample files, and then click Unzip.
  8. click OK after the files are unzipped successfully.

  The following files are contained in the download package and are referenced in the next two sections of the article.

Article2008840.bas

Article2008840_form.frm

Article2008840_form.frx

eula.txt

 

Importing a module that includes the above macro code

If you do not want to manually enter the above macro code into a module in the Visual Basic Editor, please use the following steps to import a module that contains the above macro code.

  1. Press Alt+F11 to launch the Visual Basic Editor.
  2. On the File menu click Import File.
  3. Locate and select Article2008840.bas and then click Open.
  4. Close the Visual Basic Editor.

After importing the module into the Visual Basic Editor, use the following steps to run either macro.

  1. Select the Calendar module in the Navigation Pane.
  2. On the View menu point to Current View and then click By Category.
  3. Select several test meetings to be deleted.
  4. On the Tools menu point to Macro and then click Macros.
  5. In the Macros dialog box, select either the DeleteAllSelected or DeletedAllSelectedExtended macro and then click Run.

Using a custom form to move or delete meetings in bulk

This basic idea of using the Outlook object model methods to delete meetings can be extended to other actions as well. For example, you could move the items to another folder instead of the deleting them. This article includes a sample custom form that illustrates some additional options for the processing of selected items (The screen shot for the sample custom form is listed below).

The screen shot for the custom form that can move or delete meetings.

This custom form demonstrates examples of the following actions:

  • Using a userform to present a dialog box with choices to the user 
  • Making the dialog controls “smart”. For example the Folder button is not active until the ‘Move to Folder’ option is selected. 
  • Asking the user for confirmation before processing any actions 
  • Basic error checking in the macro code behind the form.
  • Status of the processing, presented in the status bar at the bottom of the dialog box.

If you want to use this custom form to manage (move or delete) meetings in a Calendar, please use the following steps to import the custom form and the module that contains a simple macro to launch the form.

  1. Press Alt+F11 to launch the Visual Basic Editor.

    Note You can skip steps 2-3 if you have already imported the sample module.

  2. On the File menu click Import File.
  3. Locate and select Article2008840.bas and then click Open.

    Note There is also a file called Article2008840_form.frx that is a supporting file for the custom form. Please make sure the Article2008840_form.frm and Article2008840_form.frx file are in the same folder. 

  4. On the File menu click Import File.
  5. Select Article2008840_form.frm and then click Open.
  6. Close the Visual Basic Editor.

After importing the module and form into the Visual Basic Editor, use the following steps to run the form.

  1. Select the Calendar module in the Navigation Pane.
  2. On the View menu point to Current View and then click By Category.
  3. Select several test meetings to be deleted.
  4. On the Tools menu point to Macro and then click Macros.
  5. In the Macros dialog box, select the DisplayMeetingDeleteMoveForm macro and then click Run.
  6. Select either Delete or Move to Folder (if you select Move to Folder, click Folder to select the destination folder for the moved items)
  7. Click Start.

Properties

Article ID: 2008840 - Last Review: September 3, 2013 - Revision: 3.0
Applies to
  • Microsoft Office Outlook 2003
  • Microsoft Office Outlook 2007
Keywords: 
KB2008840

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com