我正在使用“netoffice”库从 Word 文件中提取文本。这应该是自动化过程。
但是,当Word文件受密码保护时,会显示警报窗口,因此用户需要输入密码。由于这是自动化过程,用户无需输入密码,程序将在此停止。
如何检测单词文件是否受“netoffice”密码保护,如果不可能,如何禁用警报窗口的显示?
我尝试将 DisplayAlerts 设置为 WdAlertLevel.wdAlertsNone,但它不起作用。
以下代码将帮助您跳过受密码保护的文件:
int iFilesWithPassword = 0;
Factory.Initialize();
Application wordApplication = new NetOffice.WordApi.Application();
try
{
// Attempt to open existing document. If document is not password protected then
// passwordDocument parameter is simply ignored. If document is password protected
// then an error is thrown and caught by the catch clause the follows, unless
// password is equal to "#$nonsense@!"!
Document newDocument = wordApplication.Documents.Open(@"C:\Users\Giorgos\Desktop\myNextFile.doc",
confirmConversions: false,
addToRecentFiles: false,
readOnly: false,
passwordDocument: "#$nonsense@!");
// read text of document
string text = newDocument.Content.Text;
}
catch(Exception e)
{
Exception inner = e.InnerException;
if (inner != null && inner.InnerException != null)
{
inner = inner.InnerException;
string sErrorMessage = inner.Message;
if (sErrorMessage.Contains("The password is incorrect."))
{
iFilesWithPassword++;
}
}
}
finally
{
// close word and dispose reference
wordApplication.Quit();
wordApplication.Dispose();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)