Wednesday, July 8, 2009

Export Data from outlook Calander to Application through javascript

function ImportAppoinments()
{
try
{
if((window.ActiveXObject))
{ // create outlook object
var objOutlook = new ActiveXObject( "Outlook.Application" );
var olAppointmentItem = 1; //fixed for different properties of outlook object

var objAppointment = objOutlook.CreateItem(olAppointmentItem);
var outlookFolderCalendar = 9;
var objNameSpace = objOutlook.GetNamespace('MAPI');
var outlookFolder = objNameSpace.GetDefaultFolder(outlookFolderCalendar);
var myOutlookItems = outlookFolder.Items;
if(myOutlookItems.count>0)
{
for(var x=1;x<=myOutlookItems.count;x++)
{
var dateOfAppointment = myOutlookItems(x).Start;
var bodyOfAppointment = myOutlookItems(x).Body;
alert(dateOfAppointment+'\n'+bodyOfAppointment);
}

}
else
{
alert('No Events');
}
return false;
}
}
catch(e)
{
alert("If MS Outlook is already there\nplease try the following steps:\nClick on Tools (Menu)=>Internet options=>\n=>Security (Tab)=>Internet=>Custom level=>\nChoose 'Enable' from 'initialize and script Activex controls not marked as safe'");
return false;
}
}

Import data from application to Outlook Calender through Javascript

// JScript File
var formattedDate;
var olAppointmentItem = 1; //fixed for different properties of outlook object
//EXPORT to OUTLOOK
function ExportDataToOutlook()
{
var duration = 120; //number of minutes (duration in Outlook being in minutes)
SetTimeForAppointment();//time of appoinment was fixed
try
{
var objOutlook = new ActiveXObject( "Outlook.Application" );
}
catch(e)
{
alert("Outlook needs to be installed on the machine for data to export.");
return false;
}
var objAppointment = objOutlook.CreateItem(olAppointmentItem);
objAppointment.Subject = "Appointment exported to Outlook from a web application";
objAppointment.Body = "This is an appointment being exported into Outlook using a web-application.";
objAppointment.Start = formattedDate;
objAppointment.Duration = duration;
objAppointment.ReminderSet = false;
objAppointment.Save();
alert("An appointment was exported successfully to your Outlook calendar in todays date.");
return true;
}
function SetTimeForAppointment()
{
var currentDate = new Date();
var month = currentDate.getMonth();
var day = currentDate.getDate();
var year = currentDate.getFullYear();
formattedDate = (month+1) + "/" + day + "/" + year + " 08:00AM";
}