How can I tell if a workbook is password-protected using VBA without actually opening it?

serbach

New Member
Joined
May 22, 2018
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Our company has > 65,000 workbooks on our shared file server. I'm working on an Excel VBA routine that documents ODBC and Workbook Connections, external Excel Links, Power Queries, etc., for each workbook and write it all out to a text file. It actually works pretty darned well... except when it comes to workbooks with passwords.

As I loop through the workbooks in a given folder, I hope to be able to record that a particular workbook is password-protected and skip to the next file. However, the VBA routine has to pause while the Password dialog is displayed. There does not seem to be any way in VBA to tell when that dialog is on-screen and, more importantly, to Cancel it and move on to the next file.

I use PowerShell quite a bit. That's how I got the long list of all the Excel files on our shared drive in the first place. I found from my research that, apparently, if you examine the file metadata in Windows File Explorer (Properties / Details), a password-protected Excel file does not have the "Program name" property set; whereas un-protected Excel files have "Microsoft Excel" as the Program name.

OK, so I wondered if there's a cmdlet in PowerShell that would read that particular property of a file. Unfortunately, no, there isn't. So I found a couple guys who wrote custom PowerShell functions that use Windows System.IO calls to extract all of the metadata from a file, which includes all the usual properties you see in File / Info / Properties, plus all of the items that PowerShell cmdlets ignore.

Well, the custom function enumerates the metadata items for a file one by one in a loop. Then one has to search through the values returned to see if a "ProgramName" property exists. If it doesn't then that means the file is password-protected. [Whew!] This will take a while to execute!

My question: does anyone know of a VBA way to tell if an Excel workbook is password-protected before it opens? Or at least Cancel opening the file if the password prompt appears?

Thank you,

Steve Erbach
Green Bay, WI
(crying in my beer that the Pack fell to the Niners)
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Yes it is possible but takes much more code than a simple VBA code to open and check
 
Upvote 0
Yes it is possible but takes much more code than a simple VBA code to open and check
Right, I've experienced it!

But your reply suggests that one could "open and check"... or am I not understanding you? When I open a password-protected workbook in a VBA For Next loop on a folder full of workbooks, I get the Password dialog box. If I could detect that Excel was waiting for me to enter a password and somehow Cancel the dialog, I might be able to continue opening other workbooks. Do you know of a way to detect the Password prompt?

Thank you,

Steve Erbach
Green Bay, WI
 
Upvote 0
So I see you have couple of questions

  1. Can we tell if an Excel workbook is password-protected before it opens?
  2. Can we cancel the dialog box after it appears?
The answer to both the questions is YES.

Let me test few things before I can confirm which would be the best way to go forward in your case.
 
Upvote 0
Try supplying password:="". You'll get an error 1004 if the wb is password protected, which you can trap and move on.
 
Upvote 0
Solution
Try supplying password:="". You'll get an error 1004 if the wb is password protected, which you can trap and move on.
Oooo! That's a good idea! Thank you for that!

Sincerely,

Steve Erbach
Green Bay, WI
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top