How can I reliably get the object of a contact context menu in an Outlook 2013 addin?

前端 未结 1 2006
小蘑菇
小蘑菇 2021-01-25 12:29

I am adding an entry to the context menu for a Contact in Outlook 2013 following the example in this article. Here is the XML:



        
1条回答
  •  失恋的感觉
    2021-01-25 13:00

    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. 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);
          }
      }
      

      }

    See HowTo: get the actual type name behind System.__ComObject with Visual C# or VB.NET for more information.

    0 讨论(0)
提交回复
热议问题