Detecting (with VBA) whether a Word file is password-protected before opening
-
Monday, September 03, 2012 10:19 AM
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 03, 2012 10:21 AM clarified that it is Word files I am opening
All Replies
-
Wednesday, September 05, 2012 3:05 AMModerator
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 05, 2012 5:34 AMModerator
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
- Marked As Answer by Tom_Xu_WXModerator Wednesday, September 12, 2012 2:54 AM
- Unmarked As Answer by Cindy Meister MVPMVP, Moderator Thursday, September 13, 2012 11:47 AM
-
Wednesday, September 12, 2012 2:54 AMModerator
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
-
Thursday, September 13, 2012 11:47 AMModerator
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
-
Friday, September 14, 2012 2:14 PM
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- Edited by Jay Freedman MVPMVP Friday, September 14, 2012 2:14 PM link the URL
- Proposed As Answer by Cindy Meister MVPMVP, Moderator Friday, September 14, 2012 5:41 PM
- Marked As Answer by Tom_Xu_WXModerator Monday, September 24, 2012 6:54 AM
-
Wednesday, September 19, 2012 7:36 AMModerator
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
- Edited by Tom_Xu_WXModerator Wednesday, September 19, 2012 7:37 AM
-
Wednesday, September 19, 2012 8:53 AMModerator
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
-
Monday, September 24, 2012 6:54 AMModerator
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

