# [SOLVED] Script to Delete Outlook Calendar Entries



## fong21051

Hi,

Hope someone can help me with this one.

I got an example script from the internet that I had modified below.

I modified the script to simply add outlook calendar entries in outlook calendar for 25th/26th December 2010 as "Christmas Day" and "Boxing Day".
This script is working and I will add more dates to this script later.

But would some please kindly help modify this script so that it will search and find these 2 calendar entries from outlook calendar and delete them?

I want to apply the script at user login. 
The reason I need this is just in case some of my users have corrupted roaming profile and the script will apply twice! Hence, why I need a script to remove the duplicates!

I've heard you have to use objEvents.Delete and use the MAPIFolder.Items.Find or Restrict method to locate the item(s), then delete them but I dont know how to do this. Any help is much appreciated!

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

Const olFolderCalendar = 9
Const olAppointmentItem = 1
Const olOutOfOffice = 3

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objCalendar = objNamespace.GetDefaultFolder(olFolderCalendar) 

Set objDictionary = CreateObject("Scripting.Dictionary")

objDictionary.Add "December 25, 2010", "Christmas Day"
objDictionary.Add "December 26, 2010", "Boxing Day" 


colKeys = objDictionary.Keys

For Each strKey in colKeys
dtmHolidayDate = strKey
strHolidayName = objDictionary.Item(strKey)

Set objHoliday = objOutlook.CreateItem(olAppointmentItem) 
objHoliday.Subject = strHolidayName
objHoliday.Start = dtmHolidayDate & " 9:00 AM"
objHoliday.End = dtmHolidayDate & " 10:00 AM"
objHoliday.AllDayEvent = True
objHoliday.ReminderSet = False
objHoliday.BusyStatus = olOutOfOffice
objHoliday.Save
Next

--------------------------------------------------------------------
Any help much appreciated.
Many thanks!


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

Does anyone know how to do this? Please help!


----------



## Ninjaboi

*Re: Script to Delete Outlook Calendar Entries*

[url]http://www.pcreview.co.uk/forums/thread-2964930.php[/URL]

That link might help. Is this Javascript? Specifying the scripting language would help. If it is Javascript, which it looks like it is, please tell me so I can help you better ( and if that link is no good to you).


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

Hi, thank you very much for your reply. I have checked out the link you provided but it doesn't tell me where to put the code or how to use it.

I think the scripting language is: Microsoft Outlook VBA Language

I had got the example script I modified from this link: Office Space: Adding a List of Holidays to Microsoft Outlook

Any ideas how to resolve this? Again thanks for getting back to me!


----------



## Ninjaboi

*Re: Script to Delete Outlook Calendar Entries*

I don't know that language at all, but I found a good resource to learn it.

[url]http://books.google.com/books?id=Jp8qWFtiSl0C&pg=PA347&lpg=PA347&dq=Microsoft+Outlook+VBA+Language+delete+calendar+entries&source=bl&ots=hdB979GFbi&sig=ZUyrNUUtkDPgsZmV8wwm90-6Ug4&hl=en&ei=9kgsTcaHN8L78Ab7p_SpCQ&sa=X&oi=book_result&ct=result&resnum=8&ved=0CEgQ6AEwBw#v=onepage&q&f=false[/URL]

Also I do hope someone here on the forums can help you. I don't see a lot of programmers on the forums, and the few that are on can't know every programming and scripting language out there. So, you might want to try:

[url]http://forum.codecall.net/forum.php[/URL]

They are specifically for programming, as this forum is a general forum. Also, the programming section of this forum is kind of dead lol. They should be more than able to help you though. Sorry I wasn't much help.


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

Hi Ninjaboi,

Thanks very much for your efforts and for doing research for me. I have also posted in the forum you had provided.

Does anyone from here have any ideas?

Again, much appreciated for your time Ninjaboi.


----------



## Ninjaboi

*Re: Script to Delete Outlook Calendar Entries*

No problem.


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

Hi again, I can't seem to get any help from the other forum. 

Just wondering, does anyone know how to add "If and Else" statement to this VBA script?
Cuz I plan to add this script as a logon scrip. I don't want it to accidentally apply twice to any of my colleagues profile (if their profile is corrupt). 

Would it be possible to use "if" statement to detect if the calendar entries exist and if it does, to skip to the end so it doesn't run the script again and create duplicate calendar entries?

Would anyone kindly help out? I am useless with VBA programming!
Many thanks for any help once again!


----------



## Ninjaboi

*Re: Script to Delete Outlook Calendar Entries*

Wow, the forum I gave you was the only one I would of recommended for actual programming lol. If they are not helping, then this is bad . Though this is scripting, and not many people on the forum work with scripting. As for if you can use if and else, I'd assume so. I'm sure that a scripting language would have those built-in. I'm praying someone with more hands-on experience with VBA will be rolling in to assist you. I know we have a few Microsoft Assistants around here, they are sure to have at least something to aid you. I'll message one that I believe will help you and they should reply to this thread for you. Again, sorry I was of no direct help to you. We can't all know it all, and so we need each other to fill the others gaps . Though I know you knew that already.


