vba 检查 Excel 文件是否受密码保护
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2672964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether Excel file is Password protected
提问by Torben Klein
I am trying to open an Excel (xlsm) file via VBA. It may or may not be protected with a (known) password. I am using this code:
我正在尝试通过 VBA 打开 Excel (xlsm) 文件。它可能受也可能不受(已知)密码保护。我正在使用此代码:
On Error Resume Next
Workbooks.Open filename, Password:=user_entered_pw
opened = (Err.Number=0)
On Error Goto 0
Now, this works fine if the workbook has a password. But if it is unprotected, it can NOT be opened. Apparently this is a bug in XL2007 if there is also workbook structure protection active. (http://vbaadventures.blogspot.com/2009/01/possible-error-in-excel-2007.html). On old XL2003, supplying a password would open both unprotected and password protected file.
现在,如果工作簿有密码,这可以正常工作。但如果它不受保护,则无法打开。如果工作簿结构保护也处于活动状态,这显然是 XL2007 中的一个错误。(http://vbaadventures.blogspot.com/2009/01/possible-error-in-excel-2007.html)。在旧的 XL2003 上,提供密码将打开不受保护和受密码保护的文件。
I tried:
我试过:
Workbooks.Open filename, Password:=user_entered_pw
If (Err.Number <> 0) Then workbooks.open filename
This works for unprotected and protected file. However if the user enters a wrong password it runs into the second line and pops up the "enter password" prompt, which I do not want.
这适用于未受保护和受保护的文件。但是,如果用户输入错误的密码,它会运行到第二行并弹出“输入密码”提示,这是我不想要的。
How to get around this?
如何解决这个问题?
回答by Torben Klein
For the record - the reason was indeed the structure protection of the workbook I was opening. I could circumvent the problem by disabling structure protection, and re-protecting in Workbook_Open() (in the protected workbook).
作为记录 - 原因确实是我打开的工作簿的结构保护。我可以通过禁用结构保护并在 Workbook_Open()(在受保护的工作簿中)重新保护来规避这个问题。
With structure protection inactive, Workbooks.Open
with password does not fail even when there is no password.
结构保护未激活,Workbooks.Open
即使没有密码,密码也不会失败。
Since I am opening via a VBA method, the VBA code is already trusted, meaning the Workbook_Open method will surely be called.
因为我是通过 VBA 方法打开的,所以 VBA 代码已经是可信的,这意味着 Workbook_Open 方法肯定会被调用。
回答by user225626
Use 1004 error handling exclusively for this particular problem, and On Error Resume Next for everything else in the Sub.
Above the statement
Workbooks.Open filename, Password:=user_entered_pw
also add another line statement without a password argument.If those (or their combination) don't work, try scouring the .xlsm flat code in Notepad for any hint that a PW protected file differs at that level from one without. Use that info in a pre-opening function.
专门针对此特定问题使用 1004 错误处理,对 Sub 中的其他所有内容使用 On Error Resume Next。
在语句上方
Workbooks.Open filename, Password:=user_entered_pw
还添加了另一行没有密码参数的语句。如果这些(或它们的组合)不起作用,请尝试在记事本中搜索 .xlsm 平面代码以获取任何提示,即受 PW 保护的文件在该级别与没有该级别的文件不同。在预开盘功能中使用该信息。