打开word文档时不要显示只读对话框

时间:2017-01-14 16:20:55

标签: c#

我编写了一个搜索word文档中特定文本的程序,但我的问题是 当它打开一个只读文件时,它会显示一条对话框消息,说明&x;文件xx由xx保留。输入密码以进行写访问'。如何防止此消息显示?如果遇到它们,我想跳过这种文件。我可以使用OpenXml()或Stream来检测这些类型的文件吗?

尝试抓住它能够捕获受密码保护的文档,但它无法捕获只读(密码保护)的文档。它会打开密码对话框。

以下是我的代码:

 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;
        }

0 个答案:

没有答案