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)
 
By the way, this is a stunningly good message board. I had no idea when I posted my (very) nerdy question that I'd get so many responses so quickly. Thank you all! And thank you Mr. Excel!

Sincerely,

Steve Erbach
Green Bay, WI
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,224,819
Messages
6,181,153
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