如何在Outlook 2013外接程序中可靠地获取联系人上下文菜单的对象? [英] How can I reliably get the object of a contact context menu in an Outlook 2013 addin?

查看:186
本文介绍了如何在Outlook 2013外接程序中可靠地获取联系人上下文菜单的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在

该条目正确显示在菜单中,当我单击它时,将执行我的事件处理程序:

The entry shows up in the menu properly, and when I click it, my event handler is executed:

public void OnDoSomething(IRibbonControl control)
{
    object context = control.Context;
    System.Diagnostics.Debug.WriteLine(context.GetType());
    if ((context as IMsoContactCard) != null) System.Diagnostics.Debug.WriteLine("IMsoContactCard");
    if ((context as ContactItem) != null) System.Diagnostics.Debug.WriteLine("ContactItem");
    if ((context as ContactCard) != null) System.Diagnostics.Debug.WriteLine("ContactCard");
    if ((context as _ContactItem) != null) System.Diagnostics.Debug.WriteLine("_ContactItem");
}

引用的文章似乎表明上下文应该是IMsoContactCard,但这不是我要的.打印出context.GetType()的行显示为System.__ComObject.

The referenced article seems to indicate that the context should be an IMsoContactCard, but that's not what I'm getting. The line that prints out context.GetType() is displaying System.__ComObject.

本文似乎表明我应该能够对象变成有用的对象,但是将其强制转换为逻辑对象(IMsoContactCardContactItemContactCard_ContactItem)的所有尝试均失败了.

This article here seems to indicate that I should be able to cast that object into something useful, but all of the attempts to cast it into something logical (IMsoContactCard, ContactItem, ContactCard, _ContactItem) have failed.

为避开该问题,我尝试根据

In an attempt to sidestep the problem, I tried keeping track of the currently selected item, as per the instructions in this article. This actually works fairly well, with the caveat that the currently selected item is not always the item that the context menu applies to.

要详细说明,我可以在联系人上单击鼠标左键,将其突出显示,并触发我的选择事件.如果我然后右键单击其他联系人以调出上下文菜单,而无需先左键单击,那么该联系人将被概述但未突出显示,并且选择事件未触发.发生这种情况时,我最终将上下文菜单单击应用于错误的联系人.

To elaborate, I can left-click on a contact and it is highlighted and my selection event fires. If I then right-click on a different contact to bring up the context menu without left-clicking on it first, then that contact will be outlined but not highlighted, and my selection event is not fired. When this happens, I end up applying the context menu click to the wrong contact.

任何建议或指导将不胜感激.谢谢.

Any advice or direction would be appreciated. Thanks.

根据下面提供的信息,我能够确定此特定回调的ContextMicrosoft.Office.Interop.Outlook.Selection类型.然后,我可以使用它来获取ContactItem,如下所示:

Based on the information provided below, I was able to figure out that the Context of this particular callback is of type Microsoft.Office.Interop.Outlook.Selection. I can then use this to get the ContactItem as follows:

private ContactItem GetContactItemFromControlContext(IRibbonControl control)
{
    var selection = control.Context as Selection;
    if (selection != null && selection.Count == 1) 
        return selection[1] as ContactItem;
    else
        return null;
}

推荐答案

上下文对象的类型取决于您单击的位置和上下文菜单的类型.要获取基础类型,您需要执行以下操作:

The type of the Context object depends on the place where you clicked and the context menu type. To get the underlying type you need to do the following:

  1. 将对象投射到IDispatch接口.
  2. 使用IDispatch接口的GetTypeInfo方法获取ITypeInfo接口.
  3. 使用ITypeInfo接口的GetDocumentation方法获取类型名称.

  1. Cast the object to the IDispatch interface.
  2. Get the ITypeInfo interface using the GetTypeInfo method of the IDispatch interface.
  3. Get the type name using GetDocumentation method of the ITypeInfo interface.

public static string GetTypeName(object comObj)
{

    if (comObj == null)
        return String.Empty;

    if (!Marshal.IsComObject(comObj))
        //The specified object is not a COM object
        return String.Empty;

    IDispatch dispatch = comObj as IDispatch;
    if (dispatch == null)
        //The specified COM object doesn't support getting type information
        return String.Empty;

    ComTypes.ITypeInfo typeInfo = null;
    try
    {
        try
        {
            // obtain the ITypeInfo interface from the object
            dispatch.GetTypeInfo(0, 0, out typeInfo);
        }
        catch (Exception ex)
        {
            //Cannot get the ITypeInfo interface for the specified COM object
            return String.Empty;
        }

        string typeName = "";
        string documentation, helpFile;
        int helpContext = -1;

        try
        {
            //retrieves the documentation string for the specified type description 
            typeInfo.GetDocumentation(-1, out typeName, out documentation,
                out helpContext, out helpFile);
        }
        catch (Exception ex)
        {
            // Cannot extract ITypeInfo information
            return String.Empty;
        }
        return typeName;
    }
    catch (Exception ex)
    {
        // Unexpected error
        return String.Empty;
    }
    finally
    {
        if (typeInfo != null) Marshal.ReleaseComObject(typeInfo);
    }
}

}

请参见如何:使用Visual C#或VB.NET获取System .__ ComObject后面的实际类型名称.

这篇关于如何在Outlook 2013外接程序中可靠地获取联系人上下文菜单的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