I keep all my email and calendar items on our Exchange server at work, and my work laptop and home PC both run Outlook against that server. Both instances of Outlook also sync up with my mobile phone using Nokia PC Suite.
When it works, it really works and I can see all my email and calendar items wherever I may be. But sometimes one Outlook can’t tell that an item that has been put on the phone by the other Outlook is the same item it should already know about and creates a duplicate when it syncs with the phone.
So I have created a very simple C# program to identify duplicate calendar items and delete them. To do this, I loop through every item in the calendar, and write the subject, location, start and end date into a hashtable as the key. If I come across the same combination of subject, location and times again, I delete the item instead. Easy.
Here is the code. You must have the Office Interop assemblies installed, and Microsoft Outlook Library referenced. You can install the Office Interop Assemblies separately if you are not able to modify your installation of Office.
using System;
using System.Collections;
using System.Diagnostics;
using Microsoft.Office.Interop.Outlook;
namespace RemoveOutlookCalendarDuplicates
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
Application olApp = new ApplicationClass();
NameSpace outlookNS =
olApp.GetNamespace("MAPI");
MAPIFolder calendarFolder =
outlookNS.GetDefaultFolder(
OlDefaultFolders.olFolderCalendar);
while(true)
{
bool somethingDeleted = false;
Hashtable uniqueItems = new Hashtable();
foreach(AppointmentItem oi in calendarFolder.Items)
{
string uniqueKey =
String.Format("{0} {1} {2:yyyyMMddhhmmss} {3:yyyyMMddhhmmss}",
oi.Subject, oi.Location, oi.Start, oi.End);
if(uniqueItems.ContainsKey(uniqueKey))
{
Console.WriteLine("DELETED: " + uniqueKey);
oi.Delete();
somethingDeleted = true;
}
else
{
uniqueItems.Add(uniqueKey, String.Empty);
}
}
if(!somethingDeleted) break;
}
Console.WriteLine("** PRESS RETURN **");
Console.ReadLine();
}
}
}
Recent Comments