Adding appointment to Outlook

J

JSimpson

So I have the code figured out how to add a an appointment to outlook,
but when I click the button, I get a null reference exception in
preview mode... Here is my click event, hope you can help.

System.NullReferenceException
Object reference not set to an instance of an object.
at PTO_request.FormCode.CTRL18_11_Clicked(Object sender,
ClickedEventArgs e)
at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick
(DocActionEvent pEvent)
at
Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick
(DocActionEvent pEvent)



public void CTRL18_11_Clicked(object sender, ClickedEventArgs e)
{
//start outlook
ApplicationClass application = new ApplicationClass();

//get MAPI namespace
NameSpace nspace = application.GetNamespace("MAPI");

//logon as current user
nspace.Logon(Missing.Value, Missing.Value, false, false);

//create a new appointment
object objappointment = application.CreateItem(
OlItemType.olAppointmentItem);

// Create an XPathNavigator to walk the main data source
// of the form.
XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager NamespaceManager =
this.NamespaceManager;

// Set the fields in the form.
//xnMyForm.SelectSingleNode("/my:myFields/my:name",
ns).SetValue("alex");
//xnMyForm.SelectSingleNode("/my:myFields/my:check",
ns).SetValue("true");


//get variables
string bStartDateValue = xnMyForm.SelectSingleNode("../
my:startDate",
NamespaceManager).Value;
string bEndDateValue = xnMyForm.SelectSingleNode("../
my:endDate",
NamespaceManager).Value;
string bUserName = xnMyForm.SelectSingleNode("../
my:userName",
NamespaceManager).Value;
string bSubjectEnd = " PTO time";
string bSuject = bUserName + bSubjectEnd;
int selectHoursDaysValue = Convert.ToInt32
(xnMyForm.SelectSingleNode("../my:selectHoursDays",
NamespaceManager).Value);
string bSingleDate = xnMyForm.SelectSingleNode("../
my:singleDate",
NamespaceManager).Value;
string bHours = xnMyForm.SelectSingleNode("../my:Hours",
NamespaceManager).Value;
string bDays = xnMyForm.SelectSingleNode("../my:days",
NamespaceManager).Value;

if (objappointment != null)
{
AppointmentItem appointment = (AppointmentItem)
objappointment;

//set importance as high
appointment.Importance =
OlImportance.olImportanceHigh;

//set subject
appointment.Subject = bSuject;

//set body
switch (selectHoursDaysValue)
{
case 1:
appointment.Body = string.Format("{0} is using
{1} Days and {2} Hours of PTO, from {3} to {4}.", bUserName, bDays,
bHours, bStartDateValue, bEndDateValue);
break;
case 2:
appointment.Body = string.Format("{0} is using
{1} Days of PTO, from {2} to {3}.", bUserName, bDays, bStartDateValue,
bEndDateValue);
break;
default:
appointment.Body = string.Format("{0} is using
{1} Hours of PTO, on {2}.", bUserName, bHours, bSingleDate);
break;

}

//set start time
appointment.Start = DateTime.Parse(bStartDateValue);

//set end time
appointment.End = DateTime.Parse(bEndDateValue);


//this event is not all day
appointment.AllDayEvent = true;

//save appointment to calendar
appointment.Save();

//cleanup
appointment = null;
}
}
 
P

Perseus109

I figured it out, you have to use the full path to the fields, not the
shortcut.
"/my:myFields/my:startDate" not "../my:startDate"

Also, if you are goig to do this, I found that you have to ad a day to
the end date to get it to post in outlook correctly:

appointment.End = (DateTime.Parse(bEndDateValue)).AddDays(1);

Hope this helps someone. :)

Perseus109
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top