在outlook |中创建文件夹。净

时间:2013-09-10 13:24:07

标签: outlook outlook-addin

我通过.net应用程序在microsoft outlook中创建文件夹但是当我重新启动outlook时它才会显示。应该显示新文件夹而不重新启动Outlook。 我使用下面的代码:

       Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI");
        Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder               
       (Outlook.OlDefaultFolders.olFolderInbox);
        Outlook.Folders inboxFolders = folderInbox.Folders;
        Outlook.MAPIFolder subfolderInbox = null;

        Outlook.Application oApp;
        oApp = nameSpace.Application;
        Outlook.Explorer exp = oApp.ActiveExplorer();

         subfolderInbox = inboxFolders.Add("InboxSubfolder",  
         Outlook.OlDefaultFolders.olFolderInbox);
         exp.CurrentFolder = subfolderInbox;

1 个答案:

答案 0 :(得分:0)

我发现你正在使用Add-in Express

我将您的代码附加到adxRibbonButton控件,它对我有用 你怎么称这个代码?
我看到一些不必要的对象创建,并在下面的编辑版本中删除它们。

另外 - you want to make sure to release the Office objects you create

我建议按如下方式更新代码:

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.NameSpace nameSpace = OutlookApp.Session; //OutlookApp.GetNamespace("MAPI");

        Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder
       (Outlook.OlDefaultFolders.olFolderInbox);
        Outlook.Folders inboxFolders = folderInbox.Folders;
        Outlook.MAPIFolder subfolderInbox = null;

        //Outlook.Application oApp;
        //oApp = nameSpace.Application;
        Outlook.Explorer exp = OutlookApp.ActiveExplorer(); //oApp.ActiveExplorer();

        subfolderInbox = inboxFolders.Add("InboxSubfolder",
            Outlook.OlDefaultFolders.olFolderInbox);
            exp.CurrentFolder = subfolderInbox;

        //Release COM Objects
        if (exp != null) Marshal.ReleaseComObject(exp);
        if (subfolderInbox != null) Marshal.ReleaseComObject(subfolderInbox);
        if (inboxFolders != null) Marshal.ReleaseComObject(inboxFolders);
        if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
        if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
    }