Tuesday, September 25, 2012

Check Password of a Word Document Automatically

After I explained the basics about including Microsoft Word in the previous post, I today want to put special attention to the opening of password protected documents.
With the following code it can be checked, whether a password is the right one to open a document. By iteration then for example can be automatically searched for a password:

            Object oMissing = System.Reflection.Missing.Value;

            Word.Application WordApp = new Word.Application();

            try
            {
                Word.Document ExistingDocument = WordApp.Documents.Open("Filename", oMissing, false, oMissing, "Password");
                MessageBox.Show("Correct Password");
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                MessageBox.Show("Wrong Password");
            }

No comments:

Post a Comment