Monday, March 10, 2008

A "fix" for the barrage of Deleted Items created by Google's Outlook Calendar sync

This article was written when Google's Outlook Calendar Sync tool was first made available to the public. Since then, Google has fixed this problem in their tool. I'm leaving the post here, as the general principle of having Outlook take specific actions (that couldn't be accomplished in a Rule) when new items show up in any Outlook location is still valid.

Google's new Outlook Calendar sync is a terrific idea. Unfortunately (but not unexpectedly) it has some problems. The first thing most people notice is that Outlook Calendar items that involve anyone besides yourself (i.e., you Invited others or someone else Invited you) don't show up in Google Calendar. The FAQ is suppposed to contain a "fix" for this, but people don't seem too happy with this.

The other problem takes longer to notice. The sync tool creates TONS of Appointments on 12/31/1979 at 7:00 pm - and then deletes them. When I say "tons", I mean it. At one point, I had >12,000 items in my Deleted Items, most of which were these 12/31/1979 appointments.

I don't know how to stop the sync from making these Appointments (I assume they're used as some temporary scratchpad (or someone really messed up their code)), but I do know how to keep all those items out of my Deleted Items.

This does require that you turn on Visual Basic (VB) for Applications (VBA) macros in Outlook. This is NOT a security risk that I know of, as I think you're the only one you can add macros in this manner.

In Outlook, go to Tool -> Macro -> Visual Basic Editor. In the left-hand column, there should be a hierarchy that looks like Project1 -> Microsoft Office Outlook Objects -> ThisOutlookSession. (If that's not there, you might need to record a macro and then delete it. This should create the ThisOutlookSession item. Click on ThisOutlookSession in the left-hand column, and then paste this into the main window:

Private WithEvents DeletedItems As Items

Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set DeletedItems = objNS.GetDefaultFolder(olFolderDeletedItems).Items
Set objNS = Nothing
End Sub

Private Sub DeletedItems_ItemAdd(ByVal myItem As Object)
If myItem.Subject = "" Then
' Google's Outlook Calendar sync causes a bunch of Appointments to be create on 12/31/1979 at 7:00pm.
' After the sync, they get "deleted", but you can have THOUSANDS of these in your Deleted Items.
' This will permanently get rid of them (as well as any other items with no Subject).
myItem.Delete
End If
End Sub


BTW, the same idea can be extended to create an event handler to handle items getting added to a folder that collects all of your automated or junk email. Add a "Private WithEvents" line, and then add this to Application_Startup():
  Set AutomatedItems = objNS.GetDefaultFolder(olFolderInbox).Folders.item("Automated").Items

And then add a new handler to do something like:
Private Sub AutomatedItems_ItemAdd(ByVal myItem As Object)
myItem.UnRead = False
End Sub

21 comments:

Pro said...
This comment has been removed by a blog administrator.
Unknown said...

I tried pasting the following directly into the box to the right:

Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set DeletedItems = objNS.GetDefaultFolder(olFolderDeletedItems).Items
Set objNS = Nothing
End Sub

It doesn't seem to do anything.

Any thoughts?

Jim Tuttle said...

You need to paste both subroutines into Outlook's VBA panel. Make sure you also have the code for DeletedItems_ItemAdd() too.

You can restart Outlook, or just put your cursor inside the code for the Application_Startup() subroutine and then press "F5" to run that subroutine.

From that point on, and NEW items added to your Deleted Items will cause that code run. If it matches the criteria in the DeletedItems_ItemAdd() subroutine, it will get permanently deleted. (It will NOT remove existing Google items in your Deleted Items.)

This works for me in Outlook 2003. The same type of code has worked for me since Outlook 2000. I've never tried Outlook 2008.

I've read elsewhere that the Apppointment date isn't the same for everyone. You might need to edit DeletedItems_ItemAdd() to match your Google Appt's.

Henrik Gemal said...

Version 0.9.3.2 of Google Calendar Sync has fixed the problem

Anthony said...

Quote: "Version 0.9.3.2 of Google Calendar Sync has fixed the problem"

Not Here. Its better, but I still get 30 items turn up in my delted items after a sync.

There are no loger any blank appointments turning up though!

1, The 30 appointment all have a date of 01/01/1980.
2, The appointments do exist in Outlook (with correct dates)
3, All are reaccuring
4, Every annual reaccuring birthday with noend point appears

Anonymous said...

Dear Jim,

I am having a similar problem that doesn't involve Google (I have not downloaded or installed their sync routine with Outlook). However, I still have a very similar problem. There are 29 old Meeting Requests (from 2004-5) that keep showing up in my deleted items folder almost every time I sync with Outlook (2007, Vista, Axim PDA). In my case every one has a subject but the "To" is missing (blank). I have tried several things, the latest is If myItem.SentTo = Null Then, etc. but I keep getting an error (run-time error '438' Object doesn't support this property or method). As you will no doubt realize I know virtually nothing about VB. Could you please help me with the right code??

Many thanks for any help!!

Terry Schuck
Roswell, GA

Jim Tuttle said...

I'm at home (on my Mac), so I can't test Outlook VBA from here, but the first thing that occurs to me is your "Null". I thought VB in general used "Nothing" in the places C-like languages use "Null".

Try "Nothing". If that doesn't work, leave another comment and I'll try to recreate. Could you turn on a few more Columns in your Deleted Items folder? Are they "From" the same person, or have a similar Subject? The problem I foresee with using "To" is that most Appt's probably have To=Nothing, unless it was actually sent by someone else (I think your own personal Appt's don't use the To field?).

Let me know!

Anonymous said...

Jim,

Thanks for the response but it turns out I'm a bigger dummy than I thought. Finally realizing that the "bogus" deleted items appeared when I was syncing my Axim PDA, I looked at each item and then found it duplicated many times on my PDA. Once I deleted all of them on my PDA, the problem went a way (duh).

Terry Schuck

Jim Tuttle said...

I've never seen a foolproof sync setup between ANY calendar/PDA systems. I had to turn off 2-way syncing between Google and Outlook. My Google account ended up "owning" many of the Outlook appt's, and I could no longer send updates to them (my Outlook acct became an Invitee, not the Organizer).

jonathan said...

Do you think you could really dumb this down for us? Is there anything in the code above that I need to edit? I'm a programming newbie, but have 1000's of deleted appointments in my Outlook Deleted items folder!

Thanks,
JC

Jim Tuttle said...

@jonathan: the post now starts with a disclaimer that Google seemed to have fixed this issue. Are you syncing with Google, or with another tool? If it's the same Google problem, you shouldn't need to edit anything to get it to work, but see my comment on March 28, 2008, about restarting Outlook. It's a different issue, you'll need determine how to correctly identify them, and add that to the code.

For example, if all of the deleted items have a certain subject, edit this line:

If myItem.Subject = "" Then

If they don't have the exact same subject, but start with a common word, etc., send that to me. You need to find something in common for the script to locate and delete the items.

Unknown said...

Mr. Tuttle, your fantastic.

Problem continues: I am using Google Calendar Synch 0.9.3.5 and am having this problem too. It started when I turned on 2-way synchronization.

My subject line is different for each deleted calendar entry. It's not blank. The calendar entry date is 12/31/1979 6:00pm CT.

I believe the code posted above is for blank subject lines. Any suggestions on how to handle clearing these out when the subject line varies by entry but the date stays the same?

Jim Tuttle said...

Thank you for your kind words! I'm glad this old post can still (sort of) help people out. I'm really surprised that this problem is back, since it was fixed once. I stopped syncing my calendars, though, as I had too much Outlook calendar corruption (Outlook kept thinking my gmail account owned the appointments, and wouldn't let me make changes in Outlook).

Regarding your question, I'm sure there's a way to do it. First, see if you can even get this code of mine running for you. A quick way to check is to throw a "MsgBox()" command in somewhere.

If it's working in general, have a look at the AppointmentItem object (http://msdn.microsoft.com/en-us/library/aa210899%28office.11%29.aspx) and see if you can use one of the properties (LastModificationTime?) to help find the items that match your particular scenario. You'll have to get your hands dirty in the VBA code, but it's pretty easy to use properties of objects when you know the object and can see the documentation. Give it a try!

Anthony said...

It was never fixed for me, its been a constant problem with ywo way sync.

Francesca said...

I just tried this once & it seemed to work!! Thanks so much.

Jen Blalock said...

If google's fixed this problem it's new to me. Anyway I used the routine and it works great! I only had one appt left I could not eradicate no matter what I tried so at least it's not ending up in my Outlook deleted folder any longer.

Anonymous said...
This comment has been removed by the author.
Anonymous said...
This comment has been removed by the author.
Anonymous said...

THANK YOU FOR THIS!

**Important to note - this only works for NEW items into the deleted items folder - not existing!**

I had to modify this for the user having problems. All his subject lines were actually filled in so i had to go by start time. My version below:

Private WithEvents DeletedItems As Items

Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set DeletedItems = objNS.GetDefaultFolder(olFolderDeletedItems).Items
Set objNS = Nothing
End Sub

Private Sub DeletedItems_ItemAdd(ByVal myItem As Object)

If myItem.Start = "12/31/1979 3:00:00 PM" Then

myItem.Delete
End If
End Sub


If you want to see the result of the myItem.Subject or myItem.Start simply use this code below for testing:

Private WithEvents DeletedItems As Items

Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.GetNamespace("MAPI")
Set DeletedItems = objNS.GetDefaultFolder(olFolderDeletedItems).Items
Set objNS = Nothing
End Sub

Private Sub DeletedItems_ItemAdd(ByVal myItem As Object)
DIM strResult As String

strResult = msgbox(myItem.Start, vbokonly)
End Sub

Kevin Coffey said...

I used Adam's code above, and it hasn't done anything. The date and time are all there, but it hasn't stopped 7,000 items from being dumped into Outlook's Deleted Items.

Outlook 2010, Windows 7. I pasted the code into the Macro area, hit f5 and it should work correct?

Jim Tuttle said...

Kevin, you need to make sure your cursor is in the Application_Startup() subroutine. At least, that's how I've always primed code like this. Or, you can close and restart Outlook, and that could should be run at, wll, Application_Startup! There might be settings in Outlook that prevent any code from running, so you might need to fix those settings first. A good way to test is to stick a line like "MsgBox('hi')" in the code, and then you should be able to tell it's running.