Link to home
Start Free TrialLog in
Avatar of John Water
John WaterFlag for United States of America

asked on

Programmatically Adding Outlook Holiday Options

I need to be able to edit the default Holiday Calendar in Outlook 2k7. If at all possible, being able to actually add new available options would be great - entries for board dates for example. If they could be picked from a list like the picture attached that would be excellent.  Anyone know if this can be done programmatically, or know of any reg hacks or if this is even possible.


Region-Capture.JPG
Avatar of John Water
John Water
Flag of United States of America image

ASKER

I think I answered my own question....
http://office.microsoft.com/en-us/outlook/HA010750021033.aspx
Although MS states to proceed at your own risk - anyone have any experience doing this enterprise wide?
Hi,

Check the following code.
using Microsoft.Office.Interop.Outlook;

_Application app = null;
NameSpace mapiNS = null;

try
{
    app = new ApplicationClass();
    mapiNS = app.GetNamespace("MAPI");

    string profile = string.Empty;
    mapiNS.Logon(profile, null, null, null);

    CreateAppointment(app, "Holiday Description", DateTime.Today.AddDays(5));
                    
}
catch (System.Exception ex)
{
    throw new ApplicationException("Unable to Add Holidays to Outlook Calendar." + Environment.NewLine + "Error Message : " + Environment.NewLine + ex.Message);
}

try
{
    if (mapiNS != null)
           mapiNS.SendAndReceive(false);
}
catch (System.Exception ex)
{
   //Log Error
}


        static void CreateAppointment(_Application olApp, string strevent, DateTime dt)
        {
            _AppointmentItem apt = (_AppointmentItem)olApp.CreateItem(OlItemType.olAppointmentItem);

            // set some properties
            apt.Subject = strevent;
            apt.AllDayEvent = true;

            apt.Start = dt.Date;
            apt.End = dt.Date.AddDays(1);

            apt.BusyStatus = OlBusyStatus.olOutOfOffice;
            apt.Location = "NDS India Holidays " + dt.Year.ToString();

            apt.ReminderSet = false;

            apt.Categories = "Holidays";
            apt.Save();
        }

Open in new window

Avatar of David Lee
Hi, swfwmd2.

Yes, you can add holidays and corporate events programmatically.  You can also create an send out an additional .hol file and let everyone add the events manually (i.e. they will have to go through the menu selections to add the contents of the custom .hol file).  The code that GiftsonDJohn posted looks like it will create a single calendar entry and I'm sure it could be modified to read entries from a file and add them to the calendar.  I'm not sure how that code would be launched.  In my opinion the best solution is an automatic solution where the items are added with no action required by each user.  Here is a link to another question on the same subject that I answered.  The code in this solution is in VBScript.  That allows it to be called from a logon script making the process completely automatic.  

https://www.experts-exchange.com/questions/23358232/Automate-update-of-Outlook-holiday-file.html
Hey BlueDevil!
Thanks for your excellent information...a few questions though:
As you can see in the attachement I am getting the Subject of the meeting in the location and vice versa with the location and the subject. Any idea why? Also I know this is a holiday file we are editing, but is there a way so the event doesnt show on the calendar as a holiday, but rather as an event or meeting? And is there a way to set the time of the event? I also got the error below when running the code. Code is not my strong point so please bear with me. If it sounds like I am asking for too much please just let me know - Thanks!
Region-Capture1.JPG
Region-Capture2.JPG
No, you're not asking too much.  First thing is that you are using a Microsoft formatted .hol file.  Although we're using the same extension (.hol) for the file that goes with this script the format is different.  As noted in the original question the proper format for this file is

Event Name,Date

The header (e.g. [Agricultural Advisory Committee] 2) will cause errors. Second, using this script holidays don't have locations.  If the item in your first screen capture was added by this script, then I don't know where the location came from.  The script as it is does not fill in the location field.  Ditto for the Holiday category.  The script does not set a category.  The error in your second screen capture is saying that the script could not find the file containing the holidays to add.  Did you edit the script and change the filename in line 3 per the comment in the script?

I can modify the script to add any details you want to items added through the script.  Of course that means adding more detail to the .hol file.  For example, if you want to add a location and a category on a per event basis, then the .hol file would need to be laid out something like

Event Name,Event Location,Event Start,Event Duration,Event Category

The fields can be in any sequence you like, I just need to know what that sequence is in order to modify the script.
Here is the code I am using, line 3 does contain a location, am I doing something wrong?
Also you said "[Agricultural Advisory Committee]" will cause errors...why is that?
If its not too much trouble additional code would be excellent!
Event Name,Event Location,Event Start,Event Duration,Event End,Event Category
And just so I am on the same page as you...in my "Outlook Holidays" folder I have two files; Outlook.hol and holidays.vbs, I need to run the .vbs file which is calling on the .hol file correct?
The item from the first screen capture was achieved by just running the edited .hol file, not using the script
Once again thanks for your help!
Dim olkApp, olkCalendar, olkEvent, objFSO, objFile, arrItem, strFilename 
'Change the file name and path on the following line as needed. 
strFilename = "D:\outlook Holidays\Outlook.hol" 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile(str.outlook.hol) 
Set olkApp = CreateObject("Outlook.Application") 
Set olkCalendar = olkApp.GetNamespace("MAPI").GetDefaultFolder(9) 
Do While Not objFile.AtEndOfStream 
    arrItem = Split(objFile.ReadLine, ",") 
    Set olkEvent = olkApp.CreateItem(1) 
    olkEvent.Subject = arrItem(0) 
    olkEvent.Start = arrItem(1) 
    olkEvent.AllDayEvent = True 
    olkEvent.ReminderSet = False 
    olkEvent.save 
Loop 
Set olkEvent = Nothing 
Set olkCalendar = Nothing 
Set olkApp = Nothing 
objFile.Close 
Set objFile = Nothing 
Set objFSO = Nothing 
WScript.Echo "Holidays Added"

Open in new window

One more thought...is there a way for a user to remove the entries from their calendar once they have subscribed to them, basically an unsubscribe?
The file isn't opening because of the change on line 5.  "str.outlook.hol" isn't the name of a variable.  Looks like you did a search and replace.  That line should read

Set objFile = objFSO.OpenTextFile(strFileNamel)

Users aren't "subscribed" to an appointment.  It is simply another item on their calendar.  They can delete or change it as they see fit.
Great info....got it to work
Any more info on this part?
"I can modify the script to add any details you want to items added through the script.  Of course that means adding more detail to the .hol file.  For example, if you want to add a location and a category on a per event basis, then the .hol file would need to be laid out something like, Event Name,Event Location,Event Start,Event Duration,Event Category"
Cool.

What sort of info?  I'll be glad to modify the code to handle the additional details, I just need to know which details you want and where on the line each field of information is going to be?
Are you able to add information such as Event Name, Location, Start and End, and Category information? Not sure what you mean by saying "where on the line each field of information is going to be" thanks for the info bluedevil
Not sure what you mean by saying "where on the line each field of information is going to be"

Each line in the .hol file describes an event. In a normal .hol file the format is Event Name, Date.  For example

Christmas Day,2010/12/25

The entries in the .hol file this script will use has to describe more about each event.  Using the fields you listed the file might look like this



So the "line" of data is laid out as "Event Name, Location, Start, End, Category"

If that's the order the data will be on for each event (read line of data), then I can proceed.  I'm simply asking you what order you want the data in for each event.  Maybe you'd prefer the dates to come first so it'd be easier to line things up.  For example you might go with Start, End, Event Name, Location, Category.  Something like this

2010/12/25 2:00PM,2010/12/25 2:00PM,Office Christmas Party,Conference Room,Parties

The order is immaterial to me.  But I have to know the sequence of data items on the line.  If I coded the script to expect the start date to come first and you place the event name first, then the process fails.

Does that help?
You're the man...
Event Name, Location, Start, End, Category would work great!
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks BlueDevil!!
You're welcome.  I'm glad I could help out.
Hi BlueDevilFan... i have been trying to get the same thing done for my company... but i am getting a error in the script you posted:
Line 3, Char 35, Expected end of statement?