Outlook Add-in, RibbonType Microsoft.Outlook.Explorer and Microsoft.Outlook.Mail.Read

耗尽温柔 提交于 2020-01-23 01:18:07

问题


I have an Outlook 2010 Add-in coded in .NET 4.0/VS.NET 2010, C#. The Add-in extends the Ribbon => it adds a RibbonTab with 4 RibbonButtons to (RibbonType Property is set to) Microsoft.Outlook.Explorer and Microsoft.Outlook.Mail.Read.

Now, if the user clicks on one of the RibbonButtons, how can i determine if the user clicked on the button which is added to the Microsoft.Outlook.Explorer OR Microsoft.Outlook.Mail.Read?


回答1:


One option is to create (2) Ribbons with a shared capability library. When you call your shared library - you can pass the context of which ribbon action was clicked.

The better choice though is to check the ActiveWindow property of the application to determine which context the user is in via ThisAddin.Application.ActiveWindow().

 var windowType = Globals.ThisAddin.Application.ActiveWindow();
 if (windowType is Outlook.Explorer)
 {
     Outlook.Explorer exp = type as Outlook.Explorer;
     // you have an explorer context
 }
 else if (windowType is Outlook.Inspector)
 {
     Outlook.Inspector exp = type as Outlook.Inspector;
     // you have an inspector context
 }



回答2:


The proposal from SilverNinja pointed me in a direction .. finally my code is:

                // get active Window
            object activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
            if (activeWindow is Microsoft.Office.Interop.Outlook.Explorer)
            {
                // its an explorer window
                Outlook.Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();
                Outlook.Selection selection = explorer.Selection;
                for (int i = 0; i < selection.Count; i++)
                {
                    if (selection[i + 1] is Outlook.MailItem)
                    {
                        Outlook.MailItem mailItem = selection[i + 1] as Outlook.MailItem;
                        CreateFormOrForm(mailItem);
                    }
                    else
                    {
                        Logging.Logging.Log.Debug("One or more of the selected items are not of type mail message..!");
                        System.Windows.Forms.MessageBox.Show("One or more of the selected items are not of type mail message..");
                    }
                }
            }
            if (activeWindow is Microsoft.Office.Interop.Outlook.Inspector)
            {
                // its an inspector window
                Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector();
                Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem;
                CreateFormOrForm(mailItem);
            }

maybe this is of help for someone else ...



来源:https://stackoverflow.com/questions/16084995/outlook-add-in-ribbontype-microsoft-outlook-explorer-and-microsoft-outlook-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!