Answered by:
How to remove password from Word Document in c#

Question
-
Dear All,
i like to remove password from word document(.doc,.docx).
please help
thanks in advance
- Edited by Harish Pandey Tuesday, July 31, 2012 6:34 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
All replies
-
-
Use like thisusing 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. } }
Sivalingam
- Edited by Sivalingam Ramasamy 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
-
-
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.
-
-