How to remove password from Word Document in c#

Отвечено How to remove password from Word Document in c#

  • 31 июля 2012 г. 18:26
     
     

    Dear All,

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

                please help

                thanks in advance

       


Все ответы

  • 31 июля 2012 г. 18:32
     
     

    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.

  • 1 августа 2012 г. 4:56
     
      С кодом

    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


  • 1 августа 2012 г. 6:33
     
     Отвечено С кодом

    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

    • Помечено в качестве ответа Harish Pandey 12 февраля 2013 г. 7:38
    •  
  • 1 августа 2012 г. 19:00
     
     

    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

  • 9 августа 2012 г. 7:38
     
     

    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.

  • 28 сентября 2012 г. 19:16
     
     
    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