locked
Delete all appointments from Outlook Calendar RRS feed

  • Question

  • UPDATE

    Please check the update below first

    -----------------------------------------

    Original post

    Hey Guys,

    I've got an HTML file that has a table in it. The table is some kind of a planboard. It contains the amount of planned hours for an employee for a customer on day x over a whole year. Our guys need it in their outlook schedules.

    Here is what i've built so far

    - Form to set parameters (select starttime, select input html file, alerting on or off)

    - Function that deletes all appointments within a set time frame that have a subject that starts with "project: "

    - Class that extracts all appointments from html and transforms them into outlook appointments

    Everything works peachy! -Except deleting appointments. I'm stuck! I've tried making a selection of appointments between two dates, i've tried looking for appointments on all days between to dates. It does not delete all items, just some of them. Of the 23 occurences it should delete, it only deletes 12 OR 13 at random.

    Hope you guys can help me out with this one,

    Cheers,

    John

     

    • Edited by John Bravo Saturday, February 26, 2011 11:39 AM
    Friday, February 25, 2011 6:43 PM

Answers

  • John,

    I not sure if this will work as I do not have the Office interop library installed to test it. If a recall correctly, when working with a collection and deleting items you have to work from last to first.

    Try something like this.

    For j As Int32 = SelectedOutlookCalendarItems.Count - 1 To 0 Step -1
      If TypeOf SelectedOutlookCalendarItems.Item(j) Is Outlook.AppointmentItem Then
          SelectedOutlookCalendarItems.Item(j).Delete()
          TotalDeleted += 1
       End If
    Next j

     

     

    • Marked as answer by John Bravo Monday, February 28, 2011 9:08 AM
    Sunday, February 27, 2011 3:46 PM