----------



## TheOutcaste

*Re: Script to Delete Outlook Calendar Entries*

Actually I believe this is just VBScript, I'm guessing you are saving it with a .vbs extension, rather than entering it into the Visual Basic editor inside Outlook.

Give this a try. I added a function that checks to see if an appointment with the same name (case insensitive) exists, it then checks to see if it's the same start date (does not check time).
If it already exists, the function returns a True value, and the appointment is not added.
(I added a few holidays for testing)


Code:


Const olFolderCalendar = 9
Const olAppointmentItem = 1
Const olOutOfOffice = 3

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objCalendar = objNamespace.GetDefaultFolder(olFolderCalendar) 
Set objApptItems = objCalendar.Items

objApptItems.IncludeRecurrences = True
objApptItems.Sort "[Start]"

'' List Appointments to add
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "November 24, 2010", "Thanksgiving"    
objDictionary.Add "November 25, 2010", "Thanksgiving"    
objDictionary.Add "December 25, 2010", "Christmas Day"
objDictionary.Add "December 26, 2010", "Boxing Day"
objDictionary.Add "November 24, 2011", "Thanksgiving"    
objDictionary.Add "November 25, 2011", "Thanksgiving"    
objDictionary.Add "December 25, 2011", "Christmas Day"
objDictionary.Add "December 26, 2011", "Boxing Day"

colKeys = objDictionary.Keys

For Each strKey in colKeys
  dtmHolidayDate = strKey
  strHolidayName = objDictionary.Item(strKey)
  '' Check if it already is on the Calendar
  Return = SearchAppts(strHolidayName, FormatDateTime(dtmHolidayDate, vbShortDate))
  If Return = False Then 
    Set objHoliday = objOutlook.CreateItem(olAppointmentItem)  
    objHoliday.Subject = strHolidayName
    objHoliday.Start = dtmHolidayDate & " 9:00 AM"
    objHoliday.End = dtmHolidayDate & " 10:00 AM"
    objHoliday.AllDayEvent = True
    objHoliday.ReminderSet = False
    objHoliday.BusyStatus = olOutOfOffice
    objHoliday.Save
  End If
Next

'' Search Function
Function SearchAppts(ByVal strName, strDate)
  SearchAppts = False
  Set objAppointment = objApptItems.GetFirst
  While TypeName(objAppointment) <> "Nothing"
    If TypeName(objAppointment) = "AppointmentItem" then
      If StrComp(objAppointment, strName,1) = 0 Then
        If DateDiff("D", objAppointment.Start, strDate) = 0 Then 
          SearchAppts = True
          Exit Function
        End If  
      End If  
    End If
    Set objAppointment = objApptItems.GetNext
  Wend
End Function

I only tested this on XP running Outlook 2003, but should work for Vista/Win 7 and Outlook 2007/2010

This should remove all duplicate appointments, same criteria, identical name and same start date.


Code:


Const olFolderCalendar = 9

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objCalendar = objNamespace.GetDefaultFolder(olFolderCalendar) 

Do
  Return = DelDupAppts()
Loop While Return = 1

''''''''''''''''''''''''''''''''''''''''''''''''
Function DelDupAppts()
  '' Deletes all duplicate appointments. Duplicates are determined by case insensitve match of name
  '' and the same start date. Start time is not checked.
  DelDupAppts = 0
  Set objApptItems = objCalendar.Items
  Count = objApptItems.Count
  objApptItems.IncludeRecurrences = True
  objApptItems.Sort "[Start]"
  Current = 1
  While Current < Count
    Set strName = objApptItems.Item(Current)
    For x = Current + 1 to Count
      Set objAppointment = objApptItems.Item(x)
      If TypeName(objAppointment) = "AppointmentItem" then
        If StrComp(objAppointment, strName,1) = 0 Then
          If DateDiff("D", objAppointment.Start, strName.Start) = 0 Then
            objAppointment.Delete
            DelDupAppts = 1
            Exit Function
          End If  
        End If  
      End If
    Next
  Current = Current + 1
  Wend
End Function


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

OMG, mr TheOutcaste thank you so much!!! I was waiting for ages for ANY help and you helped write out the script! 

Much appreciated to you and ALSO to Ninjaboi for caring about my problem emailing/researching the problem.

I am optimistic this will work  but will test this tomorrow. Thank you two very much for spending your time and effort to help with my issue.


----------



## TheOutcaste

*Re: Script to Delete Outlook Calendar Entries*

You're Welcome!

Let us know how it goes. I've never done much with scripting Outlook/Office, so it's all a bit new to me, so thanks for giving me something to learn.


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

BRILLIANT, it seems to be working. I've just renamed your first script as .vbs and ran it with outlook closed and then when I open outlook... THE CALENDAR ENTRIES ARE THERE!!!
I attempted to run your first script several times and it wouldn't let me. It seems it will only run once and that is exactly what I wanted.

