Monday, January 28, 2013

Remove write/edit protection/enforcement from Microsoft word document from code.


This is not a kind of solution that I usually like bragging about the whole day. However as I couldn't find anything as such in the internet, I thought it would be nicer to add one more scarce resource to be available in the internet, if in case somebody else is looking for it.

Problem: How to remove a password from password enforced word document using c# code(or may be any other code) when you already have a password. Before getting in to the solution let's see what kind of password protection/enforcement I'm talking about.

To enforce write/edit protection in a word document you can simply follow the following steps.
  1. Create a new word document.
  2. Go to Review ribbon tab.
  3. Click on Protect Document.
  4. Check the Formatting restrictions if you want users not to be able to apply extra formatting on the document.
  5. Check the Allow only this type of editing in the document if you want users to be able to apply certain kind of changes to the document. To do so select the kind of changes you want to allow the user.
  6. Click on Yes, Start Enforcing Protection button.
  7. Save and close.



Disclaimer: Please note that this is not a cracking, not even closer to something that can be called cracking.

Solution:

public void RemovePasswordFromDocument(string documentPath, string password)
        {
            var app = new Application();
            app.Visible = true;
            object originalFile = documentPath;
            object oFalse = false;
            object missingObject = Missing.Value;
            try
            {
                object generatedPwd = password;
                var doc = app.Documents.Open(ref originalFile,
                                             ref missingObject, ref oFalse,
                                             ref missingObject,
                                             ref generatedPwd, ref generatedPwd,
                                             ref missingObject,
                                             ref generatedPwd, ref generatedPwd);

                if (doc.ProtectionType != WdProtectionType.wdNoProtection)
                {
                    doc.Unprotect(ref generatedPwd);
                }

                doc.Save();

            }
            finally
            {
                app.Documents.Close(ref oFalse, ref missingObject,
                                    ref missingObject);

                app.Quit(ref oFalse, ref oFalse, ref oFalse);
            }

        }

That's it. Code is pretty straightforward. Only thing you need to keep in mind is that your project needs to reference two assemblies Microsoft.Office.Interop.Word.dll and office.dll and these are something that are installed when you install Microsoft office application itself. So no worries.

16 comments:

  1. Hi, I use this above code to remove the password and unprotect my word document. But for some strange reason it works fine on some files but does not remove the password protection on some.Please Help

    ReplyDelete
    Replies
    1. Please note that this code deals with the kind of the protection that has been mentioned in the blog. Make sure the protection enforced in those documents are similar to the one described above.
      Also make sure the password is correct.

      Delete
  2. the code works correctly and it opens documents when I am running but when I open documents again it asks me password .... I want remove the password once and for all

    ReplyDelete
    Replies
    1. Are you sure you called the save method on the document? After unprotecting you need to save the document in order to unprotect it forever. Also I've updated some missing variables with correct values such as oMissing and oFalse which need to be Missing.Value and false objects. Please try it with this updated code.

      Delete
    2. Yes... I am sure I do it I am trying also this code http://www.c-sharpcorner.com/Forums/Thread/61822/
      he have the same problem but he says"got it. Word does not save any changes if it just has to do with password. One has to physically make some changes to the document for the Save doc to work. So just protected it again and then unprotected it and then saved it. Work fine now." Please Help me :'(

      Delete
    3. Can only help you if you send me your code and a sample word document.

      Delete
    4. ok I will do it but by mail or here ?

      Delete
  3. I work as follows: I have a directory that contains several word documents I have to browse its documents and delete passwords. the password depends on the name of documents example SS450.DOCX password SSdms450
    string path = "C:\\SC";

    foreach (string sFileName in System.IO.Directory.GetFiles(path))
    {
    if (System.IO.Path.GetExtension(sFileName) == ".docx")
    {

    string filenameNoExtension = Path.GetFileNameWithoutExtension(sFileName);
    string passw = filenameNoExtension.Substring(0, 2);
    if (filenameNoExtension.Length == 8)
    {

    filenameNoExtension = filenameNoExtension.Replace("_", ".");
    filenameNoExtension = filenameNoExtension.Substring(2, 6);

    }
    if (filenameNoExtension.Length == 5)
    {
    filenameNoExtension = filenameNoExtension.Substring(2, 3);
    }

    string chaine = "dms";
    passw += chaine;

    passw += filenameNoExtension;


    Console.WriteLine(passw);

    var app = new Application();
    app.Visible = true;
    object originalFile = sFileName;
    string password = passw;
    object missingObject = System.Reflection.Missing.Value;
    object oFalse = false;



    try
    {
    object generatedPwd = password;
    var doc = app.Documents.Open(ref originalFile,
    ref missingObject, ref oFalse,
    ref missingObject,
    ref generatedPwd, ref generatedPwd,
    ref missingObject,
    ref generatedPwd, ref generatedPwd);


    if (doc.ProtectionType != WdProtectionType.wdNoProtection)
    {
    doc.Unprotect(ref generatedPwd);
    object noReset = true;

    }

    doc.Save();

    }
    catch { }

    }

    ReplyDelete
    Replies
    1. Code seems perfect to me. Any exception? or could you point me to a sample file on which this doesn't work?

      Delete
    2. I tried with Word 2003 and 2010 the same problem it opens once without password then it asks me to enter the password again ....I want to get rid of password once and for all... I want my documents will not be opened with password

      Delete
    3. You mean write protection right, not file-open protection. The code is supposed to unprotect your document which is protected using write protection mechanism described above. It may not work properly for file-open protection that you set in general options.
      Really sorry couldn't be of much help here.
      May be you could try msdn forum.... I am sure some one will be able to figure out your problem. And don't forget to attach your word file(the one that doesn't work) .

      Delete
    4. Thx for you help I will try again with msdn forum :) :)

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thanks for this! I didn't know you could use code to do this, but I will be using this! Great way to protect your documents :)

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. About all Microsoft programs have a possibility for setting up various dimensions of passwords. remove windows password

    ReplyDelete