none
Detecting (with VBA) whether a Word file is password-protected before opening RRS feed

  • Question

  • Hi

    I am writing a simple function that will trawl through a directory to make changes to the printer-settings in each *.doc* file, to avoid having to do it manually (approx 1,000 files to be processed).

    I don't want to open up read-only files (OK with that using GetAttr) but I also don't want to open up any files that have password protection on them.

    Is there a way to test (in Word 2007 VBA) whether a Word file has password-protection on it prior to opening it?

    Regards

    Simon


    • Edited by Simon Dobby Monday, September 3, 2012 10:21 AM clarified that it is Word files I am opening
    Monday, September 3, 2012 10:19 AM

Answers

All replies

  • Hi Simon,

    Thank you for posting in the MSDN Forum.

    It's based on my experience that you can use the ActiveDocument.ProtectionType, like this

    If ActiveDocument.ProtectionType <> wdNoProtection Then
     ActiveDocument.Unprotect
    End If

    Hope it helps.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 5, 2012 3:05 AM
    Moderator
  • Hi Leo

    I don't think that's the kind of password the OP is talking about. I believe he's referring to the kind where you get a prompt for the password when you try to open the document file.

    Pre-2007, with the old file format, you could assign a password as part of the Save As process. With the new file formats, a password is required when the document is encrypted.

    In the API that would be the Password and WritePassword parameters of the SaveAs method and the PasswordDocument and WritePassword parameters of the Open method.


    Cindy Meister, VSTO/Word MVP

    Wednesday, September 5, 2012 5:34 AM
    Moderator
  • Hi Simon,

    Thanks for posting in the MSDN Forum.

    I will mark Cindy's replay as answer. Please feel free to unmark it if you think it doesn't help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 12, 2012 2:54 AM
    Moderator
  • Hi Tom

    I don't think my reply to Leo is the Answer to the question asked. I was only trying to explain to Leo that he'd misunderstood the question.

    I haven't tried to answer Simon because I don't have an answer. I have a vague memory that, back in the days of the old binary file format it was possible to use an old tool (DSOFile, I think it was) to get that information from the document properties. But it's not a technique I ever used and I believe it stopped working with the new file formats. The tool is also no longer available for download, as far as I know.

    For files in the new file format I suppose Simon could use the Open XML SDK. If the file can't be processed by the SDK it's probably encrypted?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 13, 2012 11:47 AM
    Moderator
  • Hi Simon,

    The technique in http://www.word.mvps.org/FAQs/MacrosVBA/CheckIfPWProtectB4Open.htm still works; you'll just have to modify the extension used in the Dir function call to pick up both *.doc and *.doc* files.


    Jay Freedman
    MS Word MVP  FAQ: http://word.mvps.org


    Friday, September 14, 2012 2:14 PM
  • Hi Cindy,

    Thanks for you action, I will continue to look for it. Let's see whether we can find something for it.

    @Jay,

    Thanks for your great work.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Wednesday, September 19, 2012 7:36 AM
    Moderator
  • Hi Tom

    Jay's suggestion is what I was trying to remember and I think it's the only way (short of learning the old binary file formats) to do what Simon asks.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 19, 2012 8:53 AM
    Moderator
  • Hi Cindy,

    hmmm, I think what your said is correct. Thank you Cindy.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 24, 2012 6:54 AM
    Moderator