I'm developing a code to update protected modules in Excel.
This is the schema:
- 1: On opening the book (Thisworkbook module, open function), there is a comparison between workbook version and valid version of the book (connecting to a database). If it's the same, it ends. If not, goes to step 2.
- 2: function UnprotectVBProj is called to unprotect VBA code.
- 3: all modules (except the one that contains functions to unprotect, delete and load new modules) are deleted using function.
- 4: modules are loaded again from server, using a function called 'import_modules'.
- 5: new version is set in workbook and it's closed.
My code works if I execute it step by step.
When it runs automatically on opening the book, function "DeleteVBAModulesAndUserForms" returns an error saying that code is protected. If I debug, I can see the code is not protected, it's really unprotected. It's like vbproject variables hadn't been set to the new unprotected state.
Here is the code:
Sub Workbook_Open()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Variables
Dim herramienta As String
Dim password_hoja As String
Dim password_visual As String
password_visual = "1111111111"
herramienta = "222222222"
password_hoja = "333333333"
Dim miversion As String
Dim WS_Count as Integer, i As Integer
miversion = CStr(Sheets("Control").Range("AG106").Value)
'Here is the function to make the comparation with version stered in database
If ultima_version(herramienta) = miversion Then
Else`enter code here`
MsgBox ("Old version, need to update.")
'Unprotect the code
Call UnprotectVBProj(ThisWorkbook, password_visual)
'delete all modules
Call DeleteVBAModulesAndUserForms
'load new modules
Call importa_modulos
'change version in sheet
ActiveSheet.Unprotect Password:=password_hoja
Sheets("Control").Range("AG106").Value = ultima_version(herramienta)
ActiveSheet.Protect Password:=password_hoja, DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True, AllowSorting:= _
True, AllowFiltering:=True, AllowUsingPivotTables:=True
'Save and close
ActiveWorkbook.Close (1)
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Sub UnprotectVBProj(ByRef WB As Workbook, ByVal Password As String)
Dim vbProj As Object
Set vbProj = WB.VBProject
Application.ScreenUpdating = True
If vbProj.Protection <> 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = vbProj
SendKeys Password & "~"
SendKeys "~"
Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute
Application.ScreenUpdating = False
End Sub
Sub DeleteVBAModulesAndUserForms()
Dim vbProj2 As Object
Dim VBComp As Object
Set vbProj2 = ActiveWorkbook.VBProject
'Next line is commented to display protection error
'If vbProj2.Protection <> vbext_pp_none Then Exit Sub
For Each VBComp In vbProj2.VBComponents
Select Case VBComp.Type
Case Is = vbext_ct_StdModule, vbext_ct_ClassModule, vbext_ct_MSForm
If VBComp.Name = "A_Importa_Modulos" Or VBComp.Name = "ThisWorkbook" Then
'Thisworkbook or worksheet module
'We do nothing
Else
vbProj2.VBComponents.Remove VBComp
End If
Case Is = vbext_ct_Document
'With VBComp.CodeModule
'.DeleteLines 1, .CountOfLines
'End With
End Select
Next VBComp
End Sub