简体   繁体   English

使用密码保护 VBA 项目

[英]Protect a VBA project with password

My VBA add-in copies some sheets into a new workbook.我的 VBA 加载项将一些工作表复制到新工作簿中。 Although my add-in is project by password the new workbook is not password protected when generated.尽管我的加载项是通过密码进行的项目,但新工作簿在生成时不受密码保护。 So the user is able to read the code in it.因此,用户能够阅读其中的代码。

I am trying to use protected property to hide the code, but it doesn't seen to work.我正在尝试使用受保护的属性来隐藏代码,但没有看到它起作用。

Code to generate the new workbook:生成新工作簿的代码:

Sub CreateNewWorkbook()

On Error GoTo LabelErro

Application.ScreenUpdating = False

Set NewWorkBook = Workbooks.Add

Dim currentSheet As Worksheet
Dim sheetIndex As Integer
sheetIndex = 1

With ThisWorkbook

    .IsAddin = False

    .Sheets("Ajuda").Copy Before:=NewWorkBook.Sheets(sheetIndex)

    .Sheets("Fronteira").Copy Before:=NewWorkBook.Sheets(sheetIndex)

    .Sheets("Correl").Copy Before:=NewWorkBook.Sheets(sheetIndex)

    .Sheets("Atributos").Visible = True
    .Sheets("Atributos").Copy Before:=NewWorkBook.Sheets(sheetIndex)
    .Sheets("Atributos").Visible = xlVeryHidden

    .Sheets("Pesos").Copy Before:=NewWorkBook.Sheets(sheetIndex)

    .Sheets("Calculos").Copy Before:=NewWorkBook.Sheets(sheetIndex)

    .Sheets("Hidden").Visible = True
    .Sheets("Hidden").Copy Before:=NewWorkBook.Sheets(sheetIndex)
    .Sheets("Hidden").Visible = xlVeryHidden

    .IsAddin = True

End With

With NewWorkBook

    .Sheets("Hidden").Visible = xlVeryHidden
    .Sheets("Calculos").Visible = xlVeryHidden
    .Sheets("Pesos").Range("C1").Formula = "=INDIRECT(""D"" & Hidden!B1+Hidden!B4+1)"
    .Sheets("Pesos").Range("C2").Formula = "=INDIRECT(""E"" & Hidden!B1+Hidden!B4+1)"
    .Protect Password:="teste", Structure:=True, Windows:=True

End With

Exit Sub

LabelErro:

        ThisWorkbook.IsAddin = True

End Sub

As roryap said, a simple Google of "Excel VBA set workbook password" returned many results.正如roryap所说,“Excel VBA设置工作簿密码”的简单谷歌返回了许多结果。 Here is the first one I looked at.这是我看的第一个。

ActiveWorkbook.Protect Password:="test", Structure:=True, Windows:=True 

expression.Protect(Password, Structure, Windows) expression Required. expression.Protect(Password, Structure, Windows) 表达式 必需。 An expression that returns a Workbook object.返回 Workbook 对象的表达式。

Password Optional Variant.密码可选变体。 A string that specifies a case-sensitive password for the worksheet or workbook.为工作表或工作簿指定区分大小写的密码的字符串。 If this argument is omitted, you can unprotect the worksheet or workbook without using a password.如果省略此参数,您可以在不使用密码的情况下取消保护工作表或工作簿。 Otherwise, you must specify the password to unprotect the worksheet or workbook.否则,您必须指定密码以取消保护工作表或工作簿。 If you forget the password, you cannot unprotect the worksheet or workbook.如果忘记密码,则无法取消对工作表或工作簿的保护。 It's a good idea to keep a list of your passwords and their corresponding document names in a safe place.最好将您的密码及其相应文档名称的列表保存在安全的地方。

Structure Optional Variant.结构可选变体。 True to protect the structure of the workbook (the relative position of the sheets). True 保护工作簿的结构(工作表的相对位置)。 The default value is False.默认值为 False。

Windows Optional Variant. Windows 可选变体。 True to protect the workbook windows. True 以保护工作簿窗口。 If this argument is omitted, the windows aren't protected.如果省略此参数,则窗口不受保护。

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

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