Send Keys to unlock a VBA Project
Any I am somewhat advanced in macro writing, however it was all self-taught off this site and I don’t fully understand the big picture
I am trying to create an Excel spread sheet that will update the VBA code of another Excel spread sheet that has a password protected VBA Project. I am using SendKeys to unlock the VBA Project. I am also solving for SendKey deficiencies by writing scrip to close all open excel documents.
All the code I have written works on its own but when I try to combine it the SendKey macro is placing the password in other lines of code:
This works:
Sub UnprotectProject()
With Application
.SendKeys "%{F11}", True
.SendKeys "^r", True
.SendKeys "~", True
.SendKeys "password", True
.SendKeys "~", True
End With
End Sub
This inserts the password in other VBA Code:
Sub UnprotectProject()
With Application
.SendKeys "%{F11}", True
.SendKeys "^r", True
.SendKeys "~", True
.SendKeys "password", True
.SendKeys "~", True
End With
Application.VBE.MainWindow.Visible = False
End Sub
Update:
Both sets of code are the same except for, the following line in the second example
Application.VBE.MainWindow.Visible = False
The full code I am trying to write has five tasks, I created a macro for each task, then another macro to run the five macros. Each macro does the intended job when run independently. However when I try to run the macro that combines the individual tasks, the macro with send keays is failing, and instead of unlocking the VBA Project it sticks the password in the code of one of the individual task macros These are the five tasks
Open (Opens the workbook to change)
Unprotect VBA Project
Update VBA Code
Update a Worksheet
SaveAs new version
This is the macro I wrote to run the individual tasks Sub UsernameCheck()
lastRow = Sheets("update").Range("I" & Rows.count).End(xlUp).Row
Uname = Environ("Username")
Set aCell = Sheets("update").Range("I4:I" & lastRow).Find(What:=Uname, MatchCase:=False)
If aCell Is Nothing Then
MsgBox ("Not an Authorised User")
Else
Open_1
UnprotectProject
ChangeDateAddUserCheck
UpdateDashBoard
Save
End If
End Sub
This is the code I am using to edit a macro
Sub ChangeDateAddUserCheck()
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim S As String
Dim LineNum As Long
Set VBComp = ActiveWorkbook.VBProject.VBComponents("Module2")
'Delete
VBComp.CodeModule.DeleteLines 15, 4
'add Code
Set CodeMod = VBComp.CodeModule
LineNum = 15
S = "yr = Format(Now(), ""YYYYMMDD"")" & vbCrLf & _
"If UCase(Sheets(""DashBoard"").Range(""B21"").Value) = UCase(Environ(""Username"")) Then" & vbCrLf & _
"If yr < 20160601 Then B2_Stage Else MsgBox (""Software is Expired"")" & vbCrLf & _
"Else: MsgBox (""Not Authorized User"")" & vbCrLf & _
"End If"
CodeMod.InsertLines LineNum, S
End Sub
The password is being pasted in the code above between the folloing lines, but I think that has more to do with the placement of the Macro in the VBA Editor
Dim LineNum As Long
Set VBComp = ActiveWorkbook.VBProject.VBComponents("Module2")