在更改文本内部之前检测word文档是否受密码保护

时间:2016-07-14 14:42:57

标签: c# .net ms-word office-interop

我正在编写一个方法,它将进入目录查找所有doc文件并替换其中的特定文本。出于这个原因,我的方法接受三个参数。

  1. 目录路径
  2. 我要替换的字符串
  3. 新替换字符串
  4. 我面临的问题是当我点击受密码保护的文档时。在打开文档之前,我无法检查文档是否受到保护。在这种情况下,每次我检查文档时,我都会得到一个对话框,word窗口询问密码。我想检查文档是否受到保护,如果它只是继续使用foreach。

    这是我的代码:

    private static void ReplaceString(string folderPath, string findText, string replaceText)
    {
        // retrieve all doc files from the specified directory
        var wordFiles = Directory.GetFiles(folderPath, "*.doc", SearchOption.AllDirectories);
        var filtered = wordFiles.Where(f => !f.Contains('$'));
        foreach (var wordFilePath in filtered)
        {
            Console.WriteLine(wordFilePath);
            // start a new word application
            FileInfo fi = new FileInfo(wordFilePath);
            //  var wordDocument = new Document();
            //checking the current element if: is in use, is readonly, if is protected by password
            if (IsLocked(fi))
            {
                continue;
            }
            var wordApplication = new Application { Visible = false };
    
            //opening the word document
            Document wordDocument = null;
    // I want to catch here if the document is protected just to contonie forward
            try
            {
                wordDocument = wordApplication.Documents.Open(wordFilePath, ReadOnly: false, ConfirmConversions: false);
            }
            catch (COMException e)
            {
                continue;
            }
            //Unfolding all fields in a document using ALT + F9
            wordDocument.ActiveWindow.View.ShowFieldCodes = true;
    
            // using range class to populate a list of all document members
            var range = wordDocument.Range();
    
            try
            {
                range.Find.Execute(FindText: findText, Replace: WdReplace.wdReplaceAll, ReplaceWith: replaceText);
            }
            catch (COMException e)
            {
                continue;
            }
            // replace searched text
            var shapes = wordDocument.Shapes;
            foreach (Shape shape in shapes)
            {
                var initialText = shape.TextFrame.TextRange.Text;
                var resultingText = initialText.Replace(findText, replaceText);
                shape.TextFrame.TextRange.Text = resultingText;
            }
    
            // Show original fields without code 
            wordDocument.ActiveWindow.View.ShowFieldCodes = false;
    
            // save and close the current document
            wordDocument.Save();
            wordDocument.Close();
            wordApplication.NormalTemplate.Saved = true;
            wordApplication.Quit();
    
            // Release this document from memory.
            Marshal.ReleaseComObject(wordApplication);
        }
    }
    

1 个答案:

答案 0 :(得分:3)

Documents.Open()方法有一个PasswordDocument参数。如果您将其分配给随机密码(密码错误),则该方法将忽略您在文档未受密码保护时分配的密码。如果文档受密码保护,该方法将抛出5408异常,您可以捕获。

Source