使用新的Outlook VSTO C#项目创建的ThisAddIn类具有Application属性,您可以使用该属性来访问Outlook文件夹和项。问题是,当您在ThisAddIn类中时,您可以很容易地使用它,但从项目中的其他类访问它并不容易。这是因为它是一个实例属性。
我想找到最好的方法来访问这个属性在我的其他类中提供的相同功能,所以我想出了两个可能的解决方案,但我不知道哪一个(如果有)是好的。
假设我想要获取默认的收件箱文件夹。在ThisAddIn类中,我会简单地做如下操作:
this.Application.Session.GetDefaultFolder(Outlook.olFolderInbox);现在如何在这个类之外做同样的事情呢?
1.静态属性
首先,我可以向ThisAddIn类添加一个静态属性,并将其设置为我希望在其他类中公开的值。
public partial class ThisAddIn
{
public Outlook.Application OutlookApp;
void ThisAddIn_Startup(object sender, EventArgs e)
{
// init static variable value here
OutlookApp = this.Application
// initialize the rest of addin here
}
void InternalStartup()
{
this.Startup += this.ThisAddIn_Startup;
}
}通过这种方式,在我的任何其他类中,我都可以这样做:
ThisAddIn.OutlookApp.Session.GetDefaultFolder(Outlook.olFolderInbox);2.创建新的Application对象
我可以做的第二件事是在使用Application对象之前在我的另一个类中初始化它。但我不确定创建该类型的新对象是否会创建Outlook的新实例。
class MyOtherClass
{
public void MyMethod()
{
var app = new Outlook.Application();
var folder = app.Session.GetDefaultFolder(Outlook.olFolderInbox);
}
}有没有人有任何建议哪种方法更好,如果你对这个问题有不同的解决方案,我也会提出来。
https://stackoverflow.com/questions/2175803
复制相似问题