Turning off Document Recovery

I have had this request a couple of times. At times you may find a need to turn off the Document Recovery for a specific document. You may not want it to appear in the Document Recovery Pane if for some reason Word fails while a user is editing a specific type of document.

To turn off Document Recovery, you essentially need to find your document in the Registry after it is opened and remove it. The following Code Sample does this with a Extension method:

 
/// <summary>
/// EXTENSION METHOD CLASS
/// To make it easier to turn off resiliency on a document by document basis
/// </summary>
public static class WordDocumentExtensionMethods
{
    /// <summary>
    /// WORD DOCUMENT EXTENSION METHOD
    /// Turns off resiliency for the current docuent by deleting the
    /// registry key for it.
    /// </summary>
    /// <param name="doc"></param>
    public static void DisableResiliency(this Word.Document doc)
    {
        DeleteResiliencyKey(doc.FullName);
    }

    /// <summary>
    /// Private method that actually reads the registry, locates a specific
    /// resiliency key and then deletes it
    /// </summary>
    /// <param name="path"></param>
    private static void DeleteResiliencyKey(string path)
    {
        // This is the base path for the resiliency key in Word 2010
        string basePath = @"Software\Microsoft\Office\14.0\Word\Resiliency\DocumentRecovery";
        RegistryKey key = Registry.CurrentUser.OpenSubKey(basePath, true);
        if (key == null)
            return; // no key is present at this time
        // now loop through all the subkeys
        foreach (string k in key.GetSubKeyNames())
        {
            // now look in each sub-key and get the value by the same name
            RegistryKey subKey = Registry.CurrentUser.OpenSubKey(basePath + "\\" + k);
            // the values are binary, so we need to convert to a string
            byte[] o = (byte[])subKey.GetValue(k);
            System.Text.Encoding encoding = new System.Text.UnicodeEncoding();
            string keyVal = encoding.GetString(o);
            // now we only need to see if the path is in the string and if it
            // is, we have found the resiliency key that Word created for this
            // file automatically. Delete it...
            if (keyVal.Contains(path))
                key.DeleteSubKey(k);
        }
    }
}
To turn off the Document Recovery for a specific document, you can attach to the DocumenOpen event and then determine if the document is one that you want to turn off Recovery for. You can then execute the following line of code:
 
/// <summary>
/// DOCUMENT OPEN EVENT
/// </summary>
/// <param name="Doc"></param>
void Application_DocumentOpen(Word.Document Doc)
{
    // ... determine if the document is a member of the solution
    // and then just turn it off...
    Doc.DisableResiliency(); // call extension method
}

3 thoughts on “Turning off Document Recovery”

    1. GNU All-Permissive:

      Copying and distribution of this code, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This code is offered as-is, without any warranty.

Leave a Reply to mcCancel reply