none
Delete all Calendaritems with a specific Category (VBScript)

    Question

  • Hello everyone,

    I have a little problem here and I hope you can see, where I am thinking wrong.

    I try to delete all Items from the default Outlook Calendar which contain the Category-String "Feiertag".

    To do this I used the following code:

    Const olFolderCalendar = 9
    Dim olApp
    Dim olCalFld
    Dim olNS
    Dim olAppt
    Dim found
     
    On Error Resume Next
    Set olApp = CreateObject("Outlook.Application")
    Set olNS = olApp.GetNamespace("MAPI")
    Set olCalFld = olNS.GetDefaultFolder(olFolderCalendar)
     
    	For Each item In olCalFld.Items 
    			If (item.Categories = "Feiertag") then
    				item.delete
    			End If 
    			If Err.Number <> 0 Then
    				Err.Clear
    			End If 
    	Next    
     

    The strange thing is, that this code deletes Items with the specific category from the calendar. But only the half of the items. If I run the script again it deletes the half of the rest and so on until I am left with one item of this Category and then that one is deleted.

    I tested this on Outlook 2010 (32Bit) and Windows 7 (64Bit)

    Where did I go wrong to delete all Items at once?

    I hope someone has an idea.

    Regards, Alexander


    • Edited by Betosai Friday, January 11, 2013 4:44 PM Additional Information
    Friday, January 11, 2013 4:43 PM

Answers

  • Firstly, your code changes the collection as it iterates. Loop from Count down to 1:

    sey Items = olCalFld.Item
    For i = Items.Count to 1 step -1
    ...

    Secondly, Categories property returns a ";" separated list of categories. If an item has multiple categories, your code will fail to detect a matching item.

    And most importantly, do not just loop through all items in a folder. Use Items.Find/FindNext or Items.Restrict.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, January 11, 2013 5:05 PM

All replies

  • Firstly, your code changes the collection as it iterates. Loop from Count down to 1:

    sey Items = olCalFld.Item
    For i = Items.Count to 1 step -1
    ...

    Secondly, Categories property returns a ";" separated list of categories. If an item has multiple categories, your code will fail to detect a matching item.

    And most importantly, do not just loop through all items in a folder. Use Items.Find/FindNext or Items.Restrict.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, January 11, 2013 5:05 PM
  • Hello Dmitry,

    Thank you for your help. I was aware about the problem of the "Categories" Containing multiple entries, but I wanted to make it run for one entry first ^^

    The change in the collection you pointed out was the trick. It is rather obvious looking at it now, but I think I would had stared for ours on the code without realizing it. With your For-Loop it worked.

    Just in Case anyone reads this with a similar task. the String which contains the Categories (Items.Categories) has blank spaces in front of each entry beyond the first, in addition to the ";".

    It took me hours to find out, why my string comparision didn't work.

    Again thank you Dmitry,

    Regards, Alex

    Monday, January 14, 2013 5:56 PM