I am creating an .xls file via VBA and making it protected
I need to add workbook_Open event for this workbook.
I have added it using the below sample code
Throiugh code , i created the file, protected it , added the above code and saved.
But when i closed and re-open, this workbook_open event got vanished.
Please help
I need to add workbook_Open event for this workbook.
I have added it using the below sample code
Code:
Sub CreateEventProcedure()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Const DQUOTE = """" ' one " character
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("ThisWorkbook")
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CreateEventProc("Open", "Workbook")
LineNum = LineNum + 1
.InsertLines LineNum, " MsgBox " & DQUOTE & "Hello World" & DQUOTE
End With
End Sub
Throiugh code , i created the file, protected it , added the above code and saved.
But when i closed and re-open, this workbook_open event got vanished.
Please help
Last edited by a moderator: