modal form with listbox shows selected but when bound to the VBE, it doesn't

Contributor

i have an issue with a userform. if i run the form modally (and from Excel), then when i select an item in the listbox, the check mark appears and the item is highlighted. The button reports that this item was checked and highlighted and shows the item name in the immediate window. but if i run the form so that it is bound to the VBE, then the button fails to report those items that were selected. the code for the module to load the userform is below. then the code for the userform will follow.

 

 

Sub ShowAddinLoader()

    Dim objUserform As UAddinLoader
    Dim lAppHwnd As Long
    Dim lMeHwnd As Long
    Dim lRes   As Long
    Dim objVbp As VBProject
    
    Const SUFCLASS As String = "ThunderDFrame"

    Set objVbp = Application.VBE.ActiveVBProject
    Set objUserform = New UAddinLoader
    Load objUserform
    
    Application.VBE.MainWindow.Visible = True
    
    lAppHwnd = Application.VBE.MainWindow.hwnd

    If lAppHwnd > 0 Then
        lMeHwnd = FindWindow(SUFCLASS, objUserform.Caption)
        lRes = SetParent(lMeHwnd, lAppHwnd)
        If lRes = 0 Then
            MsgBox "The call to Set Parent failed."
        End If
    Else
        MsgBox "Unable to get the window handle of the Excel Application."
    End If

    Set objUserform.Vbp = objVbp
    objUserform.Show vbModeless
    
End Sub

 

Private CobjVbp   As VBProject

Public Property Get Vbp() As VBProject: Set Vbp = CobjVbp: End Property

Public Property Set Vbp(oVbp As VBProject): Set CobjVbp = oVbp: End Property

as for the userform itself, just add a listbox and a button to report on the selected item. the code for the button is: 

 

    Dim X
    
    For X = 0 To UAddinLoader.ListBox1.ListCount - 1
        If UAddinLoader.ListBox1.Selected(X) = True Then
            Debug.Print UAddinLoader.ListBox1.List(X)
        End If

    Next X

 

so run the userform like so: UAddinLoader.show and then click the button. all appears good. then run the userform based on the procedure (that binds it to the VBE) and then check the button again. output is no good as nothing is returned.

 

how to fix?

2 Replies
can maybe an MVP look at it? i think an engineer might know why this is happening.