Share via

VB: make a Timed Message Box

Anonymous
2015-03-21T08:20:26+00:00

Update:  ANSWER  is at bottom of this top post,  thanks.


hi,  i am trying to get a message box to close after a specific amount of time.  as with some other examples below,  i would like to have the variables for:  time & message in the call,  eg:

    call goMB (seconds) "message"

if related, other call examples are:

Sub goBEEPS(B As Long, t As Double)   'gobeeps (2), (0.5)  'BEEPS  SECS

  For B = B To 1 Step -1

    Beep

    x = Timer   'err var not defined, not setup to work under option explicit

    Do

      DoEvents

      dt = Timer - x

      If dt < 0 Then dt = dt + 86400  '86400 no. seconds in a day, in case hit midnight & timer went down to 0

    Loop Until dt >= t

  Next

End Sub

Sub goTIMER(NumOfSeconds As Long) 'in (seconds eg: 0.5) as:  gotimer (1)  'seconds

    Application.Wait now + NumOfSeconds / 86400#

    Application.EnableEvents = True          'EVENTS

End Sub

'----------

SOME EXAMPLES OF TIMED MESSAGE BOXES FOUND:

CreateObject("WScript.Shell").PopUp "Message", 5      'new,  need a call sub goMB() "msg", seconds

Sub goMBA()   'Messagebox Timer, works but need to modify as:  call goMB (seconds) "msg"

    Const Title As String = "Self closing message box"

    Const Delay As Byte = 3                   'seconds

    Const wButtons As Integer = 16            'Buttons + icon

    Dim wsh As Object, MSG As String

    Set wsh = CreateObject("WScript.Shell")

    'MSG = "the date is:  " & Date

    MSG = Space(10) & "Hello," & vbLf & vbLf & "the date is:  " & Date

    wsh.Popup MSG, Delay, Title, wButtons

    Set wsh = Nothing

    Application.EnableEvents = True           'EVENTS

End Sub

Sub goMBB()    'in secs after: strpopmsg,

    Dim strPopMsg As String

    strPopMsg = "TEST 1" '& vbCr '& "TEST 2"

    CreateObject("WScript.Shell").Popup strPopMsg, 1, "Msg frm X", 64  '2 - 15 secs, not consistant

    Application.EnableEvents = True           'EVENTS

End Sub

Sub goMBC()   'yes but inconsistant time

    Dim AckTime As Integer, InfoBox As Object

    Set InfoBox = CreateObject("WScript.Shell")

    'Set the message box to close after 10 seconds

    AckTime = 3

    Select Case InfoBox.Popup("Click OK (this window closes automatically after 3 seconds).", AckTime, "This is your Message Box", 0)

      Case 1, -1

        Exit Sub

    End Select

End Sub

Sub goMBE()   'new example

    Dim t As Integer, obj As Object

    Set obj = CreateObject("WScript.Shell")

    t = 3 '<<<close after 3 sec.

    Select Case obj.PopUp("Click OK (or wait " & t & " secs)", t, "msgbox", 0)

    Case 1, -1

    Exit Sub

    End Select

End Sub

==========    ANSWER: 

Sub goMB(Msg As String, Wait As Long)    'CALL AS:  goMB "your message", 3

    CreateObject("WScript.Shell").PopUp Msg, Wait

End Sub

  goMB "Test xxx", 3   'works,  without using the word Call in front

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-03-21T23:42:46+00:00

but for ease of use would prefer to only have to enter something like: 

    goMB "message", seconds

Just create your own subroutine for it. Copy/paste this into a general Module (same place you put macros)...

Sub goMB(Msg As String, Wait As Long)

  CreateObject("WScript.Shell").PopUp Msg, Wait

End Sub

Call it exactly like you showed, for example...

goMB "Your message", 3

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

Answer accepted by question author

Anonymous
2015-03-21T20:49:42+00:00

Hi,

try this also,

Sub PopUp_Msg()

'Mar 21, 2015

Dim t As Integer, obj As Object

Set obj = CreateObject("WScript.Shell")

t = 3 '<<<close after 3 sec.

Select Case obj.Popup("Click OK (or wait " & t & " sec)", t, "msgbox", 0)

Case 1, -1

Exit Sub

End Select

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

16 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-03-21T23:38:00+00:00

    hi,  thanks.  gave that a try and it worked well for the msgbox.  like / getting more info from that for what still need for my preference.  not sure if i could get it myself after a few days work :) - research..  but i was just trying to close out a small task area that inluded this..   but don't have time to do it myself now.

    (think can mark that as an answer, later, after see if i can get something like sample result below).

    still would like to get a version that externalizes the use of seconds & the message as part of the call.

    do you think yours can be modified for a call like?:

        call goMB (seconds), "any message"

    thanks.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-03-21T09:06:14+00:00

    hi,  thanks for the reply.  i got that to work.  one thing that seems to be the same as other examples is the stalled amount of time for eg,  5 secs turns into 10 or 15 seconds.  i do have a lot of things running though.

    hope it is ok to wait to mark as answered until get a different version.  like your short version but for ease of use would prefer to only have to enter something like: 

        goMB "message", seconds

    if that is close to what is possible.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-03-21T08:31:40+00:00

    Give this single line of code a try (it leaves the MessageBox on for about 5 seconds... that's what the 5 does, make it whatever you want)...

    CreateObject("WScript.Shell").PopUp "Your Message", 5

    See this link for complete information...

    http://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.85).aspx

    Was this answer helpful?

    0 comments No comments