开发者

How to access Application property in VSTO Outlook add-in outside of ThisAddIn class?

开发者 https://www.devze.com 2022-12-18 11:54 出处:网络
ThisAddIn class created with new Outlook VSTO C# project has a Application property that you can use to among other things get access to Outlook folders and items. The problem is that you can easily u

ThisAddIn class created with new Outlook VSTO C# project has a Application property that you can use to among other things get access to Outlook folders and items. The problem is that you can easily use it when you're inside of ThisAddIn class but there's no easy access to it from other classes in the project. This is because it's an instance property.

I want to find the best way of having access to the same functionality this property provides in my other classes so I come up with two possible solutions but I don't know which one (if any) of them is good.

Lets assume I want to get default inbox folder. Inside ThisAddIn class I would simply do something like this:

this.Application.Session.GetDefaultFolder(Outlook.olFolderInbox);

Now how to do the same outside this class?

1. Static property

First, I could add a static property to ThisAddIn class and set it to the value I want to expose in other classes.

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;
    }
}

This way in any of my other classes I could do something like this:

ThisAddIn.OutlookApp.Session.GetDefaultFolder(Outlook.olFolderInbox);

2. Create new Application object

Second thing that I could do is to init Application object in my other class before I use it. But I'm not sure if creating new object of that type doesn't create a new instance of Outlook.

class MyOtherClass
{
    public void MyMethod()
    {
        var app = new Outlook.Application();
        var folder = app.Session.GetDefaultFol开发者_开发知识库der(Outlook.olFolderInbox);
    }
}

Does anyone have any suggestions which approach is better, of if you have different solutions for this problem I'd apprieciate that as well.


Since you can have single Instance of ThisAddIn you can have a static variable to access Application form outside... FYI when you add Outlook-AddIn VSTO project,instance of ThisAddIn will be available as static member in static class Globals


It is actually bad practice to place static variables in the ThisAddIn in order to reference from around your code.

According to this answer https://stackoverflow.com/a/46493968/2068626, the Outlook Application object is a singleton so it would be preferred to do your own second suggestion

var app = new Outlook.Application();

Since this is a Outlook VSTO add-in there is zero risk of starting Outlook since it by definition will be running. If you use this method from within another Office application you would start Outlook only if Outlook is not currently running.

For completeness sake, all other shared models should use a form of Dependency Injection so there is no strong coupling in your code.

Using these two approaches it would also be easier (possible) for you to refactor your code and other classes out into a separate library for easier reuse across your VSTO projects or even a Nuget Package.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号