How to add a holiday to a calendar using VB and CDO 1.21.

' How to add a holiday to a calendar using VB and CDO 1.21.
' This example code shows how to add a holiday to a calendar using VB and CDO 1.21.
' Set a reference to CDO 1.21 and do the TODO sections in the code.
' Holidays are All Day Appointments with the Category of "Holiday" set.
' CDO 1.21 can be use on:
'   Exchange Server 2000
'   Exchange Server 2003
'   Exchange Server 2007 if CDO 1.21 is installed
'   Outlook 2002 if CDO 1.21 is installed (from Outlook CD)
'   Outlook 2003 if CDO 1.21 is installed (from Outlook CD)
'   Outlook 2007 if CDO 1.21 is installed (from Exchange 2007 CDO download)
' If CDO 1.21 is used on an Exchange Server, then it needs to run on an
' account with permission to each mailbox it accesses.
' When CDO 1.21 is used on an Outlook client, the code is usually accessing the mailbox
' of the user who is currently logged-in. If the code needs to access a different mailbox,
' then permission must be granted to that user for that mailbox.

Dim oSession As MAPI.Session
Dim oFolder As MAPI.Folder
Dim oMsgs As MAPI.Messages
Dim oAppt As MAPI.AppointmentItem
Dim i As Integer
Dim sServer As String
Dim sUser As String

Set oSession = New MAPI.Session
sServer = "smarthost" 'TODO: Change this to the name of your server.
sUser = "somebody" 'TODO: Change this to the name of an email recipient

' Set the localid and codepage - this needs to be set BEFORE calling Logon (only needed if you are
' not creating this in English
'oSession.SetLocaleIDs CLng(1041), CLng(932) ' Japanese
oSession.Logon , , False, True, , , sServer & vbLf & sUser

''oSession.SetOption "TimeZone", 10 ' Use if the times are off.  10 is EST.
'   'See:  Setting timezone options with CDO 1.21.

' Get Calendar folder
Set oFolder = oSession.GetDefaultFolder(CdoDefaultFolderCalendar)


' Create a new Appointment
Set oMsgs = oFolder.Messages
Set oAppt = oMsgs.Add

Dim sCategories(10) As String
sCategories(0) = "Holiday"

' Set up properties on the appointment
With oAppt
    .Subject = "My Own Holiday"
    .Location = "Test here"
    .StartTime = #7/1/2004# ' DateAdd("h", 12, Date) ' Holiday is 7/1/2004.
    .EndTime = #7/2/2004# ' DateAdd("h", 13, Date) ' Holiday ends at to the start of this time/day
    .AllDayEvent = True
    .Text = "This is my test holiday"
    .BusyStatus = 0
    .ConversationTopic = "Personal Holiday"
    '.ReminderSet = True
    '.ReminderMinutesBeforeStart = 15
    .Categories = sCategories
    .Encrypted = True
End With

' Call Update to save the appointment to the calendar
oAppt.Update (True)


' Clean Up
Set oMsgs = Nothing
Set oAppt = Nothing
Set oFolder = Nothing
Set oSession = Nothing

For information on how to do this with Outlook, please refer to the following:

  Outlook 2007

  Outlook 2003

  Outlook 2002



