none
How launch Form from Outlook context menu RRS feed

  • Question

  • Hi!

    I have following task. The mail have task in context menu create my Item.

    The use case is:

    In inbox folder I click right mouse button on mail. Appears context menu and it contain item - "Add record to DataBase".

    And appears my Form (inherits from Windows.Forms).

    I subscribe to MailItem.CustomAction and add Action to MailItem.

     

    The code:

     

    I subscribe on SelectionChange of Explorer.

     

    Code Snippet

    private void ThisAddIn_Startup(object sender, System.EventArgs e)

    {

    m_explorer = Globals.ThisAddIn.Application.ActiveExplorer();

    m_explorer.SelectionChange += Explorer_SelectionChange;

    }

     

     

    Function SelectionChangedAnd

     

    Code Snippet

    private void Explorer_SelectionChange()

    {

    foreach (object selectedItem in m_explorer.Selection)

    {

    // we only want to deal with selected mail items

    MailItem item = selectedItem as MailItem;

    if (item != null)
    {
             // see if the action already exists on mail item
             Action newAction = item.Actions[createNewPrompt];
             // and create it if it does not
             if (newAction == null)
             {
                     newAction = item.Actions.Add();
                     newAction.Name = createNewPrompt;
                     newAction.ShowOn = OlActionShowOn.olMenu;
                     newAction.Enabled = true;
                     item.Save();
             }
             // add the event handler for our action
             item.CustomAction += MailItem_CustomAction;
    }

    }

    }

     

     

    And processing customAction event:

    Code Snippet

    private void MailItem_CustomAction(object Action, object Response, ref bool Cancel)

    {

    try

    {

    Action mailAction = (Action)Action;

    if (mailAction.Name.Equals(CreateNewPrompt))

    {

    try

    {

    CreateDataBaseRecordForm _form = new CreateDataBaseRecordForm(m_explorer.Selection[1] as MailItem, _pluginConfig);

    _form.Show();

    }

    finally

    {

    Cancel = true;

    }

    }

    }

    catch (Exception e)

    {

    MessageBox.Show(e.ToString());

    }

    }

     

     

    Once form will open. But in second time appear default Create Mail dialog.

    Why?

    How I can it correct?

    Maybe I do this incorrect? How do this correct?

     

    The task in common is this:

    Add custom item to context menu mail. By this custom item open dialog, create record and close dialog. And this must work always, even if user create record by one mail more and more.

    Tuesday, February 12, 2008 11:51 AM

Answers

  • Hi,

     

    It happens because you put item variable declaration in the method scope.

    Code Snippet

    private void Explorer_SelectionChange()

    {

    foreach (object selectedItem in m_explorer.Selection)

    {

    // we only want to deal with selected mail items

    MailItem item = selectedItem as MailItem;

    if (item != null)
    {
             // see if the action already exists on mail item
             Action newAction = item.Actions[createNewPrompt];
             // and create it if it does not
             if (newAction == null)
             {
                     newAction = item.Actions.Add();
                     newAction.Name = createNewPrompt;
                     newAction.ShowOn = OlActionShowOn.olMenu;
                     newAction.Enabled = true;
                     item.Save();
             }
             // add the event handler for our action
             item.CustomAction += MailItem_CustomAction;
    }

    }

    }

     

     

    Since the method finished, nothing has an handle to CustomAction event and it is released by GC. Consequently, the CustomAction event handle will not be called for second time.

    Since you may have multiple selected email at one time. You need to declare a MailItem Array at Class Scope, and in foreach loop use an item in the array to keep reference of one selected email, and then subscribe the CustomAction event.

     

     

    Thanks

    Ji

    Thursday, February 14, 2008 2:31 AM
    Moderator

All replies

  • Hi,

     

    It happens because you put item variable declaration in the method scope.

    Code Snippet

    private void Explorer_SelectionChange()

    {

    foreach (object selectedItem in m_explorer.Selection)

    {

    // we only want to deal with selected mail items

    MailItem item = selectedItem as MailItem;

    if (item != null)
    {
             // see if the action already exists on mail item
             Action newAction = item.Actions[createNewPrompt];
             // and create it if it does not
             if (newAction == null)
             {
                     newAction = item.Actions.Add();
                     newAction.Name = createNewPrompt;
                     newAction.ShowOn = OlActionShowOn.olMenu;
                     newAction.Enabled = true;
                     item.Save();
             }
             // add the event handler for our action
             item.CustomAction += MailItem_CustomAction;
    }

    }

    }

     

     

    Since the method finished, nothing has an handle to CustomAction event and it is released by GC. Consequently, the CustomAction event handle will not be called for second time.

    Since you may have multiple selected email at one time. You need to declare a MailItem Array at Class Scope, and in foreach loop use an item in the array to keep reference of one selected email, and then subscribe the CustomAction event.

     

     

    Thanks

    Ji

    Thursday, February 14, 2008 2:31 AM
    Moderator
  • Thanks I realize how solve this problem by your answer.

     

    Wednesday, February 20, 2008 9:33 AM