1

我正在尝试创建一个自定义/高级消息框(因为我可以或应该能够)

我在wenb上查看了这些资源:

1) MSDN - 如何以编程方式操作用户窗体

2)以编程方式添加用户窗体

我在我的 VB 项目中创建了一个名为“MessageBox”的类模块,它添加了一个用户窗体,并且应该添加一个按钮然后显示窗体......以及添加了 Visual Basic 应用程序扩展性参考。

但是当我尝试更改用户表单名称时,^&%^ 代码不断给我一个路径/文件访问错误...

我究竟做错了什么?

Private oForm As Object

Function Show(Optional ByVal MessageText As String, Optional ByVal Buttons As     VbMsgBoxStyle = vbOKOnly, Optional ByVal Caption As String) As VbMsgBoxResult

Dim oControl() As MSForms.CommandButton
Dim x As Integer, MaxWidth As Long

Set oForm = Application.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_MSForm)

With oForm
    .Properties("Height") = 200
    .Properties("Width") = 300
    .Properties("Caption") = Caption
        If Buttons = vbDefaultButton1 Then
            ReDim oControl(0)
            Set oControl(0) = .Designer.Controls.Add("Forms.CommandButton.1")
            With oControl(0)
                .Caption = "OK"
                .Height = 18
                .Width = 44
                .Left = MaxWidth + 147
                .Top = 6
                .Name = "cmdOK"
            End With
        End If
        .Name = "cMessageBox"
    End With
    'Application.VBE.ActiveVBProject.VBComponents("UserForm1").Name="cMessageBox"

    ' ActiveWorkbook.VBProject.VBComponents ("cMessageBox")
    With oForm.CodeModule
        x = .CountOfLines
        .InsertLines x + 1, "Sub cmdOK_Click()"
        .InsertLines x + 2, "    Unload Me"
        .InsertLines x + 3, "End Sub"
    '        .InsertLines x + 4, ""
    '        .InsertLines x + 5, "Sub CommandButton2_Click()"
    '        .InsertLines x + 6, "    Unload Me"
    '        .InsertLines x + 7, "End Sub"
    End With
    Call ShowIt

    End Function

    Private Sub ShowIt()
    cMessageBox.Show
    End Sub
4

0 回答 0