Protect VBA Project Programmatically with SendKeys

zelarra821

Registered User.
Local time
Today, 01:17
Joined
Jan 14, 2019
Messages
842
Hi guys.

I need your help.

I want to protect/unprotect a VBA project using SendKeys based on the value of a specific cell in a specific sheet.

I created this macro, in which I set the sheet where it will check whether or not to protect the VBA project and the password, and then use SendKeys to protect/unprotect.

Code:
Sub ProtegerProyectoVBA()

    On Error GoTo ErrLbl

Dim NumberSheets As String
Dim SLen As Long
Dim SheetCodeName As String
Dim sh As Worksheet

    NumberSheets = CStr(ThisWorkbook.Worksheets.Count)
    
    SLen = Len(NumberSheets)
            
    SheetCodeName = "Hoja" & Right(String(SLen, "0") & 0, SLen)

    Set sh = ThisWorkbook.Sheets(SheetCodeName)

    Application.ScreenUpdating = False

    Select Case sh.Range("AB1")

        Case True 'Proteger
        
            Select Case Application.VBE.ActiveVBProject.Protection
            
                Case 0 'Desprotegido

                    Application.VBE.MainWindow.Visible = True
                    
                    Application.VBE.MainWindow.SetFocus
    
                    SendKeys "%H"
                    SendKeys "P"
                    SendKeys "^{TAB}"
                    SendKeys "B"
                    SendKeys "C"
                    SendKeys sh.Range("AB2")
                    SendKeys "{TAB}"
                    SendKeys sh.Range("AB2")
                    SendKeys "{ENTER}"
                    
                    Application.VBE.MainWindow.Visible = False
                
                Case 1 'Protegido
                
                    'No hacer nada
                
            End Select
        
        Case False 'Desproteger
        
            Select Case Application.VBE.ActiveVBProject.Protection
            
                Case 0 'Desprotegido
                
                    'No hacer nada
                
                Case 1 'Protegido
                
                    Application.VBE.MainWindow.Visible = True
                    
                    Application.VBE.MainWindow.SetFocus
    
                    SendKeys "%{HP}"
                    SendKeys "^{TAB}"
                    SendKeys "B"
                    SendKeys "C"
                    SendKeys "{BS}"
                    SendKeys "{TAB}"
                    SendKeys "{BS}"
                    SendKeys "{ENTER}"
                    
                    Application.VBE.MainWindow.Visible = False

            End Select
            
    End Select
        
    Application.ScreenUpdating = True
            
ErrLbl_Exit:

    Application.ScreenUpdating = True
    
    Exit Sub
    
ErrLbl:

    Application.ScreenUpdating = True
    
    Exit Sub
            
End Sub

Code:
Private Sub Workbook_Open()

Dim NumberSheets As String
Dim SLen As Long
Dim SheetCodeName As String
Dim sh As Worksheet

    NumberSheets = CStr(ThisWorkbook.Worksheets.Count)
    
    SLen = Len(NumberSheets)
            
    SheetCodeName = "Hoja" & Right(String(SLen, "0") & 0, SLen)

    Set sh = ThisWorkbook.Sheets(SheetCodeName)
    
    If sh.Range("AB1") Then
        
        Application.Windows(Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".") - 1)).Visible = False
                
        PedirContraseña.Show
        
        ProtegerProyectoVBA
        
    Else
        
        ProtegerProyectoVBA
        
    End If
    
    mdlCodigos.AccesoDirecto
    
End Sub

However, it writes to the spreadsheet, even though I've been tracing the process step by step using the keyboard to transcribe it to ASCII.

As you can see, the ASCII code is in Spanish, because my menu is in Spanish. I've already verified this, so it should be that way.

Can you help me?

Thank you very much.
 
Last edited:
I've already solved it.

What I did was create a form to ask for a password if the user wants to access the VBA project, enabling or disabling ALT + F11, and hiding the VBA window if opened via the Developer tab.

Thank you very much.
 
But hiding the VBA project has nothing to do with protecting or unprotecting it!

And I would strongly suggest you don't try to actually unprotect it meaning enter the password to unprotect it, using send keys, this all sounds like an extremely bad idea
 

Users who are viewing this thread

Back
Top Bottom