Modeless UserForm

B

Brandt

I sure hope someone can help me with this one. I have tried everything I
know of (which isn't all that much). I have an excel application that
basically creates a series of multi-pages on a userform based on a directory
hierarchy that it is pointed to. On the multi-pages are buttons for each
file that exists in the folder that the specific multi-page represents. What
I have created is just a simple way to access our company’s in-house software
that exists on our server. To do this I had to use what I believe is called
"design mode" so that the application can create the buttons and add the code
for the buttons to the code module. This all works fine, except that it has
become apparent that this userform must be modeless so that it can be left
open. The code I use to call the form is:

VBA.UserForms.Add(TempForm.Name).Show vbModeless

If I leave off the 'vbModeless' portion everything works fine (except that
the form is modal). With the 'vbModeless' included, the userform is shown
for about 1/2 a second and closes and all remaining code executes.

Can anyone explain what is going on here? I can, of course, post additional
code if necessary.

Thanks

Brandt
 
J

Jon Peltier

When your form is modal, the calling routine stops and waits until the
userform is closed and returns execution to it. When the form is modeless,
the procedure shows the form, then continues on its merry way.

What works best for a modeless form is to have one procedure that does
everything up to the showing of the form, then ends, and have another
procedure start when the user form is dismissed (i.e., called by the Close
button code).

- Jon
 
B

Brandt

Jon,

Thanks for your reply. That is exactly what I have (see code below). I've
used modeless userforms successfully in the past, but I can't get it to work
in this case.

Here is the code that auto runs when the application is opened. Everything
after this subroutine is code written as the form is created and should
require user imput to execute.

'----------------------------------------------------------------
Sub OpenFiles()

Dim SubDirectory As String
Dim UserChoice As Variant
Dim FolderArray As Variant
Dim SubFolderArray As Variant
Dim NumOpenApps As Integer
Dim i As Integer
Dim UserDirTest As Integer
Dim ctl As Control

Directory = Sheets("Sheet1").Range("DirDefault").value
UserDirectory = Sheets("Sheet1").Range("DirUser").value

If NumOpenWBs() = 1 Then
Application.Visible = False
Else
ActiveWindow.WindowState = xlMinimized 'application.WindowState =
xlMinimized
End If


Set TempForm = ThisWorkbook.VBProject.VBComponents("UserForm1")

Call Clearform

' Hide VBE window to prevent screen flashing
Application.VBE.MainWindow.Visible = False

FolderArray = GetDirNames(Directory & "\", UserDirectory) 'See mod2 _
UserDirectory Does Not take a "\" Because it is Essentially a Subfolder


'Add Main MultiPage
Call AddMPage(FolderArray, MainMPageTop, MainMPageLeft, MainMPageHeight,
MainMPageWidth)

For i = LBound(FolderArray) To UBound(FolderArray)
SubDirectory = Right(FolderArray(i), Len(FolderArray(i)) -
InStrRev(FolderArray(i), "\")) & "\"
SubFolderArray = GetSubDirNames(CStr(FolderArray(i)) & "\")
Call AddSubMPage(SubFolderArray, SubMPageTop, SubMPageLeft,
SubMPageHeight, SubMPageWidth, i)
Call AddButtons(FolderArray(i), SubFolderArray, i + 2)
Next

Call BuildForm

VBA.UserForms.Add(TempForm.Name).Show 'vbModeless
'DoEvents

End Su
'-------------------------------------------------------------------------------------

Thanks

Brandt
 
J

Jon Peltier

You're constructing the userform on the fly? I don't know it that has
anything to do with it, I'm just intrigued.

- Jon
 
B

Brandt

Jon,

I am constructing the form as the application runs (on the fly). I believe
that is the cause of the problem. Are you aware of modeless userforms not
working in such a case?

Brandt
 
J

Jon Peltier

I don't know that it's the cause of your problem.

In my youth I often tried building forms on the fly, but my recent attempts
are much simpler. I usually build in whatever controls I may need, and hide
the ones I'm not using at a particular time. If I do add controls, they are
usually ones intended to hold information but not respond to events.

- Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top