MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Accessing a UserForm that was imported programmically

  Asked By: Robert    Date: Jan 12    Category: MS Office    Views: 1539

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
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: " &
End Sub

Sub ShowTheForm()
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
UserFormZ.Label1.BackColor = &HFF00FF
UserFormZ.Label1.ForeColor = 0
End If
MsgBox "Label1 property value after click: " &
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()
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



No Answers Found. Be the First, To Post Answer.

Didn't find what you were looking for? Find more on Accessing a UserForm that was imported programmically Or get search suggestion and latest updates.