All replies

  • Have you set breakpoints and stepped through the code yet? I see your exception handler in your delete code does nothing at all (and there is an assumption that its probably a meeting request). You could be getting an error for some other reason, and are just ignoring it and going on to the next item.


    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
    Friday, February 25, 2011 8:26 PM
  •  

    Check out this LINK

     


    Only performance counts!
    Friday, February 25, 2011 9:04 PM
  • Thanks for your reply!

    To answer your question: Yes, I've set breakpoint and stepped through it endlessly... It is indeed an assumption that they are meeting requests if an exception is thrown. At least that's what i figured. The thing is, the collection of appointments contains "all" appointments on your calendar. In my test case that is over 1.600. The first 50 i've seen, and they were all meeting requests - Hence my assumption.

    I'm open for suggestions on how to retrieve / filter / sort / restrict the collection of real-appointments more before looping through them.

    Thanks again for your thoughts!

    Cheers,

    John

     

    Friday, February 25, 2011 9:05 PM
  •  

    Check out this LINK

     


    Only performance counts!
    ? ==> that is "this" link right?
    Friday, February 25, 2011 10:06 PM
  • Update

    So i've played around with it a little bit. The filtering of the calendar items is the -actual - issue. So here is my new sub. It prompts me with an error:

    [Start] >= '02/01/2011' 12:00 AM AND [End] <= '03/01/2011' 11:59 PM
    A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred

    The bug must be in the date selection string

     

    Dim OutlookNameSpace As Outlook.NameSpace
      Dim OutlookCalendar As Outlook.MAPIFolder
      Dim OutlookCalendarItems As Outlook.Items
      Dim Appointment As Microsoft.Office.Interop.Outlook.AppointmentItem
    
      Private Sub DeleteExistingAppointments(ByVal BodyStart As String)
        Try
          start = Me.dStart.Value
          eind = Me.dEnd.Value
    
          OutlookNameSpace = objOutlook.GetNamespace("mapi")
          OutlookCalendar = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
          OutlookCalendarItems = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items
    
          With OutlookCalendarItems
            .Sort("[Start]", False)
            .IncludeRecurrences = True
          End With
    
          Dim DateSelection As String = "[Start] >= '" & Format(start, "MM/dd/yyyy") & "' 12:00 AM" & " AND [End] <= '" & Format(eind, "MM/dd/yyyy") & "' 11:59 PM"
          Debug.Print(DateSelection)
    
          Dim SelectedOutlookCalendarItems As Outlook.Items
          SelectedOutlookCalendarItems = OutlookCalendarItems.Restrict(DateSelection)
          Debug.Print(SelectedOutlookCalendarItems.Count)
    
          For Each CalendarItem In SelectedOutlookCalendarItems
            If TypeOf CalendarItem Is Outlook.AppointmentItem Then
              Debug.Print("Datum: " & Format(CDate(CalendarItem.start), "dd/MM/yyyy") & vbCrLf & "Subject: " & CalendarItem.Subject)
            End If
          Next
        Catch ex As Exception
          Debug.Print(ex.ToString)
        End Try
      End Sub
    
    

    Thanks!

    John


    Saturday, February 26, 2011 11:36 AM
  • The bug must be in the date selection string

    From the documentation

     

    Date

    Although dates and times are typically stored with a Date format, the Find and Restrict methods require that the date and time be converted to a string representation. To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. The following example creates a filter to find all contacts that have been modified after January 15, 1999 at 3:30 P.M.

    sFilter = "[LastModificationTime] > '" & Format("1/15/99 3:30pm", "ddddd h:nn AMPM") & "'"

    Not Sure if your formatting conforms with this.

    Saturday, February 26, 2011 5:12 PM
  • I just tested your DateSelection assignment and it yields (replaced your date variable with now()):

    "[Start] >= '02/26/2011' 12:00 AM AND [End] <= '02/26/2011' 11:59 PM"

     

    So you are not bracketing the time component in the single quotes.

     

    Saturday, February 26, 2011 5:32 PM
  • YES! That's IT :-) Thank you TnTinMN!!!

    Can't believe I missed that one... arg! That happens when you are looking at the same code for too long!

    So, now it moves passed the filter, and indeed lists al 23 appointments it should list. When i try to delete the appointments, it deletes the first 12. Back to square one, what am I missing?

     Private Sub DeleteExistingAppointments(ByVal BodyStart As String)
        Try
          start = Me.dStart.Value
          eind = Me.dEnd.Value
    
          OutlookNameSpace = objOutlook.GetNamespace("mapi")
          OutlookCalendar = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
          OutlookCalendarItems = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items
    
          With OutlookCalendarItems
            .Sort("[Start]", False)
            .IncludeRecurrences = True
          End With
    
          Dim DateSelection As String = "[Start] >= '" & Format(start, "MM/dd/yyyy") & " 12:00 AM'" & " AND [End] <= '" & Format(eind, "MM/dd/yyyy") & " 11:59 PM'"
          Debug.Print(DateSelection)
    
          Dim SelectedOutlookCalendarItems As Outlook.Items
          SelectedOutlookCalendarItems = OutlookCalendarItems.Restrict(DateSelection)
          Dim i As Integer = 1
    
          For Each CalendarItem In SelectedOutlookCalendarItems
            If TypeOf CalendarItem Is Outlook.AppointmentItem Then
              If Mid(CalendarItem.Body, 1, BodyStart.Length).ToLower = BodyStart Then
                Debug.Print(i & ".")
                Debug.Print("----------------------------")
                Debug.Print(CalendarItem.start)
                Debug.Print(CalendarItem.subject)
                Debug.Print(CalendarItem.body)
                Debug.Print("----------------------------")
                'CalendarItem.Delete() <---------- Why doesn't this darn thing delete all 23?
                'TotalDeleted += 1
                i += 1
              End If
            End If
          Next
        Catch ex As Exception
          Debug.Print(ex.ToString)
        End Try
      End Sub
    

    Cheers,

    John

     

     

    Sunday, February 27, 2011 9:07 AM
  • John,

    I not sure if this will work as I do not have the Office interop library installed to test it. If a recall correctly, when working with a collection and deleting items you have to work from last to first.

    Try something like this.

    For j As Int32 = SelectedOutlookCalendarItems.Count - 1 To 0 Step -1
      If TypeOf SelectedOutlookCalendarItems.Item(j) Is Outlook.AppointmentItem Then
          SelectedOutlookCalendarItems.Item(j).Delete()
          TotalDeleted += 1
       End If
    Next j

     

     

    • Marked as answer by John Bravo Monday, February 28, 2011 9:08 AM
    Sunday, February 27, 2011 3:46 PM
  • Dear TnTinMN,

    You -my man- ....  "ROCK!", thanks to you I found what the error is and was able to fix it. There is a bug with the:

    OutlookCalendarItems.Restrict(DateSelection).Count

    This returns the count of -all- items (!) Not the count of the selected items. The second bug is in the approach I took earlier of looping through the items in the collection. The item.delete() for some reason works untill nr 12. then just stops working. If you add the index of the item, the delete -does work!

    So here is the final working code, with a big thanks to TnTinMN for pointing me in the right direction.

    Private Sub DeleteExistingAppointments(ByVal BodyStart As String)
        Try
          start = Me.dStart.Value
          eind = Me.dEnd.Value
    
          OutlookNameSpace = objOutlook.GetNamespace("mapi")
          OutlookCalendar = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
          OutlookCalendarItems = OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).Items
    
          With OutlookCalendarItems
            .Sort("[Start]", False)
            .IncludeRecurrences = True
          End With
    
          Dim DateSelection As String = "[Start] >= '" & Format(start, "MM/dd/yyyy") & " 12:00 AM'" & " AND [End] <= '" & Format(eind, "MM/dd/yyyy") & " 11:59 PM'"
          Debug.Print(DateSelection)
    
          Dim SelectedOutlookCalendarItems As Outlook.Items
          SelectedOutlookCalendarItems = OutlookCalendarItems.Restrict(DateSelection)
          
          Dim CalendarItemCount As Integer = 1
          'This is to fix the bug with OutlookCalendarItems.Restrict(DateSelection).Count
          For Each CalendarItem In SelectedOutlookCalendarItems
            CalendarItemCount += 1
          Next
    
          Debug.Print(OutlookCalendarItems.Restrict(DateSelection).Count)
    
          For j As Int32 = CalendarItemCount - 1 To 1 Step -1
            If TypeOf SelectedOutlookCalendarItems.Item(j) Is Outlook.AppointmentItem Then
              If Mid(SelectedOutlookCalendarItems.Item(j).Body, 1, BodyStart.Length).ToLower = BodyStart Then
                Debug.Print(TotalDeleted)
                Debug.Print("----------------------------")
                Debug.Print(SelectedOutlookCalendarItems.Item(j).start)
                Debug.Print(SelectedOutlookCalendarItems.Item(j).subject)
                Debug.Print(SelectedOutlookCalendarItems.Item(j).body)
                Debug.Print("----------------------------")
    
                SelectedOutlookCalendarItems.Item(j).Delete()
                TotalDeleted += 1
              End If
            End If
          Next j
        Catch ex As Exception
          Debug.Print(ex.ToString)
        End Try
      End Sub
    

    Cheers,

    John

    Monday, February 28, 2011 8:58 AM