I am trying to develop a VB6 program(DLL) that can control VBA UserForm. Please refer to below as my situation.
- Create a VBA UserForm, and during "UserForm_Initialize" sub, pass "Me" as a parameter to DLL.
- DLL dynamic Add controls, but with "declare by Object"(can't specific to MSForms object type)
- It needs to declare by "WithEvents" for object methods, but during assign, the Error Code:13, "Type Mismatch" will occur.
- check by "TypeOf IS" is mismatch neither.
Does anyone know how to assign an object "WithEvents" by VB6?
please refer to below as source code, or you may download it from HERE
Any reply will be appreciated!
in vb6, "ControlsAddClass" Class
Option Explicit
Public WithEvents cmdbtnTest As MSForms.CommandButton
'Public cmdbtnTest As Object
Function VBAUserFormControlAdd(objRunForm As MSForms.UserForm)
Dim lblTypeOfIsDesc As Object, lblTypeIsResult As Object
With objRunForm
Set lblTypeOfIsDesc = .Controls.Add("Forms.Label.1", "lblTypeOfIsDesc", True)
With lblTypeOfIsDesc
.Caption = "TypeOf cmdbtnTest Is MSForms.CommandButton: "
.Width = 360
.AutoSize = True
.BorderStyle = 1
.Visible = True
End With
Set lblTypeIsResult = .Controls.Add("Forms.Label.1", "lblTypeIsResult", True)
With lblTypeIsResult
.Left = lblTypeOfIsDesc.Left + lblTypeOfIsDesc.Width + 10
.Width = 180
.BorderStyle = 1
.Visible = True
End With
Set cmdbtnTest = .Controls.Add("Forms.CommandButton.1", "cmdbtnTest", True)
With cmdbtnTest
.Top = lblTypeOfIsDesc.Top + lblTypeOfIsDesc.Height + 10
.Caption = "Test Btn"
End With
With lblTypeIsResult
.Caption = TypeOf cmdbtnTest Is MSForms.CommandButton
.AutoSize = True
End With
End With
End Function
Private Sub cmdbtnTest_Click()
MsgBox "Test!"
End Sub
in VBA Module,
Option Explicit
Const strDLLName As String = "VB6_VBA_UserForm_Control"
Function UserFormControlsAdd()
Dim objReferenceRun As Variant, blnReferenceExist As Boolean
For Each objReferenceRun In ThisWorkbook.VBProject.References
If objReferenceRun.Description = Replace(strDLLName, "_", " ") Then
blnReferenceExist = True
Exit For
End If
Next
If Not blnReferenceExist Then RegMount
TestUserForm.Show
End Function
Function RegMount()
Dim strDLLFilePath As String
Dim FSO As New Scripting.FileSystemObject
strDLLFilePath = ThisWorkbook.Path & "\VB6_VBA_UserForm_Control.dll"
If FSO.FileExists(ThisWorkbook.Path & "\VB6_VBA_UserForm_Control.dll") Then
Shell "Regsvr32 /s """ & strDLLFilePath & """"
ThisWorkbook.VBProject.References.AddFromFile strDLLFilePath
Else
MsgBox "DLLFilePath: " & strDLLFilePath & " File Miss!"
End If
Set FSO = Nothing
End Function
in VBA, "TestUserForm" UserForm
Option Explicit
Private Sub UserForm_Initialize()
MsgBox "UserForm_Initialized!"
Dim clsRunControlsAdd As New VB6_VBA_Control.ControlsAddClass
clsRunControlsAdd.VBAUserFormControlAdd objRunForm:=Me
End Sub
