I pulled this code from an Eng-Tips faqs, but I am having a lot of trouble with it. I'm trying to create a form from a class module.
I added the reference library "Microsoft Visual Basic for Applications Extensibility 5.3" but it still cannot find the library for MSForms.
So I changes the variable NewButton from MSForms.CommandButton to just an Object.
Next it crashed on the line that reads
So I commented it out and tried to press forward. Finally it crashed again when I tried to create the form:
Ya I don't know how to work around that one. I'm using Excel 2003 prof. The .Add(vbext_ct_MSForm) did pop up in the intellisense, so it found something but it seems that the reference library wont register - or something like that. Any clues?
-JTBorton
Another Day, Another Disaster
I added the reference library "Microsoft Visual Basic for Applications Extensibility 5.3" but it still cannot find the library for MSForms.
Code:
Dim NewButton As MSForms.CommandButton
[b][COLOR=red]Compile error:
User-defined type not defined.[/color][/b]
So I changes the variable NewButton from MSForms.CommandButton to just an Object.
Next it crashed on the line that reads
Code:
Application.VBE.MainWindow.Visible = False
[b][COLOR=red]Run time error '1004'
Method 'VBE' of object '_Application' failed.[/color][/b]
So I commented it out and tried to press forward. Finally it crashed again when I tried to create the form:
Code:
Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
[b][COLOR=red]Run time error '1004'
Programmatic access to Visual Basic is not trusted[/color][/b]
Ya I don't know how to work around that one. I'm using Excel 2003 prof. The .Add(vbext_ct_MSForm) did pop up in the intellisense, so it found something but it seems that the reference library wont register - or something like that. Any clues?
Code:
Sub MakeForm()
Dim TempForm As Object ' VBComponent
Dim FormName As String
Dim NewButton As Object [highlight][COLOR=green]'MSForms.CommandButton[/color][/highlight]
Dim TextLocation As Integer
' ** Additional variable
Dim X As Integer
'Locks Excel spreadsheet and speeds up form processing
[highlight][COLOR=green]'Application.VBE.MainWindow.Visible = False[/color][/highlight]
Application.ScreenUpdating = False
' Create the UserForm
[highlight][COLOR=red]Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)[/color][/highlight]
'Set Properties for TempForm
TempForm.Name = "frmMyForm"
With TempForm
.Properties("Caption") = "Temporary Form"
.Properties("Width") = 200
.Properties("Height") = 100
End With
FormName = TempForm.Name
' Add a CommandButton
Set NewButton = TempForm.Designer.Controls.Add("forms.CommandButton.1")
With NewButton
.Caption = "Click Me"
.Left = 60
.Top = 40
End With
-JTBorton
Another Day, Another Disaster