RE: VSTO 2005 Beta 2 Outlook Addin: How to create a context menu?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I would not recommend developing a solution around a context menu if you need
it to be 100% reliable. There are scenarios (such as the one you've found)
where the event does not fire thus your code to add you context menu does not
fire. The sample code checks to see if the mail item's subject contains
specific text and if so add the context menu. You should be able to just
strip this out and add it for all items. Also we cast the selectedItem as a
MailItem. If I you want to add it to other objects just try to type cast it
to other objects such as a contact or calendar item.

"David White [MSFT]" wrote:

> I like the sample thanks. My problem is similiar but my quesiton would be how
> to I get the action for all calendar items? By using your code the action
> menu would not appear the first time I right clicked but it would the second
> time. As I would expect since I added the action to the item. How do I make
> it an action for any and all calendar (or other object) entries?
>
> "Roosevelt Sheriff [MSFT]" wrote:
>
> > Here is some sample code that copies an email message using a custom menu
> > command.
> > Outlook.Explorer explorer = null;
> > private const string FOLDER_NAME = "Picnic";
> >
> > System.Collections.ArrayList selectedItems = new
> > System.Collections.ArrayList();
> >
> > private void ThisApplication_Startup(object sender,
> > System.EventArgs e)
> > {
> > explorer = this.Explorers.Application.ActiveExplorer();
> > explorer.SelectionChange += new Outlook.
> > ExplorerEvents_10_SelectionChangeEventHandler
> > (explorer_SelectionChange);
> > }
> >
> > void explorer_SelectionChange()
> > {
> > selectedItems.Clear();
> >
> > foreach (object selectedItem in explorer.Selection)
> > {
> > Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
> >
> > if (mailItem != null)
> > {
> > if (mailItem.Subject.Contains(FOLDER_NAME))
> > {
> > Outlook.Action newAction = mailItem.Actions[
> > string.Format("Copy to {0} Folder", FOLDER_NAME)];
> >
> > if (newAction == null)
> > {
> > newAction = mailItem.Actions.Add();
> > newAction.Name = string.Format
> > ("Copy to {0} Folder", FOLDER_NAME);
> > newAction.ShowOn =
> > Outlook.OlActionShowOn.olMenu;
> > newAction.Enabled = true;
> > mailItem.Save();
> > }
> >
> > mailItem.CustomAction += new Outlook.
> > ItemEvents_10_CustomActionEventHandler
> > (mailItem_CustomAction);
> >
> > selectedItems.Add(mailItem);
> > }
> > }
> > }
> > }
> >
> > 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
> > // copy it to the FOLDER_NAME folder
> > switch (mailAction.Name)
> > {
> > case "Copy to " + FOLDER_NAME + " Folder":
> > Outlook.MailItem mailItem =
> > explorer.Selection[1] as Outlook.MailItem;
> > Outlook.MailItem mailItemCopy =
> > mailItem.Copy() as Outlook.MailItem;
> >
> > Outlook.MAPIFolder inbox =
> > this.GetNamespace("MAPI").GetDefaultFolder
> > (Outlook.OlDefaultFolders.olFolderInbox);
> > Outlook.MAPIFolder folder = null;
> >
> > // If the folder does not exist, Create it.
> > try
> > {
> > folder = inbox.Folders[FOLDER_NAME];
> > }
> > catch (System.Runtime.InteropServices.COMException)
> > {
> > // This exception is thrown when the folder is not found.
> > }
> > catch (Exception ex)
> > {
> > throw ex;
> > }
> > if (folder == null)
> > {
> > folder = inbox.Folders.Add(FOLDER_NAME,
> > Outlook.OlDefaultFolders.olFolderInbox);
> > }
> >
> > mailItemCopy.Move(folder);
> > // Do not display the inspector object.
> > Cancel = true;
> > break;
> > }
> > }
> > catch (Exception ex)
> > {
> > MessageBox.Show(ex.Message);
> > }
> > }
> >
> >
> >
> > "buechi" wrote:
> >
> > > I did some googling for creating a context menu entry IN VSTO 2005 Beta
> > > 2 (for example on a MailItem) with an icon and catching the click
> > > event? I didn't really find a promising solution. Has anyone a simple
> > > and clear example how to do this?
> > >
> > > Cheers,
> > >
> > > Daniel.
> > >
> > >
.



Relevant Pages

  • Re: WaitForSingleObject and Thread
    ... Putting the cast in such context is *ALWAYS* a coding error. ... why did you manually expand LPVOID to void*? ... MainProc will block until the ReadThread is finished. ... reasonably certain that if all you do is launch a single thread and wait for it ...
    (microsoft.public.vc.mfc)
  • Re: Passing DataTable Across ASPX Pages
    ... You may use Server.Transfer to make the previous page's context available. ... Here's some sample code from the code library on my site: ... Private Sub SubmitButton_Click(ByVal sender As System.Object, ... arrived from before you may correctly cast the context object. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: [RFC -v2] kfifo writer side lock-less support
    ... The sample code is attached with the mail too. ... This makes kfifo can be used to implement per-CPU lock-less data ... right side context can preempt left side context, ... Rostedt's trace ring buffer implementation. ...
    (Linux-Kernel)
  • [PATH -mm -v2] Fix a race condtion of oops_in_progress
    ... extern int cause_of_death; ... void machine_restart ... context or a bust_spinlocks cleared the way for us. ...
    (Linux-Kernel)
  • Re: Warning to newbies
    ... alone "bogus" science. ... In that context, I believe the minotaurs should abandon C ... I particularly like "assign a pointer to void", ... NaN or either infinity is a trap representation. ...
    (comp.lang.c)