1

I wrote a program that search for a specific text in a word document, but my problem is that when it opens a read only file it shows a dialog message that says 'File xx is reserved by xx. Enter password for write access'. How can i prevent this message from showing ? I would like to skip this kind of files if i come across them. Can i maybe use OpenXml() or Stream to detect these kind of files ?

The try catch it's able to catch documents protected by password, but it can't catch readonly(password protected) documents. It opens the password dialog instead.

Here is my code below :

 public List<string> FindTexInWordDoc(string searchFor, string filePath)
 {
            List<string> foundMatches = new List<string>();
            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Document doc = null;

            try
            {
                app = new Microsoft.Office.Interop.Word.Application();
                object objPass = "1111111111111";  
                object missing = System.Type.Missing;
                object ReadOnly = false;  
                object visible = false;

                app.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;

                doc = app.Documents.Open(filePath, ref missing, ref ReadOnly, ref missing, ref objPass, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                string[] allWords = doc.Content.Text.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);

                for (int x = 0; x < allWords.Length; x++)
                {
                    if (allWords[x].ToLower().Contains(searchFor.ToLower()) == true)
                        foundMatches.Add(allWords[x]);

                    if (started == false)
                        break;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close();
                    Marshal.FinalReleaseComObject(doc);
                }

                if (app != null)
                {
                    app.Quit();
                    Marshal.FinalReleaseComObject(app);
                }
            }

            return foundMatches;
        }
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
chosenOne Thabs
  • 1,480
  • 3
  • 21
  • 39
  • 1
    Yes, save the document without a password –  Jan 14 '17 at 16:32
  • Or pass in the [PasswordDocument](https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.documents.open.aspx) parameter when opening. – Steve Greene Jan 14 '17 at 17:40
  • I specified the dummy password so that i can differentiate between password protected files and normal files. If the file is protected by a password then i will inform the user at the end of the process to remove the password on those files. I still get the password dialog even if i remove the password code (object objPass = "1111111111111") – chosenOne Thabs Jan 15 '17 at 10:10
  • you'll have trouble saving it , if you can't open it, so instead, test for pass first. check this http://stackoverflow.com/questions/13416434/open-word-document-unless-it-has-password-with-c-sharp ps - notice the "OpenNoRepairDialog" – Stavm Jan 31 '17 at 15:04
  • also since C# 4.0 you are no longer required to put those nasty ref missing on every office function. – Stavm Jan 31 '17 at 15:11
  • I'm able to open the document with OpenNoRepairDialog(), it crashes on this line : word.ActivePrinter. So how can i check read only password after opening the document ? – chosenOne Thabs Jan 31 '17 at 19:37

0 Answers0