View Single Post
  #8  
Old March 26th 08, 04:16 PM posted to microsoft.public.outlook.program_forms
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default Save a Form Region Content

This statement returns the item displayed:

Outlook.AppointmentItem newItem =
(Outlook.AppointmentItem)msEventFolder.Items.Add(" IPM.Appointment.Event");

Therefore, you should be subscribing to the Write event of that newItem object. Note, however, that unless the user (or your code) changes an actual Outlook property, the item will not be saved and Write will not fire.

I had a hard time following the rest of your code logic, and it's not just because I read C# only a little. It looks to me like you are creating new appointments in FormRegionShowing and SaveAppointment rather than working with the item (newItem) that is already displayed.

In any case, if your only goal in using a form region is to get the user to enter data into OUtlook properties, I would strongly suggest that you build the region in Outlook's designer rather than VS2008's and save yourself a lot of coding time.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"morna" wrote in message ...
On Mar 26, 6:15 am, "Sue Mosher [MVP-Outlook]"
wrote:
How are you instantiating the targetFolder object?

"morna" wrote in ...

On Mar 25, 1:05 pm, "Sue Mosher [MVP-Outlook]"
wrote:
VS 2008 or VSTO 2005 SE? If 2008, did you create the region in the designer or import an .ofs file?


To create a new instance of a custom form programmatically, use the Add method on the target folder's Items collection:


newItem = targetFolder.Items.Add("IPM.Appointment.MS Event")
newItem.Display


If the target is a default folder, you can use the Namespace.GetDefaultFolder method to return it as a Folder object.


"morna" wrote in ...


Also - I would like to put a "MS Event" commandbar on the content menu
when a user right clicks on the calendar - I have a commandbar
included there, however don't know what to put in the event handler to
make the new form region to appear...anyone got any thoughts?


The following works well:

newItem = targetFolder.Items.Add("IPM.Appointment.MS Event")
newItem.Display

and I hooked this up to my "New MS Event" on the right click content
menu.

However, when I select the "Save" on the ribbon for this item is never
saved to the calendar. When I select the "New MS Event" from the
Actions menu, the item gets saved to the calendar. Have I missed
something here...or is it another one of those Outlook dark secret
things


Hey Sue & all,

Here is the code that handles the CommandBar event OnUpdate - this
function is in the ThisAddin.cs file:

/// summary
/// This function handles the click event of the "New MS
Event" button on the context menu.
/// /summary
/// param name="btn"/param
/// param name="cancel"/param
public void newButton_Click(Office.CommandBarButton btn, ref
bool cancel)
{
try
{
// get the MS Event calendar folder
Microsoft.Office.Interop.Outlook.MAPIFolder Calendar =
m_applicationObject.GetNamespace("MAPI").GetDefaul tFolder(Microsoft.Office.Interop.Outlook.OlDefault Folders.olFolderCalendar);
Outlook.MAPIFolder msEventFolder =
Calendar.Folders["MS Event"];

// creates a new IPM.Appointment.Event object to store
the form region content
Outlook.AppointmentItem newItem =
(Outlook.AppointmentItem)msEventFolder.Items.Add(" IPM.Appointment.Event");
// displays the MS Event region form
newItem.Display(false);
}
catch (Exception ex)
{
Utilities.Log.WriteToLog(m_EventLogName,
"newButton_Click() err: " + ex.Message,
System.Diagnostics.EventLogEntryType.Error);
}
}

so... I am getting the Calendar collections of folders and then
looking for the "MS Event" calendar folder. at that point I use the
code you suggested... it creates a MS Event form for me, however, on
the save (see code below in the MSEventRegion.cs file - I could not
find a "Save" event on the Outlook.AppointmentItem - so I used the
Item_Write event) it does not save the item to the MS Event calendar.

// Outlook application object
private Outlook.Application m_applicationObject;
private Outlook.AppointmentItem Item;

// Occurs before the form region is displayed.
// Use this.OutlookItem to get a reference to the current
Outlook item.
// Use this.OutlookFormRegion to get a reference to the form
region.
private void FormRegion_FormRegionShowing(object sender,
System.EventArgs e)
{
m_applicationObject = new Outlook.Application();

Item = this.OutlookItem as Outlook.AppointmentItem;
Item.Write += new
Microsoft.Office.Interop.Outlook.ItemEvents_10_Wri teEventHandler(Item_Write);
}

