Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming Forms / March 2008

Tip: Looking for answers? Try searching our database.

Save a Form Region Content

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
morna - 25 Mar 2008 20:44 GMT
I have created a form region from a IPM.Appointment item and have my
new item on the Actions menu "New MS Event".  Two things...when I
select "New MS Event" in a calendar I am presented with the new form
region that I created.  I populate all the fields on the form region
and the select "Save" button on the Ribbon.  When I select open again
on the item I saved the fields on the form region are not populated.
I have this built within a VSTO addin and I can catch the
FormRegion_FormRegionClosed(..) event.. should I be saving the content
of this form region or should Outlook be saving the content?  Does
anyone have a code example?

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?

Thanks for your time.
-Morna
Sue Mosher [MVP-Outlook] - 25 Mar 2008 21:05 GMT
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.

Signature

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

>I have created a form region from a IPM.Appointment item and have my
> new item on the Actions menu "New MS Event".  Two things...when I
[quoted text clipped - 14 lines]
> Thanks for your time.
> -Morna
morna - 25 Mar 2008 22:00 GMT
On Mar 25, 1:05 pm, "Sue Mosher [MVP-Outlook]"
<sue...@outlookcode.com> wrote:
> VS 2008 or VSTO 2005 SE? If 2008, did you create the region in the designer or import an .ofs file?
>
[quoted text clipped - 31 lines]
>
> - Show quoted text -

Hey Sue -

I used VS 2008 and I did create the region in the designer?

How can I get a hook to the FormRegion.cs object from the ThisAddin
class?  Also... when a user selects the "Save" on the ribbon of the
FormRegion can I catch this event in my addin?
I can create a new appointment item on the fly if I can catch the save
event and the use UserProperties to save fields that are not really in
the IPM.Appointment object and save them to the MS Event folder if I
can catch the "Save" event that fires when a user selects the "Save"
on the ribbon of the FormRegion... just thinking out loud.

Thanks so much for your time.

-Morna
Sue Mosher [MVP-Outlook] - 25 Mar 2008 22:12 GMT
> I used VS 2008 and I did create the region in the designer?

In that case, you will have to write code to set the values of the Outlook properties where you want the region's control data to be stored. You could avoid that by creating the region in the Outlook designer and importing it instead of using the VS 2008 designer.

> Also... when a user selects the "Save" on the ribbon of the FormRegion can I catch this event in my addin?

When the user opens the item (Inspectors.NewInspector, followed by Inspector.Activate), you can subscribe to the AppointmentItem.Save event. However, I'm not sure whether it's better to use Activate or use one of the form region events. Might take some experimenting.

Signature

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

On Mar 25, 1:05 pm, "Sue Mosher [MVP-Outlook]"
<sue...@outlookcode.com> wrote:
> VS 2008 or VSTO 2005 SE? If 2008, did you create the region in the designer or import an .ofs file?
>
[quoted text clipped - 32 lines]
>
> - Show quoted text -

Hey Sue -

I used VS 2008 and I did create the region in the designer?

How can I get a hook to the FormRegion.cs object from the ThisAddin
class?  Also... when a user selects the "Save" on the ribbon of the
FormRegion can I catch this event in my addin?
I can create a new appointment item on the fly if I can catch the save
event and the use UserProperties to save fields that are not really in
the IPM.Appointment object and save them to the MS Event folder if I
can catch the "Save" event that fires when a user selects the "Save"
on the ribbon of the FormRegion... just thinking out loud.

Thanks so much for your time.

-Morna
morna - 26 Mar 2008 05:09 GMT
On Mar 25, 1:05 pm, "Sue Mosher [MVP-Outlook]"
<sue...@outlookcode.com> wrote:
> VS 2008 or VSTO 2005 SE? If 2008, did you create the region in the designer or import an .ofs file?
>
[quoted text clipped - 31 lines]
>
> - Show quoted text -

Hey Sue..

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
Sue Mosher [MVP-Outlook] - 26 Mar 2008 14:15 GMT
How are you instantiating the targetFolder object?

Signature

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

