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,204 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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
Comment Utility
<<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
Comment Utility
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
Comment Utility
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
excel file 5 42
Using VB6 to write Excel Spreadsheets - 5 15
TT Status Chang 3 22
Add macros on Open 4 0
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now