|
本帖最后由 nothingwmm 于 2011-1-26 22:33 编辑
- Option Explicit
- Sub MakeForm()
- Dim TempForm As Object 'VB 组件
- Dim NewButton As Msforms.CommandButton
- Dim Line As Integer
- Dim TheForm
- ' 确认权限
- On Error Resume Next
- Dim x
- Set x = ActiveWorkbook.VBProject
- If Err <> 0 Then
- MsgBox "Your security settings do not allow this macro to run.", vbCritical
- On Error GoTo 0
- Exit Sub
- End If
-
- Application.VBE.MainWindow.Visible = False '编译器隐藏
- ' 创建Form
- Set TempForm = ThisWorkbook.VBProject. _
- VBComponents.Add(3) '添加窗体即 vbext_ct_MSForm
- With TempForm
- .Properties("Caption") = "Temporary Form"
- .Properties("Width") = 200
- .Properties("Height") = 100
- End With '设置窗体属性
- ' 添加命令按钮
- Set NewButton = TempForm.Designer.Controls _
- .Add("forms.CommandButton.1")
- With NewButton
- .Caption = "Click Me"
- .Left = 60
- .Top = 40
- End With '设置属性
- ' 为窗体添加代码
- With TempForm.CodeModule
- Line = .CountOfLines
- .InsertLines Line + 1, "Sub CommandButton1_Click()"
- .InsertLines Line + 2, "MsgBox ""Hello!"""
- .InsertLines Line + 3, "Unload Me"
- .InsertLines Line + 4, "End Sub"
- End With
- ' 显示窗体
- VBA.UserForms.Add(TempForm.Name).Show
- '
- ' 删除窗体
- ThisWorkbook.VBProject.VBComponents.Remove TempForm
- End Sub
复制代码
|
|