Tuesday, October 3, 2017

Visual Basic script to delete calendar appointments with a specific subject

I had a user on Outlook for Mac 2016 who had thousands of duplicate appointments (created by an Outlook bug).  Here was my fix . . .

I set up the user's account in my Outlook.

I pressed alt-F11 and expanded Project1 and then expanded Microsoft Outlook object and then expanded this Outlook session.  I double clicked on this Outlook session and put in this text in the right hand pane where DESIRED SUBJECT is the subject of the messages you want to remove.

When done, I clicked on the play button in the toolbar (green triangle) to run the script.


Option Explicit

Sub deleteOutlookAppt()
Dim olApp As Object 'Outlook.Application
Dim olNS As Object 'Outlook.Namespace
Dim olAptItemFolder As Object 'Outlook.Folder
Dim olAptItem As Object 'Outlook.AppointmentItem
Dim i As Long

    Set olApp = CreateObject("Outlook.Application")
    Set olNS = olApp.Session
    Set olAptItemFolder = olNS.GetDefaultFolder(9) '9=olFolderCalendar constant
   
    For i = olAptItemFolder.Items.Count To 1 Step -1
        Set olAptItem = olAptItemFolder.Items(i)
        If olAptItem.Subject Like "DESIRED SUBJECT" Then
            olAptItem.Delete
        End If
    Next i
   
    Set olAptItem = Nothing
    Set olAptItemFolder = Nothing
    Set olApp = Nothing
   
End Sub


No comments: