简体   繁体   中英

Protect a VBA project with password

My VBA add-in copies some sheets into a new workbook. 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. Here is the first one I looked at.

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

expression.Protect(Password, Structure, Windows) expression Required. An expression that returns a Workbook object.

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). The default value is False.

Windows Optional Variant. True to protect the workbook windows. If this argument is omitted, the windows aren't protected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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