简体   繁体   English

如何锁定excel宏(VB代码)

[英]how to lock down excel macros(VB code)

I have an excel which has lot of macros written, now our security team has come back and reported the vulnerability since a tech savvy can modify or delete macros.我有一个写了很多宏的 excel,现在我们的安全团队已经回来报告了这个漏洞,因为精通技术的人可以修改或删除宏。 So they suggested to lock down macros so that none of the user can even see it.所以他们建议锁定宏,这样用户甚至都看不到它。 Actually, I changed macros security by checking “Lock Project for viewing” in protection Tab which will lock down the macros, but the problem of this approach is that I need to maintain a password.实际上,我通过在保护选项卡中选中“锁定项目以查看”来更改宏安全性,这将锁定宏,但这种方法的问题是我需要维护密码。 Is there any other approach where I can lock down the macros in order to restrict the user to view .有没有其他方法可以锁定宏以限制用户查看 . help will be really appreciated.帮助将不胜感激。

Unviewable+ Pros: This is a much more secure method Unviewable+ Pros:这是一种更安全的方法

I am not so optimistic about Unviewable+ protection.我对 Unviewable+ 保护不太乐观。

Any “unviewable VBA code” protection works only in Excel VBA editor, and that VBA code is reachable outside of Excel.任何“不可查看的 VBA 代码”保护仅在 Excel VBA 编辑器中有效,并且该 VBA 代码可在 Excel 之外访问。

Here is a 10-step guide on how to view VBA code protected with Unviewable+.这是有关如何查看受 Unviewable+ 保护的 VBA 代码的 10 步指南。

Step 1. Detect the file format of the workbook or Excel add-in protected with Unviewable+.步骤 1.检测受 Unviewable+ 保护的工作簿或 Excel 加载项的文件格式。 To do that, open the file in Notepad and look at its first 2 symbols.为此,请在记事本中打开该文件并查看其前 2 个符号。 If first symbols are "PK" – the file is in XLSM format, otherwise it is in XLSB format (compound OLE storage).如果第一个符号是“PK” – 文件为 XLSM 格式,否则为 XLSB 格式(复合 OLE 存储)。 If your file is in XLSM format, then go to Step 4.如果您的文件是 XLSM 格式,请转到步骤 4。

Step 2. If you have unviewable Excel add-in in XLSB format, then you need to save it as a workbook.步骤 2。如果您有 XLSB 格式的无法查看的 Excel 加载项,则需要将其另存为工作簿。 To do that open the add-in file in Excel, open VBA Project (Alt+F11) and open "Immediate window" (Ctrl+G).为此,请在 Excel 中打开加载项文件,打开 VBA 项目 (Alt+F11) 并打开“立即窗口”(Ctrl+G)。 Type in the following command into the immediate window and press Enter (instead of "TheNameOfAddin.xlam" use your Unvewable+ protected add-in file name):在即时窗口中键入以下命令并按 Enter(而不是“TheNameOfAddin.xlam”,请使用您的 Unvewable+ 受保护的加载项文件名):

Workbooks("TheNameOfAddin.xlam").IsAddin = False

Then type the next command and press Enter:然后键入下一个命令并按 Enter:

Workbooks("TheNameOfAddin.xlam").SaveAs "ProtectedWbk.xlsb", 50

Then close Excel.然后关闭 Excel。 As a result of the actions above the "TheNameOfAddin.xlam" Excel add-in is saved as a "ProtectedWbk.xlsb" workbook in the default saving location of your Excel, which is most likely your Documents folder.作为上述操作的结果,“TheNameOfAddin.xlam”Excel 加载项在 Excel 的默认保存位置中保存为“ProtectedWbk.xlsb”工作簿,该位置很可能是您的 Documents 文件夹。

Step 3. Convert the workbook in .XLSB format into a workbook in .XLSM format.步骤 3.将 .XLSB 格式的工作簿转换为 .XLSM 格式的工作簿。 To do that open the .XLSB file in Excel and use File > SaveAs to save it in a XLSM format (Excel Macro-Enabled Workbook) like “ProtectedWbk.xlsm”.为此,请在 Excel 中打开 .XLSB 文件,然后使用“文件”>“另存为”将其保存为 XLSM 格式(Excel 启用宏的工作簿),例如“ProtectedWbk.xlsm”。 Close Excel.关闭 Excel。

