Consulting

Results 1 to 7 of 7

Thread: Solved: Is MyUserForm in ThisWorkbook

  1. #1

    Solved: Is MyUserForm in ThisWorkbook

    Sub example()

    'trying to determine if a particular UserForm is in 'ThisWorkbook'

    Dim varObj As Object
    'For Each varObj In VBProject 'or
    'For Each varObj in MSForms 'or other???
    If varObj.Name = "UserForm1" Then
    var1 = 1
    Exit For
    End If
    Next

    If var1 = 1 Then MsgBox """UserForm1"" is in the workbook."

    End Sub


    I have a workaround to my issue, but I know someone can probably just quickly answer my question! Thanks for your help!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Does this help?

    [vba]

    Sub example()
    Const vbext_ct_StdModule As Long = 1
    Const vbext_ct_ClassModule As Long = 2
    Const vbext_ct_MSForm As Long = 3

    'trying to determine if a particular UserForm is in 'ThisWorkbook'
    Dim wb As Workbook
    Dim varObjs As Object
    Dim varObj As Object
    Dim var1
    For Each wb In Application.Workbooks
    On Error Resume Next
    Set varObjs = wb.VBProject.VBComponents
    On Error GoTo 0
    If Not varObjs Is Nothing Then
    For Each varObj In varObjs
    Select Case varObj.Type
    Case vbext_ct_MSForm
    MsgBox wb.Name
    Exit For
    End Select
    Next varObj
    End If
    Next

    If var1 = 1 Then MsgBox """UserForm1"" is in the workbook."

    End Sub
    [/vba]
    ____________________________________________
    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

  3. #3

    Yes. Thanks!

    I do not see a way to extract a userform's (name), but your code adequately reflects if there are any userforms present. I can make this work! Thank you very much for your help!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The userform's name would be

    [vba]

    varObj.Name
    [/vba]
    ____________________________________________
    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

  5. #5
    Yes-that works perfectly. I don't remember now what I was smoking, but it must have been good. I think it only works on Saturdays! Thanks!

    [VBA]
    Sub asdlfk()
    Dim varObj As Object, varObjs As Object
    Set varObjs = ThisWorkbook.VBProject.VBComponents
    If Not varObjs Is Nothing Then
    For Each varObj In varObjs
    Select Case varObj.Type
    Case 3
    If varObj.Name = "UserForm1" Then
    var1 = 1
    Exit For
    End If
    End Select
    Next varObj
    End If
    If var1 = 1 Then MsgBox """UserForm1"" is in the workbook."
    End Sub
    [/VBA]

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If only a True/False is needed, there is no need to loop through components.
    [VBA]Function WorkbookContainsUF(wb As Workbook, ufName As String) As Boolean
    On Error Resume Next
    WorkbookContainsUF = (wb.VBProject.VBComponents(ufName).Type = 3)
    On Error GoTo 0
    End Function

    Sub test()
    MsgBox WorkbookContainsUF(ThisWorkbook, "UserForm1")
    End Sub[/VBA]

  7. #7
    Awsome! I love it!

Posting Permissions

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