vba 锁定 Microsoft Excel 宏

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

Lock down Microsoft Excel macro

excelvbasecurity

提问by Dave

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

谢谢

回答by Brian Beckett

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. 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. There are also many VBA Password Recovery [tools] for saleon the www.

To protect your code, open the Excel Workbook and go to Tools>Macro>Visual Basic Editor (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. After doing this you must save, close & reopen the Workbook for the protection to take effect.

当我们编写 VBA 代码时,通常希望 VBA 宏代码对最终用户不可见。这是为了保护您的知识产权和/或阻止用户弄乱您的代码。请注意,Excel 的保护能力远非安全。www 上还有许多 VBA 密码恢复 [工具] 出售

要保护您的代码,请打开 Excel 工作簿并转到工具>宏>Visual Basic 编辑器 (Alt+F11)。现在,从 VBE 中转到“工具”>“VBA 项目属性”,然后单击“保护”页面选项卡,然后选中“锁定项目以防止查看”,然后再次输入您的密码进行确认。执行此操作后,您必须保存、关闭并重新打开工作簿以使保护生效。

(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 decompilingit.

请记住,即使您能够分发已编译的代码副本,也无法阻止人们对其进行反编译

回答by shareef

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...

  1. 首先,创建一个要保护的简单宏。

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

  2. 接下来,单击工具,然后单击VBAProject 属性...

enter image description here

在此处输入图片说明

Click Tools, VBAProject Properties...

单击工具,VBAProject 属性...

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

enter image description here

在此处输入图片说明

Enter a Password Twice

输入密码两次

  1. Click OK.

  2. Save, closeand reopen the Excel file. Try to view the code.

  1. 单击“确定”

  2. 保存关闭并重新打开 Excel 文件。尝试查看代码。

enter image description here

在此处输入图片说明

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:
  1. 如果要密码保护宏不被执行,请添加以下代码行:
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
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:

单击工作表上的命令按钮时的结果:

enter image description here

在此处输入图片说明

Password Protected from being Executed

密码保护不被执行

Explanation: The macro uses the InputBox method of the Application object. If the users clicks Cancel, this method returns False and nothing happens (InputBox disappears). Only when the user knows the password ("easy" again), the secret code will be executed. If the entered password is incorrect, a MsgBox is displayed. Note that the user cannot take a look at the password in the Visual Basic Editor because the project is protected from being viewed

说明:该宏使用 Application 对象的 InputBox 方法。如果用户单击取消,则此方法返回 False 并且没有任何反应(InputBox 消失)。只有当用户知道密码时(再次“easy”),密码才会被执行。如果输入的密码不正确,则会显示 MsgBox。请注意,用户无法在 Visual Basic 编辑器中查看密码,因为该项目受到保护而无法查看

回答by stuzor

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. I wouldn't recommend relying on this to protect anything.

Excel 密码系统非常脆弱 - 只需使用基本的 HEX 编辑器,您就可以在 2 分钟内破解它。我不建议依靠它来保护任何东西。

I wrote an extensive post on this topic here: Protecting Code in an Excel Workbook?

我在这里写了一篇关于这个主题的广泛帖子:保护 Excel 工作簿中的代码?

回答by Our Man in Bananas

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

现代方法是将重要代码从 VBA 移开,并使用 c# 或 vb.net 编写 .NET 托管加载项,www 上有很多相关资源,您可以使用 MS 的 Express 版本视觉工作室

回答by JMax

you can set a passwordto your vba code but this can be quiteeasily broken up.

您可以为 vba 代码设置密码,但这容易破解。

you can also create an addinand compile it into a DLL. See herefor more information. That's at least the most secure way to protect your code.

您还可以创建一个插件并将其编译成一个DLL。请参阅此处了解更多信息。这至少是保护代码的最安全方式。

Regards,

问候,

回答by harold

Generate a protected application for Mac or Windows from your Excel spreadsheet using OfficeProtect with either AppProtect or QuickLicense/AddLicense. There is a demonstation video called "Protect Excel Spreedsheet" at www.excelsoftware.com/videos.

使用带有 AppProtect 或 QuickLicense/AddLicense 的 OfficeProtect 从 Excel 电子表格生成适用于 Mac 或 Windows 的受保护应用程序。www.excelsoftware.com/videos 上有一个名为“Protect Excel Spreedsheet”的演示视频。