I want to generate some code which will add a button and its code to a worksheet Sheet1.
Adding the button is fine but I get a 'Programmatic access to Workbook not trusted' and a Debug error of
'VBProject of Object Workbook failed' error.
At the moment I'm just trying to add the basic Sub() and End Sub() part of the code.
Can anyone see what's wrong with this?
Sub WriteCode()
Dim NewButton As Object
Set NewButton = Sheets(1).OLEObjects.Add _("Forms.CommandButton.1")
With NewButton
.Left = 10
.Top = 10
.Width = 100
.Height = 25
.Object.Caption = "Add Lesson _ Code"
End With
Code = "Sub CommandButton1_Click()"
Code = Code & vbCrLf
Code = Code & "End Sub" & vbCrLf
With ThisWorkbook.VBProject
.VBComponents _("Sheet1").CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, Code
End With
End Sub
Cheers in advance.
This message was edited by T on 2002-09-17 21:10
Adding the button is fine but I get a 'Programmatic access to Workbook not trusted' and a Debug error of
'VBProject of Object Workbook failed' error.
At the moment I'm just trying to add the basic Sub() and End Sub() part of the code.
Can anyone see what's wrong with this?
Sub WriteCode()
Dim NewButton As Object
Set NewButton = Sheets(1).OLEObjects.Add _("Forms.CommandButton.1")
With NewButton
.Left = 10
.Top = 10
.Width = 100
.Height = 25
.Object.Caption = "Add Lesson _ Code"
End With
Code = "Sub CommandButton1_Click()"
Code = Code & vbCrLf
Code = Code & "End Sub" & vbCrLf
With ThisWorkbook.VBProject
.VBComponents _("Sheet1").CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, Code
End With
End Sub
Cheers in advance.
This message was edited by T on 2002-09-17 21:10