I will keep the second script handy just in case. 
I think its done, I don't think I need to get back to you guys for further help. 
Many thanks for your time and effort guys!
Fong


----------



## TheOutcaste

*Re: Script to Delete Outlook Calendar Entries*

You're Welcome!

I'll mark this Solved then. For future reference, you can mark your own threads *Solved* by using the *Thread Tools* at the Top Right of this thread (above the first post)

Jerry.


----------



## fong21051

Hi Ninjaboi and TheOutcaste,

I do apologise but think I spoke to soon... I had tested this at home and it worked fine, I just run your "first script" and only had to re-open outlook and the calendar entries were there.

However, when I booted up my work PC and logged in this morning and ran your "first script", it will ask me to choose my outlook profile and then I click OK. Nothing happens. Then When I re-open outlook, the calendar entries are not there! I was like AHHHHHHHHH.

I've tested this few times even after restarting my work PC but to no avail.

Sometimes I get the script error: "the remote server machine does not exist or is unavailable: 'objApptItems.GetNext'

The example script that I provided in my first post works fine. But none of your script works. I dont see whats the difference between my home PC and work PC other than roaming profile at work.

Is there an easier way to modify the script in my first post to use "if else statement" to detect if the calendar entries are already there, and if so, end the script? Or does it do that already?

Sorry to keep bothering you on this, I just hope I can get this solved. Any ideas? Thanks again!!


----------



## TheOutcaste

*Re: Script to Delete Outlook Calendar Entries*

Well if you get an error, the script may exit without adding anything, but if it completes, it should add the appointments, unless they already exist in that profile. Why they aren't appearing I don't know.

I created a 2nd profile, and it works fine with either profile. Don't have roaming profiles set up, so that would be the remaining difference. Only real difference is the first script just adds the appointments, it doesn't have to read them first.

You can add some Wscript.Echo statements to list each appointment as it's testing.

Have you tried starting Outlook first, then running the script? It will work while Outlook is running.

Give this one a try, it will list each appointment it's checking, and the appointment it is comparing to.
Be sure to run if in a Command Prompt using *cscript* (*cscript filename.vbs*), or you'll have to click OK for each echo statement.


Code:


Const olFolderCalendar = 9
Const olAppointmentItem = 1
Const olOutOfOffice = 3

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objCalendar = objNamespace.GetDefaultFolder(olFolderCalendar) 
Set objApptItems = objCalendar.Items


objApptItems.IncludeRecurrences = True
objApptItems.Sort "[Start]"

'' List Appointments to add
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "November 24, 2010", "Thanksgiving"    
objDictionary.Add "November 25, 2010", "Thanksgiving"    
objDictionary.Add "December 25, 2010", "Christmas Day"
objDictionary.Add "December 26, 2010", "Boxing Day"
objDictionary.Add "November 24, 2011", "Thanksgiving"    
objDictionary.Add "November 25, 2011", "Thanksgiving"    
objDictionary.Add "December 25, 2011", "Christmas Day"
objDictionary.Add "December 26, 2011", "Boxing Day"

colKeys = objDictionary.Keys

For Each strKey in colKeys
  dtmHolidayDate = strKey
  strHolidayName = objDictionary.Item(strKey)
  '' Check if it already is on the Calendar
  Return = SearchAppts(strHolidayName, FormatDateTime(dtmHolidayDate, vbShortDate))
  If Return = False Then 
    Set objHoliday = objOutlook.CreateItem(olAppointmentItem)  
    objHoliday.Subject = strHolidayName
    objHoliday.Start = dtmHolidayDate & " 9:00 AM"
    objHoliday.End = dtmHolidayDate & " 10:00 AM"
    objHoliday.AllDayEvent = True
    objHoliday.ReminderSet = False
    objHoliday.BusyStatus = olOutOfOffice
    objHoliday.Save
  End If
Next

'' Search Function
Function SearchAppts(ByVal strName, strDate)
  Wscript.Echo vbCRLF & "Checking if " & strName & " Dated " & strDate & " Exists"
  SearchAppts = False
  Set objAppointment = objApptItems.GetFirst
  Wscript.Echo " Comparing " & TypeName(objAppointment) & " named " & objAppointment & " Dated " & objAppointment.Start
  While TypeName(objAppointment) <> "Nothing"
    If TypeName(objAppointment) = "AppointmentItem" then
      If StrComp(objAppointment, strName,1) = 0 Then
        Wscript.Echo " Names match, comparing Dates: Existing is " & objAppointment.Start & " New Appt Date is " & strDate
        Wscript.Echo " Difference in dates is " & DateDiff("D", objAppointment.Start, strDate)
        If DateDiff("D", objAppointment.Start, strDate) = 0 Then 
          SearchAppts = True
          Exit Function
        End If  
      End If  
    End If
    Set objAppointment = objApptItems.GetNext
  Wend
End Function


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

Thank you again


----------



## fong21051

*Re: Script to Delete Outlook Calendar Entries*

OK guys, would like to say thanks very much for all your help and effort. Cheers guys!! Fong


----------

