Greetings,
I have a form that creates an "Excel.Application" object, creates a button on the first sheet ("Sheet1"), and adds an "OnAction" module for the button.
The code gets so far as adding the button, but when I try to add the CodeModule, I get a Run-time error '9', saying subscript out of range.
Any ideas? I've got my Excel set to "Enable all macros" and to "Trust access to the VBA project object model".
Here's my code snippet:
Thanks for any help you can provide!
Jonathan Mulder
I have a form that creates an "Excel.Application" object, creates a button on the first sheet ("Sheet1"), and adds an "OnAction" module for the button.
The code gets so far as adding the button, but when I try to add the CodeModule, I get a Run-time error '9', saying subscript out of range.
Any ideas? I've got my Excel set to "Enable all macros" and to "Trust access to the VBA project object model".
Here's my code snippet:
Code:
Private Sub CmdTest_Click()
Set x1 = CreateObject("Excel.Application")
Set x1Wkbk = x1.Workbooks.Add
x1.Visible = True
x1.ActiveSheet.Buttons.Add(199.5, 20, 81, 36).Select
x1.Selection.Name = "New Button"
x1.Selection.OnAction = "CheckTotals"
x1.ActiveSheet.Shapes("New Button").Select
x1.Selection.Characters.Text = "Check Totals"
x1.Selection.OnAction = "ShowMessage"
'''Dim sht As Object
Dim shtCode As String
'''Set sht = x1.Sheets("Sheet1")
shtCode = _
"Sub ShowMessage" & vbNewLine & _
"Msgbox(" & Chr(34) & "Hello Jonny" & Chr(34) & ")" & vbNewLine & _
"End Sub"
x1.Sheets("Sheet1").Select
x1.ActiveWorkbook.VBProject.VBComponents(x1.Sheets("Sheet1").CodeName).CodeModule.AddFromString shtCode
End Sub
Thanks for any help you can provide!
Jonathan Mulder