我正在尝试使用VSTO Outlook AddIn实现电子邮件功能。但我得到的ComAddIn.Object始终为空,因此我无法访问VSTO AddIns的成员
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
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仍然是空的,为什么呢?请帮助解决这个问题。
发布于 2018-07-25 21:32:12
要向外部调用者公开您的VSTO外接程序,请执行以下步骤:
[ComVisible(true)] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface IOutLookApp { void CallOlMethod(); } [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class OutlookApp : StandardOleMarshalObject, IOutLookApp { public void CallOlMethod() { //do something } } 阅读更多:Call code in VSTO Add-ins from other Office solutions和VSTO Add-ins, COMAddIns and RequestComAddInAutomationService
编辑:还需要检查项目设置中的“注册Com interop",并确保以管理员身份运行Visual Studio。
https://stackoverflow.com/questions/51507056
复制相似问题