0

我正在通过 .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;
4

1 回答 1

0

我看到您正在使用Add-in Express

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

此外,您要确保释放您创建的 Office 对象

我建议更新代码如下:

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);
    }
于 2013-09-10T16:29:26.177 回答