On Mar 25, 1:05 pm, "Sue Mosher [MVP-Outlook]"
<sue...@outlookcode.com> wrote:
> VS 2008 or VSTO 2005 SE? If 2008, did you create the region in the designer or import an .ofs file?
>
[quoted text clipped - 11 lines]
> > 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
morna - 26 Mar 2008 15:17 GMT
On Mar 26, 6:15 am, "Sue Mosher [MVP-Outlook]"
<sue...@outlookcode.com> wrote:
> How are you instantiating the targetFolder object?
>
[quoted text clipped - 40 lines]
>
> - 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").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.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_WriteEventHandler(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").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
               Outlook.MAPIFolder msEventFolder =
Calendar.Folders["MS Event"];

               if( msEventFolder != null )
               {
                   Outlook.AppointmentItem appointmentItem =
(Outlook.AppointmentItem)m_applicationObject.CreateItem(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.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("Type",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("KeySpeaker",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("MSEventWebsite",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("Attendance",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("BusOwner",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
           }
           catch (Exception ex)
           {
               Utilities.Log.WriteToLog("AddUserPropToAppointment(..)
err: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
           }
       }
Sue Mosher [MVP-Outlook] - 26 Mar 2008 16:16 GMT
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.

Signature

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

On Mar 26, 6:15 am, "Sue Mosher [MVP-Outlook]"
<sue...@outlookcode.com> wrote:
> How are you instantiating the targetFolder object?
>
[quoted text clipped - 31 lines]
> 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").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.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_WriteEventHandler(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").GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
               Outlook.MAPIFolder msEventFolder =
Calendar.Folders["MS Event"];

               if( msEventFolder != null )
               {
                   Outlook.AppointmentItem appointmentItem =
(Outlook.AppointmentItem)m_applicationObject.CreateItem(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.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("Type",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("KeySpeaker",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("MSEventWebsite",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("Attendance",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
               appointmentItem.UserProperties.Add("BusOwner",
Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false,
System.Reflection.Missing.Value);
           }
           catch (Exception ex)
           {
               Utilities.Log.WriteToLog("AddUserPropToAppointment(..)
err: " + ex.Message, System.Diagnostics.EventLogEntryType.Error);
           }
       }
morna - 26 Mar 2008 16:53 GMT
On Mar 26, 8:16 am, "Sue Mosher [MVP-Outlook]"
<sue...@outlookcode.com> wrote:
> This statement returns the item displayed:
>
[quoted text clipped - 236 lines]
>
> - Show quoted text -

Hey Sue & all,

My problem here is I did build the form region through Outlook
designer and I have an MSEvent.ofs file for the form, however, I have
never been able to work throught the load process for the form to get
it to display in Outlook.  I have issues with the manifest.xml file
and the regedit enters.  I even purchased a book "Professional Outlook
2007 Programming" by Ken Slovak.  I look hard at your book "Outlook
2007  Programming" but the  editior's comments and your comments on
Amazon said it was not really for programming more for
administration.  Can you point me in a direction - a website or a book
that outlines loading a *.ofs file into an addin and working with the
saved content of the form....I reviewed several other books that
perhaps would cover the form regions, however, have found nothing that
could really help get this up and running.

Thank you so much for your time.

-Morna
Sue Mosher [MVP-Outlook] - 26 Mar 2008 17:14 GMT
There are two issues here -- design of the region and loading it in the add-in. The loading for VS2008 is covered in the MSDN article at http://msdn2.microsoft.com/en-us/library/bb608611.aspx

The critical issue in the design is that each control that you want to use for entering data to be stored permanently needs to be bound to an Outlook property. Do that on the Value tab of each control's properties dialog, in the Outlook designer. Once you do that, data that the user types into the region's controls will automatically be saved with the item. In other words, no code will be needed to add properties or save the item.

Signature

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

My problem here is I did build the form region through Outlook
designer and I have an MSEvent.ofs file for the form, however, I have
never been able to work throught the load process for the form to get
it to display in Outlook.  I have issues with the manifest.xml file
and the regedit enters.  I even purchased a book "Professional Outlook
2007 Programming" by Ken Slovak.  I look hard at your book "Outlook
2007  Programming" but the  editior's comments and your comments on
Amazon said it was not really for programming more for
administration.  Can you point me in a direction - a website or a book
that outlines loading a *.ofs file into an addin and working with the
saved content of the form....I reviewed several other books that
perhaps would cover the form regions, however, have found nothing that
could really help get this up and running.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.