When I import a UserForm programmically, I must use Calls rather than
simply UserForm.Show or VBA.UserForms.Add("UserForm").Show to get the
UserForm to work properly. I've included code for a simple UserForm to
illustrate what works and commented out what doesn't work (with what
error I get). I'd appreciate any insight as to what is going on.
I'm using Xl97 on Windows 98
Sub ImportUserFormZ()
'load and call to show
ChDir ThisWorkbook.Path
ThisWorkbook.Activate
Application.VBE.ActiveVBProject.VBComponents.Import ("UserFormZ.frm")
Call ShowTheForm 'everything seems to work with a Call
'VBA.UserForms.Add("UserFormZ").Show 'shows, updates Form
property, but doesn't update screen display for Label1.
'UserFormZ.Show 'get run-time 424 object required
'AnyControlProperty = UserFormZ.Label1.BackColor 'get run-time 424
MsgBox "Label1 property value when hidden/terminated: " &
Hex(LabelProperty)
End Sub
Sub ShowTheForm()
UserFormZ.Show
End Sub
Sub TestViaCall(IsOdd)
'Label2 code is within the UserForm. Label1 code is here
If IsOdd Then
UserFormZ.Label1.BackColor = &HFF0000
UserFormZ.Label1.ForeColor = &HFFFFFF
Else
UserFormZ.Label1.BackColor = &HFF00FF
UserFormZ.Label1.ForeColor = 0
End If
MsgBox "Label1 property value after click: " &
Hex(UserFormZ.Label1.BackColor)
End Sub
Function LabelProperty()
LabelProperty = UserFormZ.Label1.BackColor
End Function
UserFormZ code - 2 labels and one command button. Clicking labels
changes backcolor
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub Label1_Click()
'change via Call
Static lb1Cnt As Integer
Dim IsOdd As Boolean
lb1Cnt = lb1Cnt + 1
If lb1Cnt / 2 = Int(lb1Cnt / 2) Then IsOdd = False Else IsOdd = True
Call TestViaCall(IsOdd)
End Sub
Private Sub Label2_Click()
Static lb2Cnt As Integer
Dim IsOdd As Boolean
lb2Cnt = lb2Cnt + 1
If lb2Cnt / 2 = Int(lb2Cnt / 2) Then IsOdd = False Else IsOdd = True
If IsOdd Then Label2.BackColor = &HFF& Else Label2.BackColor = &HFF00&
End Sub
Private Sub UserForm_Initialize()
Top = Application.UsableHeight * 0.4
Left = Application.UsableWidth / 2 - Width / 2
End Sub