Eugene Astafiev

How To: Remove a Personal Folders file from the current Outlook profile

In my previous post we discussed how to add a new personal folders store to the Outlook profile. Now I would like to reveal the opposite action – how to remove a personal folders store. As usually I’ll show code examples for those who develop with and without Add-in Express.

The Outlook Object Model provides the RemoveStore method of the Namespace class for this task. It accepts an instance of the Folder class which represents a root folder of the store you need to remove. Be aware, if you develop a version-neutral Outlook add-in you will not find this method in the list of available methods of the Namespace class. The fact is that the RemoveStore method was introduced with Outlook 2002 (XP). Now you may ask me: “How can we handle this?“. The late-binding technology will do the trick. For example, you can use the following code to bridge the gap:

C#:

ns.GetType().InvokeMember("RemoveStore", System.Reflection.BindingFlags.Instance | 
                                          System.Reflection.BindingFlags.InvokeMethod | 
                                          System.Reflection.BindingFlags.Public,
                                          null, ns, new object[] { folder});

VB.NET:

ns.GetType().InvokeMember("RemoveStore", System.Reflection.BindingFlags.Instance Or _
                                         System.Reflection.BindingFlags.InvokeMethod Or _
                                         System.Reflection.BindingFlags.Public, _
                                         Nothing, ns, New [Object]() {folder})

So far, so good… But what if an end user still uses Outlook 2000?

Unfortunately Outlook 2000 doesn’t provide any method to remove a store. I have tried to get the root folder of the personal folders store and call the Delete method against it. But I always get an exception which is shown on the screenshot:

You may get such an error message when the Delete method of the Folder class is called.

As you may see there is no trivial way to delete a personal folders store in Outlook 2000. As a workaround you may use a low-level code (such as Extended MAPI) or any third-party libraries.

Below you will find sample codes in C# and VB.NET which illustrate how to use the RemoveStore method. Please pass an instance of the Application class and the name of the personal folders store (the name of its root folder) to the RemovePersonalFoldersStore method to run it correctly. The code iterates over all root folders and, when a match is found, removes it from the current profile.

C# & Add-in Express:

private void RemovePersonalFoldersStore(Outlook._Application OutlookApp, string storeName)
{
    Outlook.NameSpace ns = null;
    Outlook.Folders rootFolders = null;
    Outlook.MAPIFolder folder = null;
    try
    {
        ns =OutlookApp.GetNamespace("MAPI");
        rootFolders = ns.Folders;
        for (int i = 1; i < = rootFolders.Count; i++)
        {
            folder = rootFolders[i];       
            if(folder!=null)
            {
                if (folder.Name == storeName)
                {
                    // ns.GetType().InvokeMember("RemoveStore", 
                    //    System.Reflection.BindingFlags.Instance | 
                    //    System.Reflection.BindingFlags.InvokeMethod | 
                    //    System.Reflection.BindingFlags.Public,
                    //    null, ns, new object[] { folder});
                    ns.RemoveStore(folder);
                    break;
                 }
                 Marshal.ReleaseComObject(folder);
                 folder = null;
            }
        }
    } 
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (folder != null) Marshal.ReleaseComObject(folder);
        if (rootFolders != null) Marshal.ReleaseComObject(rootFolders);
        if (ns != null) Marshal.ReleaseComObject(ns);
    }
}

C# & VSTO:

using System.Runtime.InteropServices;
using System.Windows.Forms;
//
private void RemovePersonalFoldersStore(Outlook.Application Application, string storeName)
{
    Outlook.NameSpace ns = null;
    Outlook.Folders rootFolders = null;
    Outlook.MAPIFolder folder = null;
    try
    {
        ns = Application.GetNamespace("MAPI");
        rootFolders = ns.Folders;
        for (int i = 1; i < = rootFolders.Count; i++)
        {
            folder = rootFolders[i];
            if (folder != null)
            {
                if (folder.Name == storeName)
                {
                    // ns.GetType().InvokeMember("RemoveStore", 
                    //    System.Reflection.BindingFlags.Instance | 
                    //    System.Reflection.BindingFlags.InvokeMethod | 
                    //    System.Reflection.BindingFlags.Public,
                    //    null, ns, new object[] { folder});
                    ns.RemoveStore(folder);
                    break;
                }
                Marshal.ReleaseComObject(folder);
                folder = null;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (folder != null) Marshal.ReleaseComObject(folder);
        if (rootFolders != null) Marshal.ReleaseComObject(rootFolders);
        if (ns != null) Marshal.ReleaseComObject(ns);
    }
}

VB.NET & Add-in Express:

Private Sub RemovePersonalFoldersStore(ByRef OutlookApp As Outlook._Application, _
                                                        ByRef storeName As String)
    Dim ns As Outlook.NameSpace = Nothing
    Dim rootFolders As Outlook.Folders = Nothing
    Dim folder As Outlook.MAPIFolder = Nothing
    Try
        ns = OutlookApp.GetNamespace("MAPI")
        rootFolders = ns.Folders
        For i As Integer = 1 To rootFolders.Count Step 1
            folder = rootFolders.Item(i)
            If Not IsNothing(folder) Then
                If (folder.Name = storeName) Then
                    ' ns.GetType().InvokeMember("RemoveStore", _
                    '    System.Reflection.BindingFlags.Instance Or _
                    '    System.Reflection.BindingFlags.InvokeMethod Or _
                    '    System.Reflection.BindingFlags.Public, _
                    '    Nothing, ns, New [Object]() {folder})
                    ns.RemoveStore(folder);
                    Exit For
                End If
                Marshal.ReleaseComObject(folder)
                folder = Nothing
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
        If Not IsNothing(folder) Then Marshal.ReleaseComObject(folder)
        If Not IsNothing(rootFolders) Then Marshal.ReleaseComObject(rootFolders)
        If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns)
    End Try
End Sub

VB.NET & VSTO:

Imports System.Runtime.InteropServices
Imports System.Windows.Forms
'
Private Sub RemovePersonalFoldersStore(ByRef Application As Outlook.Application, _
                                                        ByRef storeName As String)
    Dim ns As Outlook.NameSpace = Nothing
    Dim rootFolders As Outlook.Folders = Nothing
    Dim folder As Outlook.MAPIFolder = Nothing
    Try
        ns = Application.GetNamespace("MAPI")
        rootFolders = ns.Folders
        For i As Integer = 1 To rootFolders.Count Step 1
            folder = rootFolders.Item(i)
            If Not IsNothing(folder) Then
                If (folder.Name = storeName) Then
                    ' ns.GetType().InvokeMember("RemoveStore", _
                    '     System.Reflection.BindingFlags.Instance Or _
                    '     System.Reflection.BindingFlags.InvokeMethod Or _
                    '     System.Reflection.BindingFlags.Public, _
                    '     Nothing, ns, New [Object]() {folder})
                    ns.RemoveStore(folder);
                    Exit For
                End If
                Marshal.ReleaseComObject(folder)
                folder = Nothing
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
        If Not IsNothing(folder) Then Marshal.ReleaseComObject(folder)
        If Not IsNothing(rootFolders) Then Marshal.ReleaseComObject(rootFolders)
        If Not IsNothing(ns) Then Marshal.ReleaseComObject(ns)
    End Try
End Sub

See you on our forums and in the e-mail support!

Post a comment

Have any questions? Ask us right now!