问题描述
我要执行的操作是在Word文档的文件夹中运行,然后使用Tiff打印机将它们转换为.tif文件.问题是,如果我遇到一个带有密码的文档,它应该跳过该文档而不提示您输入密码,而所有这些都应该保留在后台.
The thing i am trying to do is run through folders of word documents and convert them to .tif files using a tiff printer in word. The problem is, if i run into a document, with a password it should skip the document without prompting to ask for password, it should all remain in background.
我可以看到Document类具有HasPassword属性,但是在打开文档之前无法对其进行检查.
I can see that the Document class have a HasPassword property, but it cannot be checked before the document is opened.
word.Documents.OpenNoRepairDialog(@"c:\testfolder\testDocWithPw.docx", ReadOnly: true);
我还尝试给密码设置一个emtpy参数,并尝试捕获一个错误代码.但是我必须按取消"到提示输入密码的提示.
I also tried to give password a emtpy parameter, to and try to catch a errorcode. But i have to press cancel to the prompt asking for password to ever get there.
Application word = new Application();
word.DisplayAlerts = WdAlertLevel.wdAlertsNone;
try
{
word.Documents.OpenNoRepairDialog(@"c:\testfolder\Doc2.docx", ReadOnly: true, PasswordDocument: "");
word.ActivePrinter = "TIFF Image Printer 10.0";
Doc.PrintOut(); //printout untested for now
Doc.Close(false);
}
catch (System.Runtime.InteropServices.COMException ex)
{
if (ex.ErrorCode == 0x12345678)
{
//skip file and log file name and position for review
}
}
提前谢谢
只是尝试使用错误的密码来输入密码,而我可以使用错误代码部分,而最好的部分是,当没有密码时,即使您给了密码,它也会打开文件.所以它基本上可以满足我的要求.在更糟糕的情况下,如果我不应该访问密码不正确的文档,我猜我应该不应该打开文档的某人的密码,则可以检查hasPassword属性.
Just tried to feed the password with wrong password, and i could use the errorcode part, and the best part is that, when there no password it will open the file even if u give it a password. So it basically does what i want.In worse case, that i guess someones password on a document that i shouldn't have opened, i can check on the hasPassword property, if i shouldn't have access to a poorly passworded document.
推荐答案
我自己回答这个问题,所以我没有悬而未决的问题.解决方法很简单,只要打开一个密码就给一个密码,如果您留下一个空字符串,就和不提任何问题一样.然后可能会捕获到com异常,并且可以处理它,但是我想这样做.
I answer this my self, so i don't have a unanswered question hanging.Solution was simple, just give a password when open one, if you leave a empty string, its the same as not giving a question. Then a com exception could be catched and could handle it however i wanted.
Application word = new Application();
word.DisplayAlerts = WdAlertLevel.wdAlertsNone;
try
{
word.Documents.OpenNoRepairDialog(@"c:\testfolder\Doc2.docx", ReadOnly: true, PasswordDocument: "RandomButSurelyNotRightPassword");
word.ActivePrinter = "TIFF Image Printer 10.0";
Doc.PrintOut(); //printout untested for now
Doc.Close(false);
}
catch (System.Runtime.InteropServices.COMException ex)
{
if (ex.ErrorCode == 0x12345678)
{
//skip file and log file name and position for review
}
}
这篇关于打开Word文档,除非它具有C#密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!