none
Automatically providing password to protected Word document via add-in

    Question

  • Is there a way to automatically supply a password to a protected word document using a add-in? I am trying to develop an add-in that will password protect a document with a standard password every time it is saved and to automatically open the document without the user having to enter the password.

    I am able to get the document to be password protected when saved very easily but can't figure out a way to supply that password when the document is opened.

    Any help would be greatly appreciated.

    Monday, September 17, 2012 6:48 PM

Answers

  • Hi Anurag

    In that case, it would probably be best to incorporate a Ribbon customization in your add-in. If this is a VSTO add-in it would have to be a Ribbon XML component, not Ribbon Designer.

    In the Ribbon XML you need to add a <commands> section that will re-purpose the command the user uses to open documents. That would be:

      <command idMso="FileOpen" onAction="ProcInYourRibbonClass" />

    In the procedure for onAction you'd put the code that should execute when the user choosed the "Open" command. In this case, you'd display a "file picker" dialog box, capture the selected file(s) then use Documents.Open to open the files for the user. That will let you specify the password.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 21, 2012 2:23 PM

All replies

  • Hi Anurag2012

    The Open method (WordApplication.Documents.Open) has parameters for providing a password when opening a password-protected document.

    If this isn't what you're looking for, please provide more information, including the VERSION of Word you're working with.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 18, 2012 9:32 AM
  • Hi Cindy,

    Thanks a lot for you reply.

    Actually what I am looking for is invoking my add-in when a user opens the password protected file and then automatically supplying the password. I tried hooking into the "DocumentOpen" event but that does not get called until I actually supply the password at the prompt.

    I basically want to get rid of the user prompt when the user opens the file.

    Thanks

    Friday, September 21, 2012 4:12 AM
  • Which version(s) of Word are we talking about?


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 21, 2012 7:51 AM
  • 2007 and 2010.

    Thanks

    Friday, September 21, 2012 1:38 PM
  • Hi Anurag

    In that case, it would probably be best to incorporate a Ribbon customization in your add-in. If this is a VSTO add-in it would have to be a Ribbon XML component, not Ribbon Designer.

    In the Ribbon XML you need to add a <commands> section that will re-purpose the command the user uses to open documents. That would be:

      <command idMso="FileOpen" onAction="ProcInYourRibbonClass" />

    In the procedure for onAction you'd put the code that should execute when the user choosed the "Open" command. In this case, you'd display a "file picker" dialog box, capture the selected file(s) then use Documents.Open to open the files for the user. That will let you specify the password.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, September 21, 2012 2:23 PM
  • You are awesome. That worked.

    Could you help me with one last thing where I would like to do the same (automatically provide the password) when a user double clicks on the document instead of going through the ribbon to open it.

    I really appreciate the help.
    Saturday, September 22, 2012 4:11 PM
  • Hi Anurag

    As far as I know, there's no way to do the same thing when the user double-clicks in Windows Explorer. Possibly, something would be possible with the Windows API, but that's not something I know much about, nor is it a topic covered in this forum.

    Perhaps you can find a forum where they can tell you how to monitor actions in Windows Explorer?


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, September 23, 2012 10:27 AM