Outlook 对象始终为空

Outlook 对象始终为空,outlook,vsto,Outlook,Vsto,我正在尝试使用VSTO Outlook插件实现电子邮件功能。但是 我得到的ComAddIn.Object始终为空,因此我无法 访问VSTO加载项的成员 Outlook.Application OutlookObj = new Outlook.Application(); object AddinName = "OutlookAddIn"; COMAddIn AddIn = OutlookObj.COMAddIns.Item(ref AddinName);

我正在尝试使用VSTO Outlook插件实现电子邮件功能。但是 我得到的ComAddIn.Object始终为空,因此我无法 访问VSTO加载项的成员

  Outlook.Application OutlookObj = new Outlook.Application();
        object AddinName = "OutlookAddIn";
        COMAddIn AddIn = OutlookObj.COMAddIns.Item(ref AddinName);
        IOutLookApp utils = (IOutLookApp)AddIn.Object;
        utils.CallOlMethod();
这是ADDIN.cs

namespace OutlookAddIn
{
public interface IOutLookApp
{
    void CallOlMethod();
}  

public partial class ThisAddIn
{
    protected override object RequestComAddInAutomationService()
    {
        OutlookApp ol = new OutlookApp();
        return ol;
    }
    #region VSTO generated code
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        // Note: Outlook no longer raises this event. If you have code that 
        //    must run when Outlook shuts down, see 
    https://go.microsoft.com/fwlink/?LinkId=506785
    }
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }
#endregion
public void CreateOutlookItem()
{
    Outlook.MailItem newEmail = new Outlook.MailItem
    {
        To = "example@gmail.com",
        Subject = "testing",
        Importance = Outlook.OlImportance.olImportanceLow
    };
    newEmail.Send();
}
}
public class OutlookApp:
   StandardOleMarshalObject,
    IOutLookApp
{
    public void CallOlMethod()
    {
        Globals.ThisAddIn.CreateOutlookItem();
    }
}
}
namespace-OutlookAddIn
{
公共接口IOutLookApp
{
void CallOlMethod();
}  
公共部分类ThisAddIn
{
受保护的重写对象RequestComAddInAutomationService()
{
OutlookApp ol=新的OutlookApp();
返回ol;
}
#区域VSTO生成的代码
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
//注意:Outlook不再引发此事件。如果您有
//必须在Outlook关闭时运行,请参阅
https://go.microsoft.com/fwlink/?LinkId=506785
}
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
public void CreateOutlookItem()
{
Outlook.MailItem newEmail=新建Outlook.MailItem
{
To=”example@gmail.com",
主题=“测试”,
重要性=Outlook.OlImportance.olImportanceLow
};
newEmail.Send();
}
}
公共类Outlook应用程序:
标准对象,
IOutLookApp
{
public void CallOlMethod()
{
Globals.ThisAddIn.CreateOutlookItem();
}
}
}

我做错了什么?虽然我的加载项类仍然公开,但ComAddIn.Object为null,为什么?请帮助解决此问题。

要向外部呼叫者公开VSTO插件,请执行以下步骤:

  • 将接口定义为双接口并使其COM可见
    
    [ComVisible(true)]
    [接口类型(ComInterfaceType.InterfaceIsDual)]
    公共接口IOutLookApp
    {
    void CallOlMethod();
    }
    
  • 还要定义一个实现接口的类,如下所示
    
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    公共类OutlookApp:StandardOleMarshallObject,IOOutlookApp
    {
    public void CallOlMethod()
    {
    //做点什么
    }
    }
    
  • 阅读更多:和

    编辑:
    还需要在项目设置中选中“注册Com互操作”,并确保以管理员身份运行Visual Studio

    为什么要尝试创建Outlook的新实例?由于VSTO是在进程中运行的,所以您通常使用运行外接程序的应用程序实例。我通常不使用Outlook,但是标准的“入门”说明当然没有提到需要启动一个新实例:另请参见-添加属性后出现以下错误**消息=无法将“System.\u ComObject”类型的COM对象强制转换为接口类型“OutlookAddIn.IOOutLookApp”。此操作失败,因为对IID为“{392A0EEE-0732-33E3-B3F3-721E29EE4BC8}”的接口的COM组件的QueryInterface调用由于以下错误而失败:不支持此类接口(HRESULT中的异常:0x80004002(E_NOINTERFACE))。请确保在外接程序项目属性的“生成”选项卡中已选中“为COM互操作注册”