Solved

non modal message box API that does not cause vba program to pause (without using a Userform)

Posted on 2013-01-11
6
2,351 Views
Last Modified: 2013-01-16
My outlook mailEx program runs when Outlook does a send/receive. The program occasionally displays informational messages that I want the user to see. But, I don't want the messages to stop MailEx processing.

I solved this completely with a subroutine that displays a non-modal form.  In MailEx I simply code
       line33: call msgfastbox("line33","This message will stay on screen")
                    debug.print "But the program will continue to run"

I want to extend the this idea to Excel and Access, so I wonder if there is an API that does something similar but doesn't require a userform.  

I have googled for a while and I and found some web pages that say "NO THERE IS NO SUCH API". So, please,  don't point me to a potential solution, unless you are really sure  that it will run non-modally.  

For instance someone suggested I use "declare function myfunc Lib "User32" Alias "MessageBoxA"".   Perhaps I used it wrong but the last line of code at http://www.tek-tips.com/faqs.cfm?fid=4699 WAITS for the user to click OK. I tried variations, but they all waited for user response.

In case you are interested, here is the Outlook code that I am currently using. It does exactly what I want. It displays a non-modal message  box then continues executing the main program.


Sub msgFastBox(id As String, ByVal Text As String, Optional MilliSeconds)
' displays message non-modally for a few seconds.
'
' if no seconds are specified, the box will remain open until it is dismissed
' or until another msgFastBox is requested
Call writelogfile(id, Text)
frmMsgFastBox.Text.MultiLine = True
frmMsgFastBox.Text = Format$(Date, "MM/DD/YYYY") & " " & Format$(time(), "HH:MM:SS") & id & " " & Text
frmMsgFastBox.Show False
DoEvents
If Not IsMissing(MilliSeconds) Then
    Sleep MilliSeconds
    Unload frmMsgFastBox
End If
End Sub

Open in new window

0
Comment
Question by:rberke
  • 3
  • 2
6 Comments
 
LVL 57
ID: 38766771
You would use the CreateDialog API call:

http://msdn.microsoft.com/en-us/library/ms644996%28v=VS.85%29.aspx#modeless_box

 But really, this is nothing more then creating a message box form on the fly.

I'd keep things simple and just create the forms in Excel/Access to accomplish this.

Jim.
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 38767111
I have a popup form that I use for this.  It is just a small form (frmMsg) with two controls (lbl_message, and cmd_Continue).  The code behind the form has a public subroutine (Start) which I call to open the form, populate the comment and set the TimerInterval.

See attached.

From the immediate window (or your code), you can open the form with a line like:

form_frmMsg.start "message", 5

This will display "message" for 5 seconds, then automatically close the form. Or, the user can click the continue button to close the form.
MessageForm.accdb
0
 
LVL 5

Author Comment

by:rberke
ID: 38769307
Jim, That links needs to be translated to vba before I can understand it  

fyed: your solution is virtually identical to the solution I described in my first post.  I will give you a few points mainly because you saved me the trouble of translating my solution into access.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 38774296
<<fyed: your solution is virtually identical to the solution I described in my first post.>>

 For another alternative, see attached.

<<Jim, That links needs to be translated to vba before I can understand it >>

 It's not something that would translate to VBA.  You'd need to create the dialog template with API calls, then call CreateDialog().  That would all be with API's except for the calls themselves to the API's.

 Reason Dale and I have things we use like we have posted; it's simpler and easier to manage.

Jim.
ProgressBar.zip
0
 
LVL 5

Author Comment

by:rberke
ID: 38781775
The progress bar implementations you folks have posted are equivalent to the userform solution that I previously developed for Outlook.  

But, since there is no neatly packaged API, I will give points for those contributions.

By the way, I came up with the following approach which works in in every MS Office environment.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub test()
Call modelessMsgbox("ugly but it works")
End Sub
Sub modelessMsgbox(text)

' First create program ModelessMsg.vbs in c:\aaatmp\
'          Set objargsinall = WScript.Arguments
'          If WScript.Arguments.Count = 0 Then
'              MsgBox "ModelessMsg.vbs not called properly: no message passed"
'          Else
'              WScript.echo objargsinall(0)
'          End If
Set wShell = CreateObject("wscript.shell")
wshCommand = "taskkill /IM wscript.EXE /F" ' there must be a way, but this makes sure there is only one such msgbox displaying
wShell.Run wshCommand
Sleep 500
wShell.Run "wscript c:\aaatmp\test.vbs " & Chr(34) & Replace(text, Chr(34), "'") & Chr(34)

End Sub

Open in new window

0
 
LVL 5

Author Comment

by:rberke
ID: 38781861
I should point out that my initially posted vba code had an optional interval timer.
If I used a 5 second timer, the message would not continue until the 5 seconds expired even if the user dismissed it.  

I don't think the alternatives posted by the experts have that problem, so they are "better" solutions.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question