PDA

View Full Version : Setting the Name for a Dynamic UserForm



lionne
02-06-2013, 05:01 AM
Hello
I googled the topic, but couldn't find any hints that might solve my problem.

I am creating several dynamic userforms. Here is the code snippet:


Dim MetaParList As Object

Set MetaParList = Application.VBE.ActiveVBProject.VBComponents.Add(3)

With MetaParList
'.Properties("Name") = "Meta_Parlist"
.Properties("Caption") = "My Form"
End with


Setting the .Name property is obviosly not possible at run-time. Is there a way to give the custom name to the form? It's crucial, because I have different forms that are created at different time and I am having troubles referring to them, because the same form can be named "Userform1", and also "Userform2" depending on the code which was executed first...

I have tried to loop through the available forms and find, which will be the right form I have to refer at this point of time, but it doesn't work. Someone any ideas?


Dim objVBComp As VBIDE.VBComponent
Dim UserForm As Variant

For Each objVBComp In ActiveWorkbook.VBProject.VBComponents
If objVBComp.Type = VBIDE.vbext_ct_MSForm Then
If objVBComp.Name = "UserForm1" Then
Userform = "Userform2"
Else: Userform = "Userform1"
End If
End If
Next

With Userform
'it breaks at this point saying no object could be found.
End with

Any hints will be greatly appreciated.

Lionne

Bob Phillips
02-06-2013, 05:34 AM
Works fine for me, at least it does the first time. If you delete the form and re-run the code, it is failing there.