Link to home
Start Free TrialLog in
Avatar of Bryce Bassett
Bryce BassettFlag for United States of America

asked on

VBA Userform: Can't get selected items in listbox from outside a hidden userform

Working in Word 2016 VBA, running into a strange puzzle.   I have a master Form the user is interacting with in preparation for creating a document.  One button opens UserForm1, where they can select multiple items in ListBox1, then click OK.   This hides , but does not unload, UserForm1, because I'll need to pull the selected items into the document later.

When they have completed everything else on the master Form, it creates the document from a template, then reaches out to the still-open UserForm1 to pull in those selected items.  It recognizes that UserForm1 is still open, it can report the number of total items in ListBox1, but when we iterate ListBox1 to see which items are selected, it doesn't recognize that any are selected.  ListBox1 is set to multi select.

'this code is contained within a MasterForm, OKButton_Click() subroutine
'UserForm1 is hidden but still in memory 

set newdoc = Application.Documents.Add (Template:=mytemplate)

MsgBox UserForm1.ListBox1.ListCount  ' this works!
For x = 0 To UserForm1.ListCount - 1
    If UserForm1.ListBox1.Selected(x) = True Then  'this does not work.  No selections recognized
        MsgBox x   'eventually, stick listbox item(x) into newdoc
    End If
Next x
Unload UserForm1

Open in new window

BTW, I inserted the above code into a test button on UserForm1, substituting "Me" for UserForm1, and it works fine.  But it doesn't work outside the userform.

What am I missing?
Avatar of Norie
Norie

Why don't you 'pull' the selections made in the listbox in the first userform before you close/hide it and open the next one?
Have you got an 'On Error Resume' earlier in the code? If so, remove it.
I would expect UserForm1.ListCount (line 6) to raise an error.  A userform has no ListCount property/method
Avatar of Bryce Bassett

ASKER

You're right, Graham.  That's a typo.  Line 6 should be
For x = 0 To UserForm1.LISTBOX1.ListCount - 1

Open in new window

My actual code it correct, but still doesn't work.  

Other thoughts?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am using Word 2007 and Windows 10
Sorry to be slow in getting back to you.    I have wrestled with this and still having the problem.  My master form is a document creation dialog, which presents the user with a couple dozen customization options.  Each time I hide the child form but leave it in memory.  By the time they have finished and are ready to hit "Create Document" there are 8 or so of these hidden forms.   Just a way to avoid having a massive multi-page master form.

Anyway, the create routine tries to grab settings and input from each of these open userforms, then unload it.  No matter what I do I cannot simply address them by their form name as illustrated in my earlier code.  The workaround I came up with is to iterate the open forms, find the one matching the name I want to address, and assign it to a public object.  Then I can pull information from it just fine, using the FoundForm name, then unload it.  Extra steps but works quite well.   Thanks again for your suggestions.

Public FoundForm as Object

(in main module:)
FindForm ("ExecutiveSummaryForm")
If FoundForm is Nothing then GoTo skipthis
If FoundForm.OptionButton1.value = true then do stuff...
If FoundForm.ListBox1.Selected(1) then do stuff
etc.
Unload FoundForm
skipthis:

'=================== 
Sub FindForm(ByVal name2find As String)

Dim uForm As Object

For Each uForm In VBA.UserForms
    If uForm.Name = name2find Then
        Set FoundForm = uForm
        Exit For
    End If
Next uForm

End Sub

Open in new window