-1

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
Community
  • 1
  • 1
Ihidan
  • 558
  • 1
  • 7
  • 25
  • 1
    Did you try searching for this answer before you posted your question? If you don't show any research effort, your question will be down-voted. Without even doing a search myself, I know this question should have hundreds of answers already out there. – rory.ap Sep 29 '15 at 13:10

1 Answers1

1

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.

MatthewD
  • 6,719
  • 5
  • 22
  • 41
  • 1
    I am still able to read the VBA code of the workbook. Even after applying the protect property. – Ihidan Sep 29 '15 at 13:39
  • See this post.. http://stackoverflow.com/questions/17777770/vba-project-password-protect-with-sendkeys-not-working-correctly – MatthewD Sep 29 '15 at 13:41