简体   繁体   English

Outlook加载项和Windows窗体协作

[英]Outlook Add-In and Windows Form collaboration

I'm developing an outlook add in that collaborates with SharePoint. 我正在开发与SharePoint合作的Outlook插件。 I've added a new item to the Outlook add in: a Windows Form. 我在Outlook中添加了一个新项:Windows窗体。 When a button is being clicked in the Windows Form, I want to perform an action with Outlook, like this: 在Windows窗体中单击按钮时,我要使用Outlook执行操作,如下所示:

Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");

This isn't working, because I'm in the Windows Form instead of in the Outlook Add In. 这不起作用,因为我使用的是Windows窗体而不是Outlook加载项。 I'm not sure how to let those though collaborate. 我不确定如何让他们合作。

Add a new parameter to your WinForm constructor, to receive a reference to the Outlook Application object: 将新参数添加到WinForm构造函数中,以接收对Outlook Application对象的引用:

using Outlook = Microsoft.Office.Interop.Outlook;
.
.
.
public partial class MyWinForm : Form
{
    private Outlook.Application m_OutlookApp;

    public MyWinForm(Outlook.Application OutlookApp)
    {
        m_OutlookApp = OutlookApp;

        InitializeComponent();
    }

Your add-in code to launch the form then becomes something like: 然后,用于启动表单的加载项代码将变为:

MyWinForm myWinForm = new MyWinForm(Application);

myWinForm.Show();

You can then use the Outlook Application within the WinForm: 然后,您可以在WinForm中使用Outlook应用程序:

private void button1_Click(object sender, EventArgs e)
{
    Outlook.NameSpace outlookNameSpace = m_OutlookApp.GetNamespace("MAPI");
    .
    .
    .
}

The Outlook Application object passed to the add-in during initialization is already intrinsically declared in the ThisAddIn class within your VSTO project. 在初始化期间传递给外接程序的Outlook Application对象已经在VSTO项目的ThisAddIn类中进行了内部声明。 You can access it from any other item in the project using Globals.ThisAddIn.Application . 您可以使用Globals.ThisAddIn.Application从项目中的任何其他项目访问它。 You must use this object and not a new Outloook Application object because it won't be fully trusted as it wasn't passed to the add-in initialization by Outlook. 您必须使用此对象,而不是新的Outloook Application对象,因为它不会被Outlook完全传递给外接程序初始化,因此不会受到完全信任。

I think you can use something like this below, 我认为您可以在下面使用类似的方法,

var output = Globals.YourAddin.Application.GetNamespace("MAPI");

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM