View Single Post
  #7  
Old March 26th 08, 03:17 PM posted to microsoft.public.outlook.program_forms
morna
external usenet poster
 
Posts: 11
Default Save a Form Region Content

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

--
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 ...

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

-Morna- Hide quoted text -

- Show quoted text -


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