首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >COMAddIn.Object始终为空

COMAddIn.Object始终为空
EN

Stack Overflow用户
提问于 2018-07-25 04:30:50
回答 1查看 459关注 0票数 0

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

代码语言:javascript
复制
  Outlook.Application OutlookObj = new Outlook.Application();
        object AddinName = "OutlookAddIn";
        COMAddIn AddIn = OutlookObj.COMAddIns.Item(ref AddinName);
        IOutLookApp utils = (IOutLookApp)AddIn.Object;
        utils.CallOlMethod();

这是TheAddIn.cs

代码语言:javascript
复制
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();
    }
}
}

我在这里做错了什么?虽然我的AddIn类是公开的,但是ComAddIn.Object仍然是空的,为什么呢?请帮助解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2018-07-25 21:32:12

要向外部调用者公开您的VSTO外接程序,请执行以下步骤:

  1. 将接口定义为dual并使其COM可见[ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface IOutLookApp { void CallOlMethod(); }
  2. 还定义了一个实现该接口的类,如下所示[ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class OutlookApp : StandardOleMarshalObject, IOutLookApp { public void CallOlMethod() { //do something } }

阅读更多:Call code in VSTO Add-ins from other Office solutionsVSTO Add-ins, COMAddIns and RequestComAddInAutomationService

编辑:还需要检查项目设置中的“注册Com interop",并确保以管理员身份运行Visual Studio。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51507056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档