我尝试将操作传递给VSTO类,以获取邮件项的内容。
但是,当我调用Register方法时,我得到了System.ArgumentException。
[ComVisible(true)]
public interface IOutlookUtilities
{
void Register(Action<Outlook.MailItem> action);
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : StandardOleMarshalObject, IOutlookUtilities
{
[ComVisibleAttribute(true)]
public Action<Outlook.MailItem> ExtractMail;
public static AddInUtilities instance;
public AddInUtilities()
{
instance = this;
}
public void Register(Action<Outlook.MailItem> action)
{
this.ExtractMail = action;
}
}
在另一个c#解决方案中:
private void Form1()
{
Outlook.Application outlook = (Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
Microsoft.Office.Core.COMAddIns addins = outlook.COMAddIns;
if(addins != null)
{
Microsoft.Office.Core.COMAddIn addin = addins.Item("OutlookAddin1");
if(addin != null)
{
Action<Outlook.MailItem> action = new Action<Outlook.MailItem>((mail) =>
{
MessageBox.Show(mail.Subject);
});
addin.Object.Register(action); //<--exception from this line
}
}
}
异常消息:System.ArgumentException:'Type'System .__ ComObject'对象无法转换为'System.Action`1 [Microsoft.Office.Interop.Outlook.MailItem]'。”
我尝试过其他类型的课程。 似乎当我尝试将类类型传递给addin.Object的方法时。总是说无法投放。