Please Wait Dialog

I

Ian D McLean

Hello

I would like to create a modal dialog box which loops checking for the
existence of a file (ok.txt) in a specific location (c:\locationname). The
ok.txt file is being created by another application. The dialog box should
display a message ("Searching for file please wait ...") and an OK button
which only becomes available to the user once the ok.txt file has been
found. Once the ok.txt file has been found the message would change ("File
found press OK to continue") and the OK button would become available.
Clicking the OK button would then close the dialog box.

I'm sure that for most of you this is a pretty basic thing to do, but I have
very little knowledge of any kind of programming and I would like to impress
my colleage who uses a different programming language and says that he can't
do this!!!

Eagerly awaiting your replies.

Ian
 
J

Jay Freedman

Hi, Ian,

It's a bad idea to have only a disabled OK button in a modal form. What if
the other application crashes and never creates ok.txt? You really want a
Cancel button, too...

Create a userform in your template (see
http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.htm). On the form,
draw a label (default name Label1) and two command buttons named cmdOK and
cmdCancel. For cmdOK, set the caption to "OK". For cmdCancel, set the
caption to "Cancel" and set the Cancel property to True. Then copy and paste
the following code into the userform's code window:

Option Explicit
Private Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
Dim FormClosed As Boolean

Private Sub cmdCancel_Click()
FormClosed = True
Unload Me
End Sub

Private Sub cmdOK_Click()
Unload Me
End Sub

Private Sub UserForm_Activate()
Do
DoEvents
If FormClosed Then Exit Do
Sleep 500 ' half-second delay
Loop Until Len(Dir("c:\temp\ok.txt")) > 0

If FormClosed Then
Unload Me
Else
Label1.Caption = "File found. Press OK to continue."
cmdOK.Enabled = True
cmdOK.SetFocus
End If
End Sub

Private Sub UserForm_Initialize()
Label1.Caption = "Searching for file. Please wait..."
cmdOK.Enabled = False
FormClosed = False
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
FormClosed = True
End Sub

Also create a regular code module in the template, and add this macro to it:

Sub DoWaitForFile()
WaitForFile.Show
End Sub

Alternatively, if you already have a macro that starts the other application
(the one that creates ok.txt), you can simply add the WaitForFile.Show
command to that macro just before starting the app.
 
M

Malcolm Smith

In addition, wouldn't it be better if the other application raised an
event which this application trapped?

Malc
 
J

Jay Freedman

Malcolm said:
In addition, wouldn't it be better if the other application raised an
event which this application trapped?

Malc

Hi, Malc,

Yes, it would be better... but it's feasible only if you have source access
to the app in question and can make it raise an event. If it's some
third-party black box, then you need some kludge like waiting for a file.
 
M

Malcolm Smith

Indeed, but since the application is making something like an ok.txt file
then I was thinking that this was an in-house application. Failing that,
yes, the creation of a file or something would be one of the least-worst
options.

It's all yuck! But I would do it with a dialog box, a timer control (set
to go off at 1,000 milli-second intervals) and the FileSystemObject (in
the Scripting library) to check for whether the file exists or not.

- Malc
www.dragondrop.com
 
C

Chip Orange

What a great example, and how kind of you to post it for all of us!!! :)

There's one thing I'm not clear on however, there are several procedures
named something like:

Private Sub UserForm_*



But, later, there's a:

WaitForFile.show


Should these procedures be named WaitForFile_* instead


thanks.

Chip
 

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