none
How to password protect a MS Word .doc or .docx file using Microsoft Word Library? RRS feed

  • Question

  • I have been trying this code to password protect a MS Word file.

     public static void PasswordProtectDocument(string fileName, string docPassword)
        {
            Application wordAppwordApp = new Application();
            Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object readOnly = false;
            object visible = true;
            object password = docPassword;
            object fileToOpen = fileName;  //fileName is name of file with its full path
            try
            {
                doc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing,
                                                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                                ref visible, ref visible, ref missing, ref missing, ref missing);
                doc.Activate();

                doc.SaveAs(ref fileToOpen, ref missing, ref missing, ref missing, ref missing, ref password, ref missing, ref missing,
                           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            catch (Exception ex)
            {
                
            }
            finally
            {
                doc.Close(ref missing, ref missing, ref missing);
                wordApp.Quit(ref missing, ref missing, ref missing);
            }
        }

    It is doing also the same but while opening it manually, It is giving this type of dialog box.If we enter the password and click on OK,it is displaying the content.On clicking Cancel,its not displaying anything.But Now the problem is that on clicking Read Only,it is also displaying the content in a read only mode.

    This defeats the whole purpose of password protecting a file.So,Instead I want the dialog box  not to contain any read only option while opening a password protected MS Word file.

    I guess the answer is within the parameters of the Save As or Open function.Kindly help me in completely password protecting a file.

    • Moved by CoolDadTx Wednesday, October 23, 2013 2:26 PM Office related
    Wednesday, October 23, 2013 9:47 AM

Answers

  • Here is an example that might show you the way:

    // C#
    // Save the document. In a real application,
    // you'd want to test to see if the file
    // already exists. This will overwrite any previously 
    // existing documents.
    Object fileName = @"C:\Test\MyNewDocument.doc";
    Object fileFormat = Type.Missing;
    Object lockComments = Type.Missing;
    Object password = Type.Missing;
    Object addToRecentFiles = Type.Missing;
    Object writePassword = Type.Missing;
    Object readOnlyRecommended = Type.Missing;
    Object embedTrueTypeFonts = Type.Missing;
    Object saveNativePictureFormat = Type.Missing;
    Object saveFormsData = Type.Missing;
    Object saveAsAOCELetter = Type.Missing;
    Object encoding = Type.Missing;
    Object insertLineBreaks = Type.Missing;
    Object allowSubstitutions = Type.Missing;
    Object lineEnding = Type.Missing;
    Object addBiDiMarks = Type.Missing;
    
    ThisDocument.SaveAs(ref fileName, ref fileFormat, ref lockComments, 
      ref password, ref addToRecentFiles, ref writePassword, 
      ref readOnlyRecommended, ref embedTrueTypeFonts, 
      ref saveNativePictureFormat, ref saveFormsData, 
      ref saveAsAOCELetter, ref encoding, ref insertLineBreaks, 
      ref allowSubstitutions, ref lineEnding, ref addBiDiMarks);
    

    You see the differences especially int the use of the 'SaveAs()' method to your own code. For further details, see Understanding the Word Object Model from a .NET Developer's Perspective.

    There you'll also find an example on how to open such a password protected word document file.

    Kind regards,

    wizend

    Wednesday, October 23, 2013 10:26 AM
  • Hi Geni

    Wizend has put you on the right track. Read the Language Reference entry for the SaveAs command carefully. Word has different kinds of password protection - only one of those "locks" the file against opening. You're not assigning the password to the correct type of protection to not allow the document to be opened. You're only preventing anyone from overwriting the original file. That's why the document can still be opened Read-Only.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 24, 2013 2:03 PM
    Moderator

All replies

  • Here is an example that might show you the way:

    // C#
    // Save the document. In a real application,
    // you'd want to test to see if the file
    // already exists. This will overwrite any previously 
    // existing documents.
    Object fileName = @"C:\Test\MyNewDocument.doc";
    Object fileFormat = Type.Missing;
    Object lockComments = Type.Missing;
    Object password = Type.Missing;
    Object addToRecentFiles = Type.Missing;
    Object writePassword = Type.Missing;
    Object readOnlyRecommended = Type.Missing;
    Object embedTrueTypeFonts = Type.Missing;
    Object saveNativePictureFormat = Type.Missing;
    Object saveFormsData = Type.Missing;
    Object saveAsAOCELetter = Type.Missing;
    Object encoding = Type.Missing;
    Object insertLineBreaks = Type.Missing;
    Object allowSubstitutions = Type.Missing;
    Object lineEnding = Type.Missing;
    Object addBiDiMarks = Type.Missing;
    
    ThisDocument.SaveAs(ref fileName, ref fileFormat, ref lockComments, 
      ref password, ref addToRecentFiles, ref writePassword, 
      ref readOnlyRecommended, ref embedTrueTypeFonts, 
      ref saveNativePictureFormat, ref saveFormsData, 
      ref saveAsAOCELetter, ref encoding, ref insertLineBreaks, 
      ref allowSubstitutions, ref lineEnding, ref addBiDiMarks);
    

    You see the differences especially int the use of the 'SaveAs()' method to your own code. For further details, see Understanding the Word Object Model from a .NET Developer's Perspective.

    There you'll also find an example on how to open such a password protected word document file.

    Kind regards,

    wizend

    Wednesday, October 23, 2013 10:26 AM
  • Hi Geni

    Wizend has put you on the right track. Read the Language Reference entry for the SaveAs command carefully. Word has different kinds of password protection - only one of those "locks" the file against opening. You're not assigning the password to the correct type of protection to not allow the document to be opened. You're only preventing anyone from overwriting the original file. That's why the document can still be opened Read-Only.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 24, 2013 2:03 PM
    Moderator
  • Hi Cindy,

    Thanx!

    I could understand that from wizend's posted link but i could not figure it out correctly what code to use.Can you please tell me the solution to my problem?

    Friday, October 25, 2013 9:29 AM
  • Hi Geni

    Look at the code sample wizend posted, paying special attention to the variable password and its position as method argument (as opposed to writepassword, which is what you were using)


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 25, 2013 1:35 PM
    Moderator
  • Hi Cindy,

    Thanx!
    The part you pointed out was quite correct.I got though this.Problem Solved.

    But my new concern is that can we unprotect a password protected doc or docx file though C#.I now want to remove the password protection from the doc(x) files.I tried this but its failing to bring about any changes in the file nature.

     Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            object nullobj = System.Reflection.Missing.Value;
            string pass = Password;
            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;      
            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);


    genius sanjog

    Thursday, October 31, 2013 4:22 AM