locked
How to remove password from Word Document in c# RRS feed

  • Question

  • Dear All,

                i like to remove password from word document(.doc,.docx).

                please help

                thanks in advance

       


    Tuesday, July 31, 2012 6:26 PM

Answers

  • http://www.c-sharpcorner.com/Forums/Thread/61822/

    code from this link :

    	Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 
            object nullobj = System.Reflection.Missing.Value; 
            string pass = "abcd1234"; 
            object password = pass; 
            object FName = fileName; 
            Microsoft.Office.Interop.Word.Document aDoc = null; 
            wordApp.Visible = false; 
            aDoc = wordApp.Documents.Open(ref FName, ref nullobj, ref nullobj, ref nullobj, ref password, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); 
            aDoc.Activate(); 
            if (aDoc.HasPassword) 
            aDoc.Password = null; 
            if (aDoc.ProtectionType != wdProtectionType.wdNoProtection) 
            aDoc.Unprotect(ref password); 
            aDoc.SaveAs(ref FName, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); 
            aDoc.Close(ref nullobj, ref nullobj, ref nullobj); 

    regards

    joon

    • Marked as answer by Harish Pandey Tuesday, February 12, 2013 7:38 AM
    Wednesday, August 1, 2012 6:33 AM

All replies

  • You can't. And the request to do so sounds very suspicious. The best you can do is to check to see if the file is password protected.

    Tuesday, July 31, 2012 6:32 PM
  • using DocumentFormat.OpenXml.Packaging;

    using DocumentFormat.OpenXml.Wordprocessing; using (WordprocessingDocument wd = WordprocessingDocument.Open(filename, false)) { DocumentProtection dp = wd.MainDocumentPart.DocumentSettingsPart.Settings.GetFirstChild<DocumentProtection>(); if(dp != null && dp.Enforcement == BooleanValues.One && dp.Edit == DocumentProtectionValues.ReadOnly) { // Document is password protected for read only. } }

    Use like this

    Sivalingam


    Wednesday, August 1, 2012 4:56 AM
  • http://www.c-sharpcorner.com/Forums/Thread/61822/

    code from this link :

    	Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); 
            object nullobj = System.Reflection.Missing.Value; 
            string pass = "abcd1234"; 
            object password = pass; 
            object FName = fileName; 
            Microsoft.Office.Interop.Word.Document aDoc = null; 
            wordApp.Visible = false; 
            aDoc = wordApp.Documents.Open(ref FName, ref nullobj, ref nullobj, ref nullobj, ref password, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); 
            aDoc.Activate(); 
            if (aDoc.HasPassword) 
            aDoc.Password = null; 
            if (aDoc.ProtectionType != wdProtectionType.wdNoProtection) 
            aDoc.Unprotect(ref password); 
            aDoc.SaveAs(ref FName, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); 
            aDoc.Close(ref nullobj, ref nullobj, ref nullobj); 

    regards

    joon

    • Marked as answer by Harish Pandey Tuesday, February 12, 2013 7:38 AM
    Wednesday, August 1, 2012 6:33 AM
  • hi joon

    Thanks for your reply

    but when i implement this piece of code i got error

    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    now what should I do?

    thanks

    Wednesday, August 1, 2012 7:00 PM
  • Please have a look at the link(http://www.c-sharpcorner.com/Forums/Thread/61822/) which joon gave. This may helps. I copy it here:

    Hi All, 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. Is there something I am missing? Thanks in advance. Rals Apologies for the wierd display.

    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.

    • Proposed as answer by Lisa Zhu Friday, August 10, 2012 5:15 AM
    • Unproposed as answer by Lisa Zhu Friday, August 10, 2012 7:36 AM
    Thursday, August 9, 2012 7:38 AM
  • Harish, I hope it is your file, and you just forgot the password.  Basically, what are your intentions?  If your intentions are sound, I'll tell you how to do this.

    Ryan Shuell

    Friday, September 28, 2012 7:16 PM
  • 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
    Wednesday, July 24, 2013 1:21 PM