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

后端 未结 2 837
半阙折子戏
半阙折子戏 2021-01-04 01:07

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 ite

相关标签:
2条回答
  • 2021-01-04 01:20

    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 讨论(0)
  • 2021-01-04 01:29

    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

    0 讨论(0)
提交回复
热议问题