Calling Vba Project password

Asked

Viewed 134 times

0

Hello, I need to run a macro which is the Private Sub Workbook_BeforeSavehowever the spreadsheet points out that the project is protected and therefore does not execute.

I wanted to know how to 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 would like a light. I tried to search the possible ID’s, I did not find any compatible.

  • Why are you calling this ID 761? It is not the project’s protection window, it is the ID 2578 which is what calls the properties of the project.

  • Queria saber qual é o numero do hahaha huh?

1 answer

1

There’s a way to find the ID's which may be used in that method FindControl, it would be something like that:

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 ID that you need to do this is the 2578 which refers to the properties of the project, which is where you can use the SendKeys to protect and unprotect the VBA.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.