UserForm.Name can and cannot be accessed

C

Cooz

Hi everyone,

My template contains three UserForms: UserForm1, UserForm2 and, indeed,
UserForm3. In addition, it has this sub:

Sub ASub()
Dim aForm As UserForm

Load UserForm1
Load UserForm2
Load UserForm3
MsgBox UserForms(1).Name

For Each aForm In UserForms
If aForm.Name = "UserForm2" Then Exit For
Next
Call AnotherSub(aForm)

End Sub

When I step through the code, it displays 'UserForm2' in the MsgBox.
Stepping further, the part 'If aForm.Name = "UserForm2" Then' results in
run-time error 438: "Object doesn't support this property or method".

Now why is that? And how can I refer to UserForm2 to pass this particular
form as an argument to a sub? UserForms("UserForm2") is not allowed, and
performing a test beforehand on the name of the UserForm apparently is not
allowed either.

Thank you,
Cooz
 
H

Helmut Weber

Hi Cooz,
Now why is that?

I don't know, however:

Sub ASub()
Dim i As Long
Load UserForm1
Load UserForm2
For i = 0 To UserForms.Count - 1
If UserForms(i).name = "UserForm2" Then Exit For
Next
Call anothersub(UserForms(i))
End Sub

Sub anothersub(x As Object)
MsgBox x.name
End Sub
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
C

Cooz

Hi Helmut,

Of course. Simple as that. Should have thought of this myself :).

Thank you. The reason why aForm.Name yields an error where UserForms(0).Name
doesn't still completely eludes me though.

Cooz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top