Bassem Murad

Hello,

I have 2 problems. Im using VSTO SE 2005 to create an addin for outlook 2003. This addin is supposed to add an item to the context menu of the email called "Save to DB". On clicking on that item, it should perform some task on the database and display a message box. My problem is that after the message box is displayed, i get the reply window. I want to cancel that but i cant figure out how. that is my first problem...

my second problem is that after the events gets fired once, it doesnt get fired again, when i select my custom action (i.e: i get to do it once only...the next times i get the reply window [back to problem one again})

im despeate and i need help urgently....

here is the code for the custom actions i used

void mailItem_CustomAction(object Action, object Response, ref bool Cancel)

{

try

{

Outlook.Action mailAction = (Outlook.Action)Action;

// Get the current selection from the explorer and

switch (mailAction.Name)

{

case "Copy to DB":

performAction();

break;

default:

MessageBox.Show("Error.");

break;

}

Cancel = false; // im not sure at all about this line

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}




Re: Visual Studio Tools for Office Outlook 2003 custom actions fail to fire

Sue Mosher - Outlook MVP

Cancel = false should cancel the creation of the response item. You might try moving it higher in the procedure. You could also try calling Response.Close olDiscard.

Is mailItem declared at the class level If not, it's going out of scope before you use it a second time.

Note that the custom action hack for add-in to the context menu is not recommended for messages. If you forward one of the affected messages and include an attachment, a non-Outlook recipient may not be able to open the attachment.

Also note that the current Explorer selection is not involved in this code at all, contrary to the comment.





Re: Visual Studio Tools for Office Outlook 2003 custom actions fail to fire

Bassem Murad

i forgot to mention..cancel it set to True, the addin works like a charm, but only of one time.. Sad

if i click on the menu-item of a different email, the event doesnt fire, but i just get a defualt reply window...






Re: Visual Studio Tools for Office Outlook 2003 custom actions fail to fire

Sue Mosher - Outlook MVP

Sorry, I meant Cancel = True. That's definitely correct and what you should be doing to suppress creation of the response item.

Again, to handle the event, do the work that you want to do on the item, and be able to suppress the reply, you need a valid MailItem object. That may not be possible if the user doesn't change the selection (firing Explorer.SelectionChange) before right-clicking. That's what makes this approach a hack and not a great solution.

Richard Kagerer has posted a code sample at http://www.outlookcode.com/codedetail.aspx id=314 that shows how you might trick Outlook into exposing the context menu through Explorer.CommandBars.

The C++ sample add-in at http://www.codeproject.com/atl/outlook2k3addin.asp also shows a technique for working with the context menu.

Outlook 2007 provides Application-level events for the most commonly used context menus.





Re: Visual Studio Tools for Office Outlook 2003 custom actions fail to fire

Bassem Murad

hi,

Thanks for the quick reply,

1 - Do you mean canel = true

i put it at the top of the event...and it disables the response the first time...

2 - i declared the mail item is at class level...still same problem...it runs the first time, doesnt run the second...

3- and yea, that is an old comment...it took me a while actually to understand what u where talking about.. Smile

here is how my code looks like...i try to show that menu item on all mails in all items..that is y i bind it on folderSwitch...

namespace OutlookAddIn

{

public partial class OutlookAddIn

{

string MenuItemName = "Send to DB";

private Outlook.Explorer explorer = null;

private Outlook.MailItem MyMailItem;

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

{

explorer = Application.Explorers.Application.ActiveExplorer();

explorer.SelectionChange += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_SelectionChangeEventHandler(explorer_SelectionChange);

Outlook.MAPIFolder mapi = Application.ActiveExplorer().CurrentFolder;

foreach (object x in mapi.Items)

{

MyMailItem = x as Outlook.MailItem;

if (MyMailItem != null)

{

Outlook.Action newAction = MyMailItem.Actions[MenuItemName.ToString()];

if (newAction == null)

{

newAction = MyMailItem.Actions.Add();

newAction.Name = MenuItemName.ToString();

newAction.ShowOn = Outlook.OlActionShowOn.olMenuAndToolbar;

newAction.Enabled = true;

MyMailItem.Save();

}

MyMailItem.CustomAction += new Microsoft.Office.Interop.Outlook.ItemEvents_10_CustomActionEventHandler(mailItem_CustomAction);

}

}

mapi.GetExplorer(1).FolderSwitch += new Microsoft.Office.Interop.Outlook.ExplorerEvents_10_FolderSwitchEventHandler(OutlookAddIn_FolderSwitch);

}

////////////////////////

void OutlookAddIn_FolderSwitch()

{

Outlook.MAPIFolder mapi = Application.ActiveExplorer().CurrentFolder;

foreach (object x in mapi.Items)

{

MyMailItem = x as Outlook.MailItem;

if (MyMailItem != null)

{

Outlook.Action newAction = MyMailItem.Actions[MenuItemName.ToString()];

if (newAction == null)

{

newAction = MyMailItem.Actions.Add();

newAction.Name = MenuItemName.ToString();

newAction.ShowOn = Outlook.OlActionShowOn.olMenuAndToolbar;

newAction.Enabled = true;

MyMailItem.Save();

}

MyMailItem.CustomAction += new Microsoft.Office.Interop.Outlook.ItemEvents_10_CustomActionEventHandler(mailItem_CustomAction);

}

}

}

/////////////////////

void mailItem_CustomAction(object Action, object Response, ref bool Cancel)

{

Cancel = true;

MyMailItem = (Outlook.MailItem)Response;

MyMailItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);

try

{

Outlook.Action mailAction = (Outlook.Action)Action;

switch (mailAction.Name)

{

case "Send to DB":

performAction();

break;

default:

MessageBox.Show("error...");

break;

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

////////////////////////////

private void performAction()

{

MyMailItem = explorer.Selection[1] as Outlook.MailItem;

// do stuff with subject and other mail objects

}

/////////

private void InternalStartup()

{

this.Startup += new System.EventHandler(ThisAddIn_Startup);

this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

}

}

}

Thanks in advance for the help...






Re: Visual Studio Tools for Office Outlook 2003 custom actions fail to fire

Bassem Murad

Hello again,

i came across this code http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=227517&SiteID=1 and i used it in a new project...it worked...then i changed a part of mine to work like it, and it did..

Thanks again






Re: Visual Studio Tools for Office Outlook 2003 custom actions fail to fire

zeltera

The event fire only one time because the Garbage collector comes and collect your object. You can prevent this by keeping a reference to that object in a HashTable object (or ArrayList).