Home > Microsoft Office Automation > Disable Password Prompts In Microsoft Word and Excel Automation

Disable Password Prompts In Microsoft Word and Excel Automation

It’s not an easy task to automate MS Office, and especially to run it as a service or within a ASP.Net web site, but it is possible and with a little bit of hard work and research you can actually have a very stable interaction.  This is the first of my many "things to note" and "gotcha’s" I found when dealing with MS Office Automation, and it’s a long list.

Disabling Password Prompts in MS Word and Excel Automation, seemed to be a hot topic in a lot of forums with few insights to an answer, but here is the trick that I figured out.

First you have to accept that fact that you likely aren’t going to be able to open the password protected file, and that Word or Excel is going to fail the open request, so you have a COM Exception to catch.  The nice thing is that the Error Code returned from the exception is unique for password issue so you can catch it specifically and deal with it (either tell the user or simply ignore it, whatever you want).  Again I will post a sample with the codes soon, but if you try to open a password protected file and hit cancel you will see the exception returned so you can get the code from there.

Now onto the second and most important point, the brilliant engineers at Microsoft figured that if a document isn’t password protected then there is no point in even trying to use any password supplied.  So basically… enter a garbage password into the various password fields (something around 8 chars seems fine; too many though and it causes another exception all the time).  Both word and Excel behave the same way, only using the password when the document is protected, and so normal documents will open fine since they won’t use the password, and documents that require a password will use the garbage one you provided instead of prompting, and will simply fail with ‘Invalid password’ which you can catch and handle.

   1:  const uint COM_CODE_BADPASSWORD = 0x800A1520;
   2:  const string OPEN_OPTION_MOCK_PASSWORD = "GARBAGE1";
   3:   
   4:  object missingValue = Type.Missing;
   5:  object fileNameObject = fileName;
   6:  object confirmConversions = false;
   7:  object openAsReadOnly = true;
   8:  object addToRecentFiles = false;
   9:  object passwordDocumentObject = OPEN_OPTION_MOCK_PASSWORD;
  10:  object passwordTemplateObject = OPEN_OPTION_MOCK_PASSWORD;
  11:  object revertObject = missingValue;
  12:  object writePasswordDocumentObject = OPEN_OPTION_MOCK_PASSWORD;
  13:  object writePasswordTemplateObject = OPEN_OPTION_MOCK_PASSWORD;
  14:  object formatObject = missingValue;
  15:   
  16:  object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
  17:   
  18:  try
  19:  {
  20:      // Using OpenOld is documented to be more compatible with older versions of Word.
  21:      // Word only tries the password if it needs it.
  22:      Document document = wordApplication.Documents.OpenOld(
  23:          ref fileNameObject,
  24:          ref confirmConversions,
  25:          ref openAsReadOnly,
  26:          ref addToRecentFiles,
  27:          ref passwordDocumentObject,
  28:          ref passwordTemplateObject,
  29:          ref revertObject,
  30:          ref writePasswordDocumentObject,
  31:          ref writePasswordTemplateObject,
  32:          ref formatObject
  33:          );
  34:   
  35:      // Do stuff with the document.
  36:   
  37:      document.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
  38:   
  39:  }
  40:  catch (COMException ex)
  41:  {
  42:      if (((uint) comException.ErrorCode) == COM_CODE_BADPASSWORD)
  43:      {
  44:          Debug.WriteLine("Detected a password protected Word document.");
  45:      }
  46:      else
  47:      {
  48:          throw;
  49:      }
  50:  }

You may ask, that’s fine for Word and Excel, but what about the other office apps… well unfortunately I haven’t tried Publisher or Access since I didn’t have need at the time, but I did learn that PowerPoint doesn’t expose any way to enter a password for opening the presentation, so the old Dialog Killer thread was the only resolution we had, looking for visible dialog windows with a particular name and sending them a WM_CLOSE message (I say ‘visible’ dialog since we were also dealing with Acrobat which keeps invisible dialog boxes with the particular name we were monitoring, and visible only seems to work 100% of the time).

My next post will be on using the Automatic Recovery options to open documents and some very frustrating issues I had with those.

  1. No comments yet.
  1. No trackbacks yet.

Leave a comment