Protect VBA Project Programmatically with SendKeys

zelarra821

Registered User.
Local time
Today, 03:05
Joined
Jan 14, 2019
Messages
843
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
 
But hiding the VBA project has nothing to do with protecting or unprotecting it!
I know

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
That's the reason I want to learn how to hide VBA project, not protect.

Thanks.
 
I know


That's the reason I want to learn how to hide VBA project, not protect.

Thanks.
Ok. Understood.. kind of.

One other thing to remember is that it's very important to go ahead and password protect the VBA project as well as to have good error handling in any non-trivial sub or function. If you get an unhandled error and the ugly runtime debug window comes up, people will be very confused and worse yet if the project isn't password protected they will click debug and go straight into your code. All they have to do is hit a space bar and enter key from that point to mess up your code, or keep using it in debug mode to corrupt the whole thing.
You probably know all of these things I just thought they were relevant to throw out.
 

Users who are viewing this thread

Back
Top Bottom