Consulting

Results 1 to 2 of 2

Thread: Setting the Name for a Dynamic UserForm

  1. #1
    VBAX Regular
    Joined
    Dec 2011
    Posts
    58
    Location

    Setting the Name for a Dynamic UserForm

    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:

    [vba]
    Dim MetaParList As Object

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

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

    [/vba]
    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?
    [vba]

    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
    [/vba]
    Any hints will be greatly appreciated.

    Lionne

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •