1

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.

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
  • This post will give an [idea](http://stackoverflow.com/questions/6262104/how-can-i-see-if-a-word-file-is-password-protected) – bonCodigo Nov 16 '12 at 12:27
  • It also depends on whether you're using the later .docx file formats or the older .doc type. – Lee Taylor Nov 16 '12 at 12:33
  • bonCodigo, that is what i attempted with the try catch method :), Lee Taylor can u explain, what will the difference be between doc and docx in regards of password. – Henrik Bøgelund Lavstsen Nov 16 '12 at 12:53
  • @HenrikBøgelundLavstsen See the first answer on this page http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/34701a34-f1d4-4802-9ce4-133f15039c69 – Lee Taylor Nov 16 '12 at 13:18
  • Truly I am still adoring ms 2003. Never want to use 2007. But I don't mind 2010. Found [this](http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/991af08f-d80e-4604-828d-d54dde8fc7bf) guessing this is what Taylor was hintting :-) – bonCodigo Nov 16 '12 at 13:28
  • @LeeTaylor Thx, that seems like a bit more work for a "simple" task, but thx anyway. I am gonna test with more documents later, stuck in a different issue right now. – Henrik Bøgelund Lavstsen Nov 16 '12 at 13:34

2 Answers2

5

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
    }
}
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
0

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...

yubo
  • 1