Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Excel VBA v VSTO Delete Outlook Appointment

I am trying to build a procedure in my Excel VB.net/VSTO add-in to delete an Outlook appointment.
I found the VBA code at the bottom and converted what I could but am not sure how to convert the following line:
"If oObject.Class = olAppointment Then"
I have marked the line with ???????

Public Sub DeleteAppointments(ByVal argSubject As String)

        Dim oApp As Microsoft.Office.Interop.Outlook.Application
        Dim oNameSpace As Microsoft.Office.Interop.Outlook.NameSpace
        Dim oApptItem As Microsoft.Office.Interop.Outlook.AppointmentItem
        Dim oFolder As Microsoft.Office.Interop.Outlook.MAPIFolder
        Dim oMeetingoApptItem As Microsoft.Office.Interop.Outlook.MeetingItem
        Dim oObject As Object

        Dim sErrorMessage As String

        oApp = New Microsoft.Office.Interop.Outlook.Application

        System.Threading.Thread.Sleep(3000) 'a bit of startup grace time.

        On Error GoTo Err_Handler
        oNameSpace = oApp.GetNamespace("MAPI")
        oFolder = oNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar)

        For Each oObject In oFolder.Items
            If oObject.Class = ??????? Then
                oApptItem = oObject
                If InStr(oApptItem.Subject, argSubject) > 0 Then
                    oApptItem.Delete()
                End If
            End If
        Next oObject

        oApp = Nothing
        oNameSpace = Nothing
        oApptItem = Nothing
        oFolder = Nothing
        oObject = Nothing

        Exit Sub

Err_Handler:
        sErrorMessage = Err.Number & " " & Err.Description

    End Sub


Option Explicit
Option Compare Text
 
Public Sub Driver()
 Call DeleteAppointments("wibble")
End Sub
 
Public Sub DeleteAppointments(ByVal argSubject As String)
 
 Dim oApp As Outlook.Application
 Dim oNameSpace As Outlook.Namespace
 Dim oApptItem As Outlook.AppointmentItem
 Dim oFolder As Outlook.MAPIFolder
 Dim oMeetingoApptItem As Outlook.MeetingItem
 Dim oObject As Object
 Dim iUserReply As VbMsgBoxResult
 Dim sErrorMessage As String
 
 On Error Resume Next
 ' check if Outlook is running
 Set oApp = GetObject("Outlook.Application")
 If Err <> 0 Then
   'if not running, start it
   Set oApp = CreateObject("Outlook.Application")
 End If
 
 On Error GoTo Err_Handler
 Set oNameSpace = oApp.GetNamespace("MAPI")
 Set oFolder = oNameSpace.GetDefaultFolder(olFolderCalendar)
 
 For Each oObject In oFolder.Items
   If oObject.Class = olAppointment Then
     Set oApptItem = oObject
     If InStr(oApptItem.Subject, argSubject) > 0 Then
       iUserReply = MsgBox("Appointment found:-" & vbCrLf & vbCrLf _
            & Space(4) & "Date/time: " & Format(oApptItem.Start, "dd/mm/yyyy hh:nn") _
            & " (" & oApptItem.Duration & "mins)" & Space(10) & vbCrLf _
            & Space(4) & "Subject: " & oApptItem.Subject & Space(10) & vbCrLf _
            & Space(4) & "Location: " & oApptItem.Location & Space(10) & vbCrLf & vbCrLf _
            & "Delete this appointment?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete Appointment?")
       If iUserReply = vbYes Then oApptItem.Delete
     End If
   End If
 Next oObject
 
 Set oApp = Nothing
 Set oNameSpace = Nothing
 Set oApptItem = Nothing
 Set oFolder = Nothing
 Set oObject = Nothing
 
 Exit Sub
 
Err_Handler:
 sErrorMessage = Err.Number & " " & Err.Description
 
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks for the help