CDO 1.21 User Options Best Practices

I worked a case this weekend where a product used to synchronize calendars between Exchange 5.5 and Exchange 2003 was setting CDO's user options to GMT and then not setting it back.  This of course would have no affect on the user if they were in GMT but it was causing CDO to render Calendar items in GMT time instead of the local time of the user, in this case Eastern.

To avoid this problem in the future, CDO applications should always set the user options back to their original values before ending the application.  To me this just makes logical sense, but hey what do I know.  Here is some sample code to add to your CDO applications if you are doing this:

    Dim objSession
    Dim tmzUser As Long
    Const tmzGMT = &H1
    Set objSession = CreateObject("MAPI.Session")
    ' Get the user's current timezone
    tmzUser = objSession.GetOption("TimeZone")
    ' Set the timezone to whatever you would like in this
    ' case we want to use GMT
    objSession.SetOption "TimeZone", tmzGMT
    ' Do something
    ' Now set the TimeZone back what it was
    objSession.SetOption "TimeZone", tmzUser
    Set objSession = Nothing

Comments (1)

  1. Something else to consider – if you’re using CDO, are going to be reading times, and expect the times to be in a certain timezone, then go ahead and use SetOptions to ensure that timezone is selected. And of course, don’t forget to set it back when you’re done! 🙂

Skip to main content