Comments (9)

  1. sjg says:

    This is good, but can you make a blog post which shows how to create and modify Categories?  That would be a great post considering EWS does not allow any way to accomplish this.

  2. Max O'Brien says:

    Hi Daniel, this is really useful.

    Could you give me some suggestion as I got trouble in MAPI.

    we got a C# project using MAPI, cdo1.21.

    We need to get mails from exchange server, and when there were Chinese characters in the mail, all the Chinese words changed to ‘?’

    I find your article, and add

    session.SetLocaleIDs((long)2052, (long)65001);

    before session.login,

    We have installed the Chinese Language support package, But the problem still exists.

    Could you help me? My email is miker.xu"

  3. Webdav101 says:

    The question marks may be showing-up if the window you are displaying the Chinese text in is not full Unicode enabled. Many of the older development environements and code they produce will display text in double-byte format format.  Double-byte characters need to have the default language of the machine to be set to the language to be displayed.  Newer development environments and code such as that of .NET will render full unicode text, however CDO 1.21 is not supported under .NET.

  4. Max O'Brien says:

    We use vs2005 as the dev tool,

    Then I use your configuration to show Japanese characters.

    SetLocaleIDs CLng(1041), CLng(932) ‘ Japanese

    Although Japanese words can’t display, at least I can see some text changed.

    for ex: I’ve    changed to    I致e

  5. Max O'Brien says:

    besides, I’m not clearly about "CDO 1.21 is not supported under .NET."

    Our project is a .NET project, and we can receive emails expect we can not show Chinese characters in the email body

    I searched out one article about this issue,

    So could the Threading affect some part of CDO functions?

  6. Webdav101 says:

    Hello Max;

    OK, if your using Visual Studio 2005 with VB.NET, then you should be able to see the Japanese Text in a .NET window as long as you have the Japanese and/or Chinese language code pages loaded which match the language you are working with. Each language may have several code pages – Japanese has a couple and Chinese has several. I usually install all languages on my development boxes.

    I work with Steve Griffin – we are in the same Microsoft Developer Support group which covers messaging APIs such as CDO 1.21, MAPI, Exchange Web Services (EWS), CDOSYS, Outlook Object Model (OOM), etc.  CDO 1.21 and MAPI are absolutely not supported under any .NET thread – both APIs will have threading and memory issues under a .NET thread.  These APIs were written pre-.NET.

    If your going against Outlook, then consider using Outlook Object Model (OOM) through the Outlook Primary Interop Assembly (PIA).  If your going against Exchange 2000 or 2003, look at using something like WebDAV.  If your going against Exchange 2007, WebDAV is an option, however its far better to use Exchange Web Services (EWS).  EWS is the go-forward API for going against Exchange for working with mailbox and public folder content.

    Here are some links for more information:

       CDO 1.21 and Unicode Text (Japanese, Chinese, Korean, etc)

       Support policy for Microsoft Exchange APIs with the .NET Framework applications

    My team blogs with the keyword "DevMsgTeam" in thier blogs – so you can use that to see what other people on my team have written on this and any other issue you are facing.

    I hope that this information helps.



  7. Max O'Brien says:

    Thanks, Daniel

    Finally we fix this issue.

    You use below to login by using server address and user account:

    oSession.Logon , , False, True, , , sServer & vbLf & sUser

    But we use profile to login

    session.Logon(profileName, vEmpty, false, false, vEmpty, vEmpty, vEmpty);

    when we changed to use server address, it works.

    But we still do not know what’s the difference between the two ways to login.

    I think they should be the same.

    Could you tell me what’s the difference between the two ways?

  8. Webdav101 says:

    Hello Max;

    It sounds like you were running into an Ambiguous Name Resolution (ANR) issue.  If you put in the full smtp address, CDO 1.21’s underlaying code should be able to resolve to a mailbox all the time.  However if only the alias is used, then the underlaying code may incorrectly resolve.  The article below covers ANR.

    Ambiguous Name Resolution for LDAP in Windows 2000

    Because of this, its always best to use the full smtp address of the user who’s mailbox you are trying to log into.

  9. Max O'Brien says:

    Hi Daniel,

    I may not describe my problem clearly.

    We use "Profman.exe" to create a profile with the smtp address and alias, and got a profileName of this profile.

    Then we use the "profileName" to login. And there should be no problem with ANR issue.

    If we use smtp address to login, it will also create a temp profile which we can see in "Profman.exe".

    The two ways should be the same, but only latter can show utf-8 words correctly.

    So we wonder what’s the difference between the two ways.



Skip to main content