This Lazy Programmer's code sample is built from my all-purpose bulk contact code sample. While most people won't make a lot of bulk changes to appointments, it can be useful make some changes, especially when you use an If statement to limit the appointments that are changed.
Public Sub DoSomethingWithAppt() Dim objFolder As Outlook.MAPIFolder Dim objItems As Outlook.Items Dim objAppt As Outlook.AppointmentItem Dim obj As Object 'On Error Resume Next Set objFolder = Session.GetDefaultFolder(olFolderCalendar) Set objItems = objFolder.Items For Each obj In objItems Set objAppt = obj With objAppt ' do whatever .Save End With Err.Clear Next Set obj = Nothing Set objAppt = Nothing Set objItems = Nothing Set objFolder = Nothing End Sub
Delete Appointments using a macro
When you delete items, you need to count backwards, otherwise you'll delete every other item.
In this code sample, we're deleting appointments in the Holiday category. Change the references to appointments and the calendar to use it with other Outlook item types.
Public Sub DeleteAppt() Dim objFolder As Outlook.MAPIFolder Dim objItems As Outlook.Items Dim objAppt As Outlook.AppointmentItem Dim obj As Object 'On Error Resume Next Set objFolder = Session.GetDefaultFolder(olFolderCalendar) Set objItems = objFolder.Items For i = objItems.Count To 1 Step -1 Set objAppt = objItems(i) With objAppt ' do whatever here If InStr(1, LCase(.Categories), "holiday") Then .Delete End If '.Save End With Err.Clear Next i Set obj = Nothing Set objAppt = Nothing Set objItems = Nothing Set objFolder = Nothing End Sub
Note: Because the text is case sensitive, you either need to use lower case text in the statements, such as If InStr(1, LCase(.Categories), "holiday") Then or use the correct case and remove LCase from the code, such as If InStr(1, .Categories, "Holiday") Then
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
Hello,
I am trying to use the delete appointments code in combination with another, working code I have to add appointments to a specific folder. That code I created by combining elements I found on this site with one code I found elsewhere.
I am trying to specify to this code that I only want items from a specific folder deleted, but adding
Set oFolder = oNameSpace.GetFolderFromID("0000000040CF0A647269524CB2384CFFC462B1A601002BCFC23C040B1041A64497CD63CF3252025FFFBB009E0000")
and changing the first line of DeleteAppt to
Dim oFolder As Object
isn't working.How would you suggest I go about telling DeleteAppt to only delete items from a specific Outlook folder (calendar)?
Thank you for the fantastic content you create!
You can call the folder by name - see https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/
Thank you, that's very helpful!
I'm now getting "Run-time error '91': Object variable or With block variable not set" on this line:
Set objItems = objFolder.Items
Does this error have an easy fix? :) Thank you again.
If objfolder referencing an existing folder? What code are you using to identify the folder?
Sorry for my lack of knowledge here but I'm not sure I understand the questions.
Does this help?:
Remove items from the folder, so it looks like this:
Set objFolder = Session.GetDefaultFolder(olFolderCalendar)
.Folders("Calendar")Set objItems = objFolder.Items
Oops - that is not right - well, it is, if a folder named Calendar is a subfolder of the Calendar - to use the default calendar, you just need
Set objFolder = Session.GetDefaultFolder(olFolderCalendar)
Set objItems = objFolder.Items
Thank you for your help.
To use the default calendar, you just need this:
Set objFolder = Session.GetDefaultFolder(olFolderCalendar)
Set objItems = objFolder.Items
I have fixed a typo earlier in the code that seems to have allowed the code to run without error, and now the appointments in the specified calendar are not being deleted. Actually, no calendar appointments are being deleted. I'm not sure where I went wrong or what my next best steps are. Thank you for any advice.
Make sure you use this code - to identify the default data Calendar folder:
Set objFolder = Session.GetDefaultFolder(olFolderCalendar)
Set objItems = objFolder.Items
(The delete sample on the web page is working here for appointments in the holiday category.)