/// summary
/// This function handles the save event of the MS Event
object
/// /summary
/// param name="Cancel"/param
public void Item_Write(ref bool Cancel)
{
try
{
SaveAppointment();
Cancel = false;

if (Item != null)
Item = null;
}
catch (Exception ex)
{
Utilities.Log.WriteToLog("FormRegion.Item_Write(.. )
err: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
}
}

// Occurs when the form region is closed.
// Use this.OutlookItem to get a reference to the current
Outlook item.
// Use this.OutlookFormRegion to get a reference to the form
region.
private void FormRegion_FormRegionClosed(object sender,
System.EventArgs e)
{
MessageBox.Show("Form Closed");
}

/// summary
/// This function saves the MSEvent object in a
IPM.Appointment item
/// /summary
private void SaveAppointment()
{
try
{
// get the MS Event folder
Microsoft.Office.Interop.Outlook.MAPIFolder Calendar =
m_applicationObject.GetNamespace("MAPI").GetDefaul tFolder(Microsoft.Office.Interop.Outlook.OlDefault Folders.olFolderCalendar);
Outlook.MAPIFolder msEventFolder =
Calendar.Folders["MS Event"];

if( msEventFolder != null )
{
Outlook.AppointmentItem appointmentItem =
(Outlook.AppointmentItem)m_applicationObject.Creat eItem(Outlook.OlItemType.olAppointmentItem);
AddUserPropToAppointment(appointmentItem);

appointmentItem.Subject = this.TextBoxName.Text;
appointmentItem.Start =
this.DatePickerStartDate.Value;
appointmentItem.End =
this.DatePickerEndDate.Value;
appointmentItem.Location =
this.TextBoxLocation.Text;

appointmentItem.UserProperties["Description"].Value =
this.TextBoxDescription.Text;
if(this.ComboBoxType.SelectedItem != null)
appointmentItem.UserProperties["Type"].Value =
this.ComboBoxType.SelectedItem.ToString();
appointmentItem.UserProperties["KeySpeaker"].Value
= this.TextBoxKeySpeaker.Text;

appointmentItem.UserProperties["MSEventWebsite"].Value =
this.TextBoxWebsite.Text;
appointmentItem.UserProperties["Attendance"].Value
= this.TextBoxEstAttendance.Text;
appointmentItem.UserProperties["BusOwner"].Value =
this.TextBoxBusOwner.Text;
}
}
catch(Exception ex)
{
Utilities.Log.WriteToLog("SaveAppointment() err: " +
ex.Message, System.Diagnostics.EventLogEntryType.Error);
}
}

/// summary
/// This function creates the custom user fields that will
allow for storage of an MSEvent object in a IPM.Appointment item
/// /summary
/// param name="appointmentItem"/param
private void AddUserPropToAppointment(Outlook.AppointmentItem
appointmentItem)
{
try
{
appointmentItem.UserProperties.Add("Description",
Microsoft.Office.Interop.Outlook.OlUserPropertyTyp e.olText, false,
System.Reflection.Missing.Value);
appointmentItem.UserProperties.Add("Type",
Microsoft.Office.Interop.Outlook.OlUserPropertyTyp e.olText, false,
System.Reflection.Missing.Value);
appointmentItem.UserProperties.Add("KeySpeaker",
Microsoft.Office.Interop.Outlook.OlUserPropertyTyp e.olText, false,
System.Reflection.Missing.Value);
appointmentItem.UserProperties.Add("MSEventWebsite ",
Microsoft.Office.Interop.Outlook.OlUserPropertyTyp e.olText, false,
System.Reflection.Missing.Value);
appointmentItem.UserProperties.Add("Attendance",
Microsoft.Office.Interop.Outlook.OlUserPropertyTyp e.olText, false,
System.Reflection.Missing.Value);
appointmentItem.UserProperties.Add("BusOwner",
Microsoft.Office.Interop.Outlook.OlUserPropertyTyp e.olText, false,
System.Reflection.Missing.Value);
}
catch (Exception ex)
{
Utilities.Log.WriteToLog("AddUserPropToAppointment (..)
err: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
}
}


Ads