#1
|
|||
|
|||
Show userForm from variable
I have several documents which use the same userForms, and I'm trying to make a function to show them in a specific order (Not all will contain the same forms, not always the same order). I have created an array with the forms in the order they need, but I can't seem to use the contents of the array to show the forms. I can use the TypeName and a Select Case block to show the form, but since not all documents will have all of the forms, this won't compile across the board. Plus, if I need to add/delete/rename any of the userforms, it will require updating this section, which I would prefer not to be hard-coded.
Code:
' Create an array using the needed forms in order: Dim dialogs(4) As UserForm ' NOTE: Using 'NEW' doesn't make any difference to functionality Set dialogs(0) = frmDataInput Set dialogs(1) = frmSummaryInput Set dialogs(2) = New frmCondition Set dialogs(3) = New frmSummaryInput ' The following code would be in a separate function, common to all documents Dim i As Integer Dim ct As Integer ct = UBound(dialogs) For i = 0 To ct Set dialog = dialogs(i) ' NOTE: This doesn't work: ' dialog.show ' Need to use 'Select Case' to activate forms Dim name As String name = TypeName(dialog) Select Case name Case "frmSummaryInput" frmSummaryInput.show Case "frmDataInput" frmDataInput.show Case "frmCondition" frmCondition.show End Select Next I |
#2
|
|||
|
|||
When I search the object browser, I find 4 hidden items with the .show and .hide methods:
Library.........Class......Member Unknown5...._Form....Show Unknown6...._Form....Show Unknown7...._Form....Show Unknown8...._Form....Show The UserForm class is missing these methods, so I'm assuming that the UserForms used in the application are a subclass of this UserForm class, utilizing these hidden _Form classes? (my terminology here may not be completely accurate) Is there any class that exposes the .show method that can be used as a variable to do what I need? |
#3
|
|||
|
|||
This seems to show all in the array:
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 1/31/2018 Dim Dialogs(2) As Object Dim lngIndex As Long Set Dialogs(0) = New UserForm1 Set Dialogs(1) = New UserForm2 Set Dialogs(2) = New UserForm3 For lngIndex = 0 To UBound(Dialogs) Dialogs(lngIndex).Show Next lbl_Exit: Exit Sub End Sub |
#4
|
|||
|
|||
Thanks for the response.
I tried to replicate your results and at first, it wasn't working. Then I noticed that you dimmed the array as 'Object', when I had it as 'UserForm'. That seems to be the trick in getting it to work. Odd that the generic 'Object' works, but the specific 'UserForm' doesn't. I'm a bit rusty in VisualBasic, so these issues are easy to stumble upon. I believe I now have what I need to get what I wanted to work. Thanks again. |
#5
|
|||
|
|||
Ok, apparently I'm even rustier in VBA than I had originally thought. I thought I had everything, but I'm having a problem that I believe I've run into before, but don't remember how to solve.
I'm passing the userForm from one function into a class, and instead of passing the UserForm itself, apparently the object I'm receiving as a parameter is the UserForm.Controls object. Code:
Function test() Dim frm As UserForm Set frm = New UserForm1 AddAsObject (frm) AddAsUserForm (frm) End Function Function AddAsObject(ByRef frm As Object) ' This works because the variable received is a 'Controls' object End Function Function AddAsUserForm(ByRef frm As UserForm) ' This DOESN'T work because the variable received is NOT a 'UserForm' object End Function |
#6
|
|||
|
|||
For now, I have solved my issue with a bit of a hack. In the subFunction, I check the typeof the variable 'frm', and if it is 'Controls', I loop through the controls to get the parent UserForm. (NOTE, this won't work if there are no controls on the UserForm, but at least it is acceptable until I find a more proper way to make this work since that's never going to be the case in my program)
Code:
If (TypeOf frm Is Controls) Then Dim ctrl As Control For Each ctrl In frm If (TypeOf ctrl.Parent Is UserForm) Then Set frm = ctrl.Parent Exit For End If Next End If Thanks. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Assigning a string variable to a userform label caption | Larry_1 | Excel Programming | 3 | 12-18-2017 06:59 AM |
Show Userform when Content Control CheckBox ticked | derajlance | Word VBA | 1 | 05-13-2016 01:55 PM |
Using Userform.Show in Excel 2013 | BillCPA | Excel Programming | 0 | 05-04-2015 09:52 AM |
Command button to show userform suddenly stopped working | jpb103 | Word VBA | 0 | 05-22-2014 06:05 AM |
Show userform without losing document focus? Or other method to get a graphic to pop? | AlexR | Word VBA | 7 | 03-31-2013 12:17 PM |