Results 1 to 9 of 9

Thread: [RESOLVED] Unlocking a project with VBA

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Resolved [RESOLVED] Unlocking a project with VBA

    I have an Excel project distributed to several hundred users that is self updating. When launched, it compares it's version number to a current version number on a remote server. If the numbers don't match, all of the modules and forms in the project are replaced with the most up to date versions.

    My problem now is that the project needs to be locked down due to some users editing code to circumvent certain controls. If I lock the project for editing or viewing, my modules can't be changed during one of the automatic update.

    I can view the current locked state of the project with the following property:
    Application.VBE.ActiveVBProject.Protection

    Unfortunately the property is read-only.

    Is there a way with VBA to unlock a password protected project, allow the necessary changes to occur and then relock it?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Unlocking a project with VBA

    I do not believe its possible and even if it was you would have to have each and every user manually check the "Trust programmatic access to VBA Projects" as this is also not programmable either.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Unlocking a project with VBA

    Well if you know the password and can supply it: (Note: this is not a finished code, it still needs a bit of work, but with a correctly supplied password, works pretty good even with send keys) It does include a bit of a test for the "Trusted Access to Visual Basic" problem also, as RobDog pointed out.

    VB Code:
    1. Option Explicit
    2. 'Your password goes here!!!!
    3. Const gszProjPassword As String = "hello"
    4.  
    5. Public Sub UnlockMe()
    6.     Dim wbName As Variant
    7.     Dim wbBook As Workbook
    8.     Dim vbaProj As Object
    9.     Dim oWin As Object
    10.     Dim X As Integer
    11.  
    12.     On Error GoTo ErrorHandler
    13.  
    14.     'Select the workbook with the project to unlock
    15.     wbName = Application.GetOpenFilename("Excel Files (*.xls),*.xls")
    16.    
    17.    
    18.     'Open it, assign an object ref to it's vba project
    19.     Set wbBook = Workbooks.Open(wbName)
    20.     Set vbaProj = wbBook.VBProject
    21.    
    22.     'Close any open code windows
    23.     For Each oWin In vbaProj.VBE.Windows
    24.       If InStr(oWin.Caption, "(") > 0 Then oWin.Close
    25.     Next oWin
    26.    
    27.     Application.VBE.MainWindow.Visible = False
    28.    
    29.     'Check to see if the VBA project is already unlocked
    30.     If vbaProj.Protection <> 1 Then
    31.         MsgBox "The VBA Project for the file you selected is already unlocked.", 0
    32.         Exit Sub
    33.        
    34.     'We found the project to be locked
    35.     ElseIf vbaProj.Protection = 1 Then
    36.  
    37.         On Error Resume Next
    38.        
    39.             Do While X < 4
    40.                 If vbaProj.Protection <> 1 Then
    41.                     MsgBox "The VBA project for " & wbName & " was unprotected successfully", 64
    42.                     Exit Do
    43.                 End If
    44.                 UnprotectVBProject wbBook, gszProjPassword
    45.                 X = X + 1
    46.             Loop
    47.         On Error GoTo 0
    48.  
    49.     End If
    50.    
    51. ErrorExit:
    52.     Set wbBook = Nothing
    53.     Set vbaProj = Nothing
    54.     Exit Sub
    55.    
    56. ErrorHandler:
    57.     Select Case Err.Number
    58.          
    59.     Case 1004
    60.    
    61.         wbBook.Close False
    62.        
    63.         MsgBox "You will need to set the " & _
    64.         "{ TRUST ACCESS TO VISUAL BASIC PROJECT } setting" & vbNewLine & _
    65.         "When the dialog appears, go to the Trusted Sources tab, " & _
    66.         "check the setting, click OK, and rerun this code again", 64
    67.         SendKeys "%T", True
    68.         SendKeys "M", True
    69.         SendKeys "S", True
    70.          
    71.     Case Else
    72.          
    73.         MsgBox Err.Description
    74.          
    75.     End Select
    76.     Resume ErrorExit
    77. End Sub
    78.  
    79. Public Sub UnprotectVBProject(wb As Workbook, ByVal Password As String)
    80.     Dim vbProj As Object
    81.  
    82.     On Error GoTo ErrorHandler
    83.  
    84.     Application.ScreenUpdating = False
    85.    
    86.         Set vbProj = wb.VBProject
    87.  
    88.         'Check to see if VBA project is already unlocked
    89.         If vbProj.Protection <> 1 Then Exit Sub
    90.    
    91.         'Activate chosen VBA Project
    92.         Set Application.VBE.ActiveVBProject = vbProj
    93.  
    94.             'SendKeys is the only way
    95.             If Password = "^^" Or Password = "++" Then
    96.                 Password = ""
    97.                 Exit Sub
    98.             ElseIf Right(Password, 2) = "^^" Or Right(Password, 2) = "++" Then
    99.                 Password = ""
    100.                 Exit Sub
    101.             Else
    102.                 SendKeys Password & "~~" & "{ESC}"
    103.                 Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
    104.             End If
    105.    
    106.         'Not the right password
    107.         If vbProj.Protection = 1 Then
    108.             SendKeys "%{F11}", True
    109.         End If
    110.  
    111.         'Reset Password
    112.         Password = ""
    113.    
    114.     Application.ScreenUpdating = True
    115.     Set vbProj = Nothing
    116.    
    117.     Exit Sub
    118.  
    119. ErrorHandler:
    120.     MsgBox Err.Description, 64
    121. End Sub
    Justin Labenne
    www.jlxl.net

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Unlocking a project with VBA

    I was going to suggest something like sendkays but I hate sendkeys as its so unreliable. If the user accidentally hits a key or clicks the mouse it will throw it all out of sync.

    I would reccomend writting your app as an Add-In or as a dll so there would be a minimum of code exposed in the VBA IDE. Its allot easier to update a dll file vs the alternative.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Unlocking a project with VBA

    Agreed. One other crazy thing I had seen in regards to utilizing my method above was that "why not disable the keyboard while it's running?".

    Never bothered to try and implement that kind of thing into this, if your really needing to protect your code that much from users, dll or locked up add-in all the way.... just replace the add-in with an updated add-in, so much easier....and reliable.
    Justin Labenne
    www.jlxl.net

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Unlocking a project with VBA

    I think we are both thinking the same things tonight! Great minds think alike.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Unlocking a project with VBA

    You're both my heroes. I have the sendkeys option in there for now, but I'd love to get everything in a dll. What do you use to compile your vba code to a dll?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: Unlocking a project with VBA

    Nevermind. The internet concensus seems to be VB6 while avoiding VB.NET at all costs for dlls. Thanks again.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Unlocking a project with VBA

    If you dont have VB6 you may want to look into MS Office Developer version which ccan make dlls seeing how your doing commercial support and production.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width