Reputation: 1633
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.
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);
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
}
}
Thx in advance
EDIT: 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.
Upvotes: 1
Views: 4997
Reputation: 1
When I first tried it
word.Documents.OpenNoRepairDialog(@"c:\testfolder\Doc2.docx", ReadOnly: true, PasswordDocument: "RandomButSurelyNotRightPassword");
When I open a word without a password, the hint is unsuccessful. Then i found
"RandomButSurelyNotRightPassword"
is too long as a placeholder password...
Upvotes: 0
Reputation: 1633
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
}
}
Upvotes: 5