none
Closing a FormRegion without Prompting to Save RRS feed

  • Question

  • How do we signal Outlook that a FormRegion may be closed without promoting the user to Save?

     

    We’ve developed an Outlook 2010 COM AddIn using VSTO and VS2012 and it contains several forms.  Users activate these forms from Custom Ribbon Button Clicks.  In the event handler(s), we Add a new item to the Inbox using the appropriate form’s Message Class.  Most forms are blank.  If the user simply closes the form, some of them prompt to save, while others do not.

     

    What triggers the need to ask the user to save a form message prior to closing?  The MailItem.Saved() property appears Read-Only.

    Thursday, January 2, 2014 7:29 PM

Answers

  • Hi Peter,

    It looks like the difference in behavior depends on the parameter passed to the Close method of Outlook items. The following values can be used:

    olDiscard                  

    1

    Changes to the document are discarded.

    olPromptForSave                  

    2

    User is prompted to save documents.

    olSave                  

    0

    Documents are saved.

    Also the Outlook Object Model provides the Close event for their items such as MailItem, AppointmentItem and etc. You can subscribe to the Close event and display your own dialog for saving if needed.

    Friday, January 3, 2014 9:00 AM

All replies

  • Hello Peter,

    The form region is not related to prompts for saving items in Outlook. You need to set a property of the item before closing it for getting the prompt. If you call the Save method of the Outlook item before closing the inspector window you will not get a prompt. Most probably you don't release underlying COM objects instantly. You can read more about this in the Systematically Releasing Objects article in MSDN.

    Finally, could you please describe a particular case for further investigation?

    Thursday, January 2, 2014 10:00 PM
  • Hello Peter,

    The form region is not related to prompts for saving items in Outlook. You need to set a property of the item before closing it for getting the prompt. If you call the Save method of the Outlook item before closing the inspector window you will not get a prompt. Most probably you don't release underlying COM objects instantly. You can read more about this in the Systematically Releasing Objects article in MSDN.

    Finally, could you please describe a particular case for further investigation?

    Thanks for the reply, Eugene.

    We can see how a failure to release objects may create problems such as this. But in this case the "prompt to save" upon closing mimics that of Outlook’s Create New versus Reply To email message.  For example, we activate our form(s) in one of two ways.  One creates a new mail item using Outlook.Items.Add() and the other uses Outlook.MailItem.Reply().  Refer to the code provided below.  The behavior we see follows that of Outlook’s Create New message and Reply To message.  Closing each of Outlook’s native forms upon creation, one prompts to save, the other does not.

    So the question is:  Do we have any control over Outlook asking to prompt for saving on a close?  We want to suspend prompting when nothing has been entered into our COM AddIn FormRegion(s), and enforce prompting when they do.  Calling Save() just to suppress the message seems messy, if doing so leaves an entry in the Drafts folder.

        Public Sub Button_Click(ByVal control As Office.IRibbonControl)

            MyDebug.Print("Button_Click({0})", control.Id)

            Try

                Select Case control.Id

                    Case "Button1"

                        Dim Application As Outlook.Application = _

                            Globals.ThisAddIn.Application

                        Dim MAPI As Outlook.NameSpace = _

                            Application.GetNamespace("MAPI")

                        Dim Inbox As Outlook.MAPIFolder = _

                            MAPI.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)

                        Dim Items As Outlook.Items = Inbox.Items

                        Dim newItem As Outlook.MailItem = _

                            Items.Add(ThisAddIn.MC_HELPDESK_CREATE)

                        newItem.Display(False)

                        newItem = Nothing

                        Items = Nothing

                        Inbox = Nothing

                        MAPI = Nothing

     

                    Case "Button2"

                        Dim CurrentExplorer As Outlook.Explorer = _

                            TryCast(control.Context, Outlook.Explorer)

                        Dim CurrentInspector As Outlook.Inspector = _

                            TryCast(control.Context, Outlook.Inspector)

                        Dim CurrentItem As Outlook.MailItem = Nothing

                        Try

                            If Not (CurrentExplorer Is Nothing) Then

                                If CurrentExplorer.Selection.Count = 1 Then

                                    CurrentItem = _

                                        TryCast(CurrentExplorer.Selection.Item(1), _

                                            Outlook.MailItem)

                                End If

                            ElseIf Not (CurrentInspector Is Nothing) Then

                                CurrentItem = TryCast(CurrentInspector.CurrentItem, _

                                    Outlook.MailItem)

                            End If

     

                            If Not (CurrentItem Is Nothing) Then

                                If (TypeOf CurrentItem Is Outlook.MailItem) Then

                                    Dim newItem As Outlook.MailItem

                                    newItem = CurrentItem.Reply()

                                    CurrentItem.Close(Outlook.OlInspectorClose.olDiscard)

                                    newItem.MessageClass = ThisAddIn.MC_HELPDESK_REPLY

                                    newItem.Display()

                                End If

                            End If

                        Catch ex As Exception

                            Windows.Forms.MessageBox.Show(ex.ToString, _

                                My.Application.Info.Title)

                        Finally

                            CurrentItem = Nothing

                            CurrentInspector = Nothing

                            CurrentExplorer = Nothing

                        End Try

     

                    Case Else

                        MyDebug.Print(String.Format( _

                            "Unhandled Ribbon Click from: {0}", control.Id))

                End Select

            Catch ex As Exception

                Windows.Forms.MessageBox.Show(ex.ToString, My.Application.Info.Title)

            End Try

        End Sub

     

    Thursday, January 2, 2014 11:05 PM
  • Hi Peter,

    It looks like the difference in behavior depends on the parameter passed to the Close method of Outlook items. The following values can be used:

    olDiscard                  

    1

    Changes to the document are discarded.

    olPromptForSave                  

    2

    User is prompted to save documents.

    olSave                  

    0

    Documents are saved.

    Also the Outlook Object Model provides the Close event for their items such as MailItem, AppointmentItem and etc. You can subscribe to the Close event and display your own dialog for saving if needed.

    Friday, January 3, 2014 9:00 AM