Listing Userform Names

G

Guest

Hi All
I am trying to list all userform controls. Part of this process requires
that I also show the userform name and caption.
In testing I discover an anomoly, that I cannot resolve - the first test
below works and correctly prints the userform name and caption. the second
does not work - can someone explain why please?

' this works
Debug.Print ufReport.Name, ufReport.Caption

' this does not work?
Dim uf As UserForm
Load ufReport
For Each uf In UserForms
Debug.Print uf.Name, uf.Caption
Next
Unload ufReport
 
G

Guest

I know you are looking for an explanation to your question. I do not have
that. However, you could iterate through the forms like this(if they are
already loaded)

For i = 1 To UserForms.Count
Debug.Print UserForms(i - 1).Name
Next i
 
G

Guest

While I do not have an answer to your good question as I too have wondered
about that.. here is another way of iterating through the loaded collection
of forms

For i = 1 To UserForms.Count
Debug.Print UserForms(i - 1).Name
Next i
 
B

Bob Phillips

That is because a user form does not exist in the userforms collection until
after it has been loaded.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
B

Bob Phillips

This will return nothing in most instances, unless the userforms have been
loaded.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
G

Guest

Bob,
I did say that.?

Bob Phillips said:
This will return nothing in most instances, unless the userforms have been
loaded.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
P

Peter T

Sub test()
Dim oVBP As Object ' VBProject
Dim oCmp As Object ' VBComponent
Dim ctl As Control

For Each oCmp In ThisWorkbook.VBProject.VBComponents
If oCmp.Type = 3& Then ' userform module
Debug.Print oCmp.Name
For Each ctl In oCmp.designer.Controls
Debug.Print , ctl.Name
Next
End If
Next

End Sub

You would need Trust access to VBProjects but not necessary to load the
forms

Regards,
Peter T
 
N

Nigel

I see. Nut do not understand why?

If I define uf as a userform my code fails, if I define it as an object and
then declare it in the loop by assigning each userform it does!!

Not very intuitive, but then you live and learn

Many thanks for the feedback
 

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