Step 4. Create a new, empty workbook in Excel and save it in XLSM format (for example with the name "NewWbk.xlsm").步骤 4.在 Excel 中创建一个新的空工作簿并将其保存为 XLSM 格式(例如名称为“NewWbk.xlsm”)。 Open VBA Project (Alt+F11) in "NewWbk.xlsm" and add a new module into the project (by right-clicking on the "Project – VBAProject" explorer window, then left-clicking the Insert->Module buttons).在“NewWbk.xlsm”中打开 VBA 项目 (Alt+F11) 并将一个新模块添加到项目中(通过右键单击“项目 – VBAProject”资源管理器窗口,然后左键单击“插入”->“模块”按钮)。 Save the workbook.保存工作簿。 Close Excel.关闭 Excel。

Step 5. Rename the new workbook into a file with a zip extension, as such: "NewWbk.zip" Rename the workbook with the unviewable VBA code from "ProtectedWbk.xlsm" into a zip file like "ProtectedWbk.zip"步骤 5.将新工作簿重命名为具有 zip 扩展名的文件,例如:“NewWbk.zip” 将带有不可见 VBA 代码的工作簿从“ProtectedWbk.xlsm”重命名为“ProtectedWbk.zip”等 zip 文件

Step 6. Open ProtectedWbk.zip in Windows Explorer and go to the folder called "xl" If you see one "bin" file called "vbaProject.bin" then unzip it onto the disk.步骤 6.在 Windows 资源管理器中打开 ProtectedWbk.zip 并转到名为“xl”的文件夹 如果您看到一个名为“vbaProject.bin”的“bin”文件,则将其解压缩到磁盘上。 If you see 2 "bin" files called "vbaProject.bin" and "printerSettings.bin" then unzip the "printerSettings.bin" file on disk and rename the unzipped file from "printerSettings.bin" to "vbaProject.bin".如果您看到 2 个名为“vbaProject.bin”和“printerSettings.bin”的“bin”文件,则解压缩磁盘上的“printerSettings.bin”文件并将解压缩的文件从“printerSettings.bin”重命名为“vbaProject.bin”。

Step 7. Open NewWbk.zip in Windows Explorer and go to the "xl" folder and overwrite "vbaProject.bin" with "vbaProject.bin" you have unzipped on Step 6.第 7 步。在 Windows 资源管理器中打开 NewWbk.zip 并转到“xl”文件夹并用您在第 6 步中解压缩的“vbaProject.bin”覆盖“vbaProject.bin”。

Step 8. If you do not have LibreOffice installed, then download it from their official website (linked below) and install it on your computer.步骤 8。如果您没有安装 LibreOffice,请从他们的官方网站(链接如下)下载并安装到您的计算机上。

https://www.libreoffice.org/download/download/ https://www.libreoffice.org/download/download/

Step 9. Open LibreOffice Calc.步骤 9.打开 LibreOffice Calc。 Open "NewWbk.zip" in LibreOffice Calc.在 LibreOffice Calc 中打开“NewWbk.zip”。

Step 10. Follow the path -- LibreOffice Calc main menu > Tools > Macros > Edit Macros.步骤 10.按照路径 - LibreOffice Calc 主菜单 > 工具 > 宏 > 编辑宏。 Open "NewWbk.zip" catalog and observe all the VBA code from the "protected" file.打开“NewWbk.zip”目录并观察“受保护”文件中的所有 VBA 代码。

In terms of protecting your macros from being edited by other users, the simplest method is certainly the Password Method you've been suggested (Lock Project from viewing).在保护您的宏不被其他用户编辑方面,最简单的方法当然是您建议的密码方法(Lock Project from viewer)。

There is another way that I know to stop people from viewing your macros.我知道还有另一种方法可以阻止人们查看您的宏。 It's a tool called Unviewable+ .这是一个名为Unviewable+的工具。 This tool works well when you are a developer that want to distribute an add-in or a workbook with code to other people and you don't want them to have access or change the code.如果您是一名开发人员,希望将带有代码的加载项或工作簿分发给其他人,并且您不希望他们访问或更改代码,则此工具非常有用。

Password密码

Pros:优点:

  • Simple to implement实施简单

Cons:缺点:

  • Need to remember the password.需要记住密码。
  • Less secure as there are ways to bypass the password using some techniques you can find online.不太安全,因为有一些方法可以使用您可以在网上找到的一些技术来绕过密码。

Unviewable+不可见+

Pros:优点:

  • This is a much more secure method这是一种更安全的方法

Cons:缺点:

  • This is not free这不是免费的

  • You need to have a master version where you keep your code in a separate file.您需要有一个主版本,将代码保存在单独的文件中。 It will not be possible to reverse the process after making the code unviewable, so you want to apply this protection only to the file accessible to your end users.在使代码不可见后将无法逆转该过程,因此您只想将此保护应用于最终用户可访问的文件。

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

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