简体   繁体   English

使用VBA从第一个密码保护第二个工作簿的宏

[英]Password-protect the macros of a second workbook from a first using VBA

I have a workbook, which creates a second workbook (using ThisWorkbook.SaveCopyAs ) to present the data nicely to users. 我有一个工作簿,它创建了第二个工作簿(使用ThisWorkbook.SaveCopyAs )以很好地向用户展示数据。 It relies on macros for part of the processing. 它依赖宏进行部分处理。

However, I've been asked to make those macros inaccessible to users. 但是,有人要求我使用户无法访问这些宏。 They still need to run them, but to view/edit them should require a password. 他们仍然需要运行它们,但是要查看/编辑它们,则需要输入密码。 I can figure out how to do it using the GUI (VBA Editor -> right click VBAProject -> VBAProject Properties -> Protection, tick the box and enter a password), but I haven't found a way to do so using VBA. 我可以弄清楚如何使用GUI(VBA编辑器->右键单击VBAProject-> VBAProject属性->保护,在框内打勾并输入密码),但是我还没有找到使用VBA的方法。 The Workbook.Protect function seems to lock down everything except VBA. Workbook.Protect函数似乎可以锁定 VBA 以外的所有内容。

If I try to apply it to the source workbook, I get "Can't perform operation since the project is protected", so that doesn't work either. 如果我尝试将其应用于源工作簿,则会收到“由于项目受保护而无法执行操作”,因此也不起作用。

I'm running 2010, but the workbook needs to be compatible with 2003, so no fancy new tricks. 我正在运行2010,但是该工作簿需要与2003兼容,因此没有新奇的花样。 :( :(

此方法描述了不使用sendkey http://www.standards.com/Office/SetVBAProjectPassword.html设置项目密码的方法

I had a similar problem. 我有一个类似的问题。 I needed excel code to populate user created new sheets, but I wanted to password protect the project. 我需要Excel代码来填充用户创建的新工作表,但是我想用密码保护该项目。 It would work great as long as I did not use password proection, but Excel will not allow it to generate code in the new sheets with password protection. 只要我不使用密码保护,它就可以很好地工作,但是Excel不允许它在具有密码保护的新工作表中生成代码。

The solution was interesting. 解决方案很有趣。 Give your project an easy name or initials . 给您的项目起一个简单的名字或缩写。 Something people can find with ease. 人们可以轻松找到一些东西。

You password protect your entire project and save the resulting file as an XLA (add on). 您可以用密码保护整个项目,并将结果文件另存为XLA(附加)。

Then UNprotect the workbook, rename the file (just in case you make a mistake) and strip it of all the code, modules, classes, forms, etc. Save the new stripped Excel File. 然后取消保护工作簿,重命名文件(以防万一您犯错了),并剥离所有代码,模块,类,窗体等。保存新的剥离的Excel文件。

Now open the new stripped file and add the xla in TOOLS/add ons. 现在打开新的剥离文件,并在工具/附加组件中添加xla。 Then go to the VBA editor, References and find your project wiht a new name, and link (you may have to save and reopen once to find under referneces). 然后转到VBA编辑器“参考”,并使用新名称查找项目并进行链接(您可能必须保存并重新打开一次才能在“参考”下查找)。

That's it. 而已。 the new file, empty of all code, will operate using the old code but you will have no access to it without a password. 新文件(不含所有代码)将使用旧代码运行,但是如果没有密码,您将无法访问它。 At the same time, the old code can place the code on the new worksheets as before since the new workbook Project is not password protected 同时,由于新工作簿项目没有密码保护,因此旧代码可以像以前一样将代码放置在新工作表上

What I have found is that if you change locations, your users may need to find the link in the references, but I am sure you can create a macro to find and link automatically (that macro, of course, would be visible to the world). 我发现,如果您更改位置,您的用户可能需要在引用中找到链接,但是我确信您可以创建一个宏来自动查找和链接(当然,该宏对世界是可见的)。 )。

I think this will solve any problem with protected code. 我认为这可以解决受保护代码的任何问题。

Good luck 祝好运

This post may be helpful to you. 这篇文章可能对您有帮助。 Note that in the comments it says you need to add vbeext1.olb . 请注意,在注释中说您需要添加vbeext1.olb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM