Click here to Skip to main content
Click here to Skip to main content

Import and Export Outlook Appointments (using JavaScript)

By , 24 Sep 2008
 

Screenshot - OutlookDemo2.png

Introduction

This article illustrates an example of how to import and export appointments from an Outlook application installed on the client machine and a web-application on the server. The web application being hosted on the server, extraction of appointments can not be done on the server-side as every other user has his/her own list of appointments stored in their Outlook.

Background

I was working on a project that had a requirement to import and export appointments from an Outlook application: saving appointments on the client machine as per the client's choice, and porting appointments from Outlook appointments into the web-application. I tried to find help on the Internet for the same, but failed to find any article written on it.

Using the code

Since we need to extract the appointments on the client-side, we will be using an ActiveX object. Thus, we need to enable the setting to allow ActiveX scripts, as shown below (found under Security Settings of Internet Options):

Screenshot - OutlookDemo1.png

Once an Outlook application is installed on the system and the security setting is done as per above, we are ready to import/export appointments.

// create outlook object
var objOutlook = new ActiveXObject( "Outlook.Application" );

In the case of exporting: for the different items in Outlook, there are fixed item numbers, like:

var olAppointmentItem = 1; //fixed for different properties of outlook object
var objAppointment = objOutlook.CreateItem(olAppointmentItem);

Now, the properties of this object can be set as per required and then saved.

objAppointment.Subject = "Appointment exported to Outlook from a web application"; 
..... 
objAppointment.Duration = duration; 
objAppointment.Save();

In the case of import: again, for the different items in Outlook, there are fixed item numbers, like:

var outlookFolderCalendar = 9;

We will get all the appointments after creating the MAPI namespace, and give the default folder name.

var objNameSpace = objOutlook.GetNamespace("MAPI");
var outlookFolder = objNameSpace.GetDefaultFolder(outlookFolderCalendar);
var myOutlookItems = outlookFolder.Items;

For example, I have to show the first appointment in the calendar. The different properties can be extracted like:

var dateOfAppointment = myOutlookItems(1).Start;
.....
var bodyOfAppointment = myOutlookItems(1).Body;

The extracted values of each appointment can now be used as per your needs. One thing to note is while extracting appointments from Outlook, we get a prompt like shown below:

Screenshot - Outlookdemo3.png

We need to allow access in order to get the appointments from Outlook.

Points of Interest

Since I was using an ActiveX object, it was difficult getting the different property names required, as ActiveX objects cannot be debugged for quick-watch. It was fun and irritating at the same time to find all the properties required. Not to forget the case-sensitive nature of JavaScript!

History

First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Member
From Bangalore, India.
My blog: http://smewara.wordpress.com/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDoes not shows the outputmembergraceselvia8 Jan '13 - 4:10 
Hi,
 
This line does not work for me var objOutlook = new ActiveXObject( "outlook.application" );
 
I enabled ActiveX in my browsers and it also popped up a message saying "A website wants to open web content using this program on your computer"
But after i click allow the same old pop up comes "Outlook needs to be installed on the machine for data to export."
I m using outlook 2007..
QuestionBroser compatiblitymemberSyedur Rahaman15 Nov '12 - 6:58 
Is the code run to all browsers?
Questionkdjshfkdsmembersiphosmall15 Jan '12 - 20:18 
a,sdlfkjklanf
QuestionFetch all the appointmentsmemberserafen11 Mar '11 - 14:28 
Hey man, GREAT work! =)
I'm trying to make it fetch ALL my appointments, as it is only printing the first one.. but no luck yet!
 
something like this perhaps? :
for( var i=1; i<=colCalendar.count;i++)
var v = colCalendar.item(i);
document.write(v["+subjectOfAppointment+"");  
 
Kinda trying to loop it until all appointments have been printed.
but that doesnt work.. Any help would be greatly appreciated. Smile | :)
-Serafen
AnswerRe: Fetch all the appointmentsmvpSandeep Mewara11 Mar '11 - 20:12 
Your code does not look right.
 
Try:
var objNameSpace = objOutlook.GetNamespace("MAPI");
var outlookFolder = objNameSpace.GetDefaultFolder(outlookFolderCalendar);
var myOutlookItems = outlookFolder.Items;
 
// Check it's count of length of object myOutlookItems
for( var i=1; i<=myOutlookItems.count;i++)
{
  var v = myOutlookItems(i).Subject;
  // One by one appointment subjects would be printed now
  document.write(v);  
}

GeneralRe: Fetch all the appointments [modified]memberserafen12 Mar '11 - 2:44 
that is much closer to what im going for, thank you so much it printed out the subject of all the appointments Smile | :)
if i can trouble you a bit further.. cause the scripts function for me will be to populate a form with values from the calendar appointments.
I'm "learning by doing" so my understanding of this function is kinda limited.
Ive got a similar function for fetching contacts and that script defines
for( var i=1; i<=colContacts.count;i++)
{
 var v = colContacts.item(i);
and then uses document write like this for example:
        document.write(v["FirstName"]+"");
This printed out all of the firstnames until there were no more contacts.
 
i continue by putting them into forms. The example underneath works but it only fetches one appointment.
 
         document.write ('<form id="myForm" action="blabla.php" method="post">');
         document.write ('<label><input type="text" name="title" value="');
        document.write(""+subjectOfAppointment+"");
         document.write('" />- <input type="text" name="description" value="');
        document.write(""+bodyOfAppointment+"");
         document.write('" /> - <input type="text" name="stamp" value="');
        document.write(""+timeOfAppointment+"");
         document.write('" /></label><br />');
document.write('<input type="submit" method="post" value="add events">');
         document.write ('</form>');
 
How do i make
document.write(""+subjectOfAppointment+"");
write all of the appointment-subjects just like this one does
(v["FirstName"]+""); 
It has to be wrapped in v or something like that?
i tried this and it prints "undefined" 4 times (i have 4 appointments in the calendar)
  document.write(v["subjectOfAppointment"]+ ""); 

And again, thank you so much for your time!
-serafen
 
*Modified : did some braining and your code worked! thank you a whole lot =)*

modified on Saturday, March 12, 2011 4:25 PM

GeneralMy vote of 1memberAshish Tyagi 4015 Jan '11 - 9:28 
use less
QuestionExtra Work?memberShaun Baines17 Dec '10 - 13:58 
Hey,
 
Would you be interested in expanding this code to cover contacts, and additional folders other than the original?
 
I would be happy to pay for it.
 
Regards,
Shaun
QuestionOutLook 2007 Calendar Type Controlmemberlatell15 Oct '09 - 23:05 
HI!
thanks for the help. Smile | :)
 
is there any way to create a recurrencing appointment
with LUNAR Calendar date?
 
i mean by OutLook addin or c#, or other programs...
Questionnot working in Safari & Mozilamemberamit72127 Apr '09 - 18:52 
<b>this is very nice code,
but only limited to IE
it is not working in MOZAILA & SAFARI & CHORME....
 
because... ActiveX only support by IE
 
Any Idea How we can run this code in SAFARI & CHROME??</b>

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 Sep 2008
Article Copyright 2007 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid