简体   繁体   English

锁定微软 Excel 宏

[英]Lock down Microsoft Excel macro

I've spent some time writing an Excel Macro that is potentially worth money to a lot of companies.我花了一些时间编写一个 Excel 宏,这对很多公司来说可能是物有所值的。

How can I lock down the macro to prevent people seeing the source / forwarding to other people?如何锁定宏以防止人们看到源/转发给其他人?

Thanks谢谢

Protect/Lock Excel VBA Code :保护/锁定 Excel VBA 代码

When we write VBA code it is often desired to have the VBA Macro code not visible to end-users.当我们编写 VBA 代码时,通常希望 VBA 宏代码对最终用户不可见。 This is to protect your intellectual property and/or stop users messing about with your code.这是为了保护您的知识产权和/或阻止用户弄乱您的代码。 Just be aware that Excel's protection ability is far from what would be considered secure.请注意,Excel 的保护能力远非安全。 There are also many VBA Password Recovery [tools] for sale on the www.也有许多 VBA 密码恢复 [工具] 在 www 上出售

To protect your code, open the Excel Workbook and go to Tools>Macro>Visual Basic Editor (Alt+F11).要保护您的代码,请打开 Excel 工作簿和 go 到工具>宏>Visual Basic 编辑器 (Alt+F11)。 Now, from within the VBE go to Tools>VBAProject Properties and then click the Protection page tab and then check "Lock project from viewing" and then enter your password and again to confirm it.现在,从 VBE go 到工具>VBAProject 属性,然后单击保护页面选项卡,然后选中“锁定项目以防查看”,然后输入您的密码并再次确认。 After doing this you must save, close & reopen the Workbook for the protection to take effect.完成此操作后,您必须保存、关闭并重新打开工作簿才能使保护生效。

(Emphasis mine) (强调我的)

Seems like your best bet.似乎是你最好的选择。 It won't stop people determined to steal your code but it's enough to stop casual pirates.它不会阻止人们决心窃取您的代码,但足以阻止临时盗版。

Remember, even if you were able to distribute a compiled copy of your code there'd be nothing to stop people decompiling it.请记住,即使您能够分发代码的编译副本,也没有什么可以阻止人们对其进行反编译

Just like you can password protect workbooks and worksheets, you can password protect a macro in Excel from being viewed (and executed).就像您可以密码保护工作簿和工作表一样,您可以密码保护 Excel 中的宏不被查看(和执行)。

Place a command button on your worksheet and add the following code lines:在您的工作表上放置一个命令按钮并添加以下代码行:

  1. First , create a simple macro that you want to protect.首先,创建一个您想要保护的简单宏。

    Range("A1").Value = "This is secret code"

  2. Next , click Tools , Then VBAProject Properties ...接下来,单击工具,然后单击VBAProject 属性...

在此处输入图像描述

Click Tools, VBAProject Properties...单击工具、VBA 项目属性...

  1. On the Protection tab , check " Lock project for viewing " and enter a password twice.在“保护”选项卡上,选中“锁定项目以供查看”并输入两次密码。

在此处输入图像描述

Enter a Password Twice输入两次密码

  1. Click OK .单击确定

  2. Save , close and reopen the Excel file.保存关闭并重新打开 Excel 文件。 Try to view the code.尝试查看代码。

在此处输入图像描述

The following dialog box will appear:将出现以下对话框:

Password Protected from being Viewed密码保护不被查看

You can still execute the code by clicking on the command button but you cannot view or edit the code anymore (unless you know the password).您仍然可以通过单击命令按钮来执行代码,但您不能再查看或编辑代码(除非您知道密码)。 The password for the downloadable Excel file is "easy".可下载的 Excel 文件的密码是“easy”。

  1. If you want to password protect the macro from being executed, add the following code lines:如果要密码保护宏不被执行,请添加以下代码行:
 Dim password As Variant password = Application.InputBox("Enter Password", "Password Protected") Select Case password Case Is = False 'do nothing Case Is = "easy" Range("A1").Value = "This is secret code" Case Else MsgBox "Incorrect Password" End Select

Result when you click the command button on the sheet:单击工作表上的命令按钮时的结果:

在此处输入图像描述

Password Protected from being Executed密码保护不被执行

Explanation : The macro uses the InputBox method of the Application object.解释:该宏使用 Application object 的 InputBox 方法。 If the users clicks Cancel, this method returns False and nothing happens (InputBox disappears).如果用户单击取消,则此方法返回 False 并且没有任何反应(InputBox 消失)。 Only when the user knows the password ("easy" again), the secret code will be executed.只有当用户知道密码时(再次“easy”),密码才会被执行。 If the entered password is incorrect, a MsgBox is displayed.如果输入的密码不正确,则会显示一个 MsgBox。 Note that the user cannot take a look at the password in the Visual Basic Editor because the project is protected from being viewed请注意,用户无法在 Visual Basic 编辑器中查看密码,因为该项目受到保护,无法查看

You can check out this new product at http://hivelink.io , it allows you to properly protect your sensitive macros.您可以在http://hivelink.io查看这款新产品,它可以让您正确保护敏感的宏。

The Excel password system is extremely weak - you can crack it in 2 minutes just using a basic HEX editor. Excel 密码系统非常薄弱——您只需使用基本的 HEX 编辑器即可在 2 分钟内破解它。 I wouldn't recommend relying on this to protect anything.我不建议依靠它来保护任何东西。

I wrote an extensive post on this topic here: Protecting Code in an Excel Workbook?我在这里写了一篇关于这个主题的文章: 在 Excel 工作簿中保护代码?

The modern approach is to move away from VBA for important code, and write a .NET managed Add-In using c# or vb.net, there are a lot of resources for this on the www, and you could use the Express version of MS Visual Studio The modern approach is to move away from VBA for important code, and write a .NET managed Add-In using c# or vb.net, there are a lot of resources for this on the www, and you could use the Express version of MS Visual Studio

you can set a password to your vba code but this can be quite easily broken up.您可以为 vba 代码设置密码,但这容易被破解。

you can also create an addin and compile it into a DLL .您还可以创建一个插件并将其编译为DLL See here for more information.请参阅此处了解更多信息。 That's at least the most secure way to protect your code.这至少是保护您的代码的最安全的方法。

Regards,问候,

Generate a protected application for Mac or Windows from your Excel spreadsheet using OfficeProtect with either AppProtect or QuickLicense/AddLicense.使用带有 AppProtect 或 QuickLicense/AddLicense 的 OfficeProtect 从 Excel 电子表格中为 Mac 或 Windows 生成受保护的应用程序。 There is a demonstation video called "Protect Excel Spreedsheet" at www.excelsoftware.com/videos. www.excelsoftware.com/videos 上有一个名为“Protect Excel Spreedsheet”的演示视频。

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

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