PDA

View Full Version : Outlook Calendar Macro Question



jsticca
02-16-2011, 07:45 AM
I have my Outlook 2003 calendar synced with my google calendar via google calendar sync. This program works well except for one instance, if my calender event is a microsoft live meeting the message class class is different than a regular calendar event so it won't sync. I have a macro to convert the live meeting message classes but it will only work if I run it when I am in calendar in Outlook and manually click run. Below is the code:


Sub Calender_Fix()
' Change the following line to your new Message Class
NewMC = "IPM.Appointment"
Set CurFolder = Application.ActiveExplorer.CurrentFolder
Set AllItems = CurFolder.Items
NumItems = CurFolder.Items.Count
' Loop through all of the items in the folder
For i = 1 To NumItems
Set CurItem = AllItems.Item(i)
' Test to see if the Message Class needs to be changed
If CurItem.MessageClass <> NewMC Then
' Change the Message Class
CurItem.MessageClass = NewMC
' Save the changed item
CurItem.Save
End If
Next
End Sub


Ideally, I would have this macro run every time google calendar sync runs (right before). However, if this is too complicated, I would like to set it to run every hour. And if this is too hard, as a last resort, I would like to have it run every time I open/close Outlook.

Any help would be greatly appreciated. Thanks!

Zack Barresse
02-20-2011, 01:06 PM
It doesn't have to be the active explorer, just reference it, assuming you want the default calendar...

Sub Calender_Fix()
Dim NS As NameSpace
Dim CurFolder As Folder
Dim AllItems As Items
Dim i As Long
Dim NewMC As String
Dim CurItem As AppointmentItem
Dim NumItems As Long
' Change the following line to your new Message Class
NewMC = "IPM.Appointment"
Set NS = Application.GetNamespace("MAPI")
Set CurFolder = NS.GetDefaultFolder(olFolderCalendar)
Set AllItems = CurFolder.Items
NumItems = CurFolder.Items.Count
' Loop through all of the items in the folder
For i = 1 To NumItems
Set CurItem = AllItems.Item(i)
' Test to see if the Message Class needs to be changed
If CurItem.MessageClass <> NewMC Then
' Change the Message Class
CurItem.MessageClass = NewMC
' Save the changed item
CurItem.Save
End If
Next
End Sub

HTH

jsticca
02-21-2011, 11:11 AM
I get a "User-defined type not defined" with the Dim CurFolder as Folder line highlighted when I try to run this. Any idea why?

Also, I am still looking to have this macro run automatically right before google calender sync runs... or automated to run some other way.

Thanks!

Zack Barresse
02-21-2011, 03:36 PM
I'm assuming you have a reference set to the Outlook object model, right? (Tools | References... | Microsoft Outlook xx.0 Object Library, where xx.0 is your version number) You're right though, you should explicitly reference it, so it shouldn't be "Folder" but "Outlook.Folder".

As far as automating when Google Sync runs, I don't know if that's possible, since you can't hook the action (as far as I know) and there isn't a dll or api we can hook (again, as far as I know). So I think there you're SOL. Sorry. :(

jsticca
02-22-2011, 07:37 AM
Thanks again.

Is there anyway I can have this macro run automatically when Outlook starts up and then closes?

jsticca
02-22-2011, 07:57 AM
Also one more note... My reference is set to microsoft outlook 11.0 library and I'm still receiving the user-defined type not defined error. I changed Folder to Outlook.Folder too.

Charlize
02-23-2011, 02:53 AM
Or maybe use a 'withevents' setup with all the necessary things involved with it. Out of my head, a folderwatch for your default calendarfolder, a new calendaritem checker, an objectinspector, a private sub to deal with the new calendaritem and off course the declaration in the application_startup and application_quit subs.

So basically check when a new calendaritem is added. If the class isn't the one you want, change it and save the calendaritem.

This is offcourse only theory, only done it with a task folder.

Charlize