如何从 VBA 代码打开受保护的工作簿?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15759981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:18:10  来源:igfitidea点击:

How to open a protected workbook from VBA code?

vbaexcel-vbaexcel-2007excel

提问by khaled Rihane

How to open a protected workbook(with password) from VBA code ? I already try this instruction

如何从 VBA 代码打开受保护的工作簿(带密码)?我已经尝试过这个指令

Workbooks.Open Filename:="D:\file.xls", ReadOnly:=False, Password:="semsem"And it doesn't work. Is there any suggestion about this Problem?
Note: I use EXcel2007.

Workbooks.Open Filename:="D:\file.xls", ReadOnly:=False, Password:="semsem"它不起作用。对这个问题有什么建议吗?
注:我使用EXcel2007。

回答by glh

In the vba project did you protect the project from viewing? If so I don't think this will be achievable as the project needs to be physically unprotected if you are wanting to do any code changes. This doesn't help from a vba programming point but from a user tamper free perspective it does.

在 vba 项目中你有没有保护项目不被查看?如果是这样,我认为这是无法实现的,因为如果您想要进行任何代码更改,则该项目需要在物理上不受保护。从 vba 编程角度来看,这没有帮助,但从用户无篡改的角度来看,它确实有帮助。

  1. If you do not intend on editing the code I can only suggest you leave the password out.

  2. If you intend to alter the code I can only suggest you leave the project unprotected and try to protect the file another way.

  3. Untestedbut an idea would be to programmatically create a sheet with the new code in it and copy this sheet into this workbook.

  1. 如果您不打算编辑代码,我只能建议您不要输入密码。

  2. 如果您打算更改代码,我只能建议您不要保护项目并尝试以另一种方式保护文件。

  3. 未经测试,但一个想法是以编程方式创建一个包含新代码的工作表,然后将此工作表复制到此工作簿中。

Not much help but the only answer I can offer.

没有多少帮助,但我能提供的唯一答案。

To manually remove the protection:

手动移除保护:

  1. If this is a once off, double click the project and enter the password and once finished, saved and closed the project will keep this protection. http://www.allaboutvba.vbapasswordremover.net/images/screen/protect-excelrun10.gif

  2. Or if it's to be permanent then right click the project>project properties>protection and uncheck lock project for viewing. http://www.allaboutvba.vbapasswordremover.net/images/screen/protect-excellock8.gif

  1. 如果这是一次关闭,双击项目并输入密码,一旦完成、保存和关闭项目将保持这种保护。 http://www.allaboutvba.vbapasswordremover.net/images/screen/protect-excelrun10.gif

  2. 或者,如果它是永久性的,则右键单击项目>项目属性>保护并取消选中锁定项目以进行查看。 http://www.allaboutvba.vbapasswordremover.net/images/screen/protect-excellock8.gif

Images from All About VBA

来自All About VBA 的图片

回答by KumaraPush

If a Workbook is password protected, it can be opened using VBA. But, If a VBA Macro is Password Protected, It cannot be opened or controlled from VBA. For security purpose this is not allowed.

如果工作簿受密码保护,则可以使用 VBA 打开。但是,如果 VBA 宏受密码保护,则无法从 VBA 打开或控制它。出于安全目的,这是不允许的。

One alternate option would be to pass KeyPress events to enter the password.

另一种选择是通过 KeyPress 事件来输入密码。

回答by Karthick Gunasekaran

Try the following:

请尝试以下操作:

Workbooks.Open Filename:="D:\file.xlsx", Password:="semsem"