I've tried to insert the following code in a routine to which I've passed the reference to the worksheet belonging to a workbook just created and saved, but it does not add any code to the new workbook, without issuing any errors. Any idea? Thanks in advance
for your answers.
Dim ExcelWorkbook As Workbook
Dim ExcelProject As VBProject
Dim ThisWorkBookPrj As VBComponent
Dim WBname As String, WBtest As String
Dim VBAcode As String
Dim indWB As Integer
Set ExcelWorkbook = ExcelWorksheet.Parent
WBtest = ExcelWorkbook.Name
For indWB = 1 To Application.VBE.VBProjects.Count
WBname = Right(Application.VBE.VBProjects.Item(indWB).Filename, InStr(StrReverse(Application.VBE.VBProjects.Item(indWB).Filename), "") - 1)
If WBname = WBtest Then Exit For
Next indWB
If indWB <= Application.VBE.VBProjects.Count Then
Set ExcelProject = Application.VBE.VBProjects.Item(indWB)
Set ThisWorkBookPrj = ExcelProject.VBComponents.Item("ThisWorkBook")
VBAcode = "Private Sub Workbook_Open()" & vbLf + "with sheets(""" & ExcelWorksheet.Name & """)" & vbLf & _
".Protect Password:=""" & password & """, UserInterfaceOnly:=True" & vbLf & _
".EnableOutlining = True" & vbLf & "End With" & vbLf & "End Sub"
ThisWorkBookPrj.CodeModule.AddFromString VBAcode
End If