Call the Vba Project password

0

Hello, I need to run a macro that is Private Sub Workbook_BeforeSave but the worksheet points out that the project is protected and therefore does not run.

I wanted to know how I can call the password box, so I can unlock and lock quickly.

The code I'm trying to use:

    Sub ProtectVBProject()

    Dim wb As Workbook

    Set wb = ActiveWorkbook

    ' Ativa a planilha a ser bloqueada
    wb.Activate

    ' Envia o comando para abrir o VBA
    SendKeys "%{F11}", True

    ' Abre a janela de proteção do projeto VBA
    wb.VBProject.VBE.CommandBars(1).FindControl(ID:=761, recursive:=True).Execute ' Aqui eu precisaria do ID da caixa da senha

End Sub

If possible I would like a light. I tried searching the possible ID's, I did not find any compatible.

    
asked by anonymous 14.06.2017 / 14:55

1 answer

1

There is a way to find the ID's that can be used in this FindControl method, it would look something like this:

Private Sub ListarIDs()
    Dim ctrl1 As CommandBarControl, ctrl2 As CommandBarControl

    ' Percorre os primeiros níveis da barra de menu do VBA: Arquivo, Editar, Exibir, Inserir, Formatar, Depurar...
    For Each ctrl1 In Application.VBE.CommandBars(1).Controls
        ' Exibe o nome e o ID do primeiro nível
        Debug.Print "Nome: " & ctrl1.Caption & " - ID: " & ctrl1.ID
        ' Percorre os submenus como Arquivo > Salvar, Depurar > Compilar VBAProject...
        For Each ctrl2 In Application.VBE.CommandBars(1).Controls(ctrl1.Caption).Controls
            ' Exibe o nome e o ID do segundo nível
            Debug.Print "Nome: " & ctrl2.Caption & " - ID: " & ctrl2.ID
        Next
    Next
End Sub

The% w / w you need to do this is ID that refers to the project properties, which is where you can use 2578 to protect and unprotect VBA .

If you want to prevent the user from doing something that hinders sending SendKeys you can disable and then enable interaction with SendKeys and Application.Interactive = False .

    
14.06.2017 / 19:43