3

I have a small script that runs (basically fires a msgbox to tell people that their account passwords are about to expire).

I want it to close itself after say 30 minutes, but cannot get the syntax right to do it, can anyone help?

Below is the section that calls the msgbox

if (daysLeft < warningDays) and (daysLeft > -1) then

Msgbox "Your Password Expires in " & daysLeft & " day(s)" & " at " & whenPasswordExpires & chr(13) & chr(13) & "Please ensure that you change your password before" & chr(13) & "its expiry time to prevent any disruption of service.", 4144, "PASSWORD EXPIRATION WARNING!"

End if

--

Gavin.

Gavin
  • 33
  • 1
  • 1
  • 4
  • 1
    possible duplicate of [Is there a way to get a MessageBox to automatically dismiss after a set interval of time?](http://stackoverflow.com/questions/10252237/is-there-a-way-to-get-a-messagebox-to-automatically-dismiss-after-a-set-interval) – Helen Apr 30 '14 at 07:04

4 Answers4

11

The Popup() method of the WShell object can timeout after a specified number of seconds. Here's an example that should disappear after 30 minutes.

CreateObject("WScript.Shell").Popup "Hello!", 1800, "Title"
Bond
  • 16,071
  • 6
  • 30
  • 53
  • Works really good,just remember that the time set as 1800 is in seconds. – Miguel Rubio Mar 27 '18 at 11:30
  • Is there a way to make this popup SystemModal or a way to keep it on top of other windows? – Ben Aug 15 '19 at 13:44
  • Add `,4096` to the end of the command to open as a system modal message box that appears in a top most window. – Ben Aug 15 '19 at 14:08
  • Note that the timeout works in vbscript but not in Office VBA. If you want to use it in Office VBA, try this method that uses mshta.exe: https://stackoverflow.com/a/44837192/1898524 – Ben Aug 15 '19 at 14:24
  • I'm not the only one having issues with in Office VBA. Discussion and an alternative using mshta.exe are shown [here](https://stackoverflow.com/a/31143673/1898524). Note that the mshta method does not support carriage returns and always shows the message on the primary monitor. [This link](https://stackoverflow.com/a/57512186/1898524) shows a API MsgBox that supports timeout, carriage returns, and return values. @Bond, your answer here was an inspiration for me. I've been using VBA 20 years and never knew a timeout was possible until this week. – Ben Aug 16 '19 at 04:34
1

You wont be able to do it that way, you will need to use your own custom form and timer, because a message box can only close on user interaction.

  • This article suggests its possible though:http://autohotkey.free.fr/docs/commands/MsgBox.htm – Gavin Apr 29 '14 at 13:52
  • Gavin if you care to look towards the bottom of that article the mention of IfMsgBox and example syntax makes it clear that the documentation is not for a dialect of VBA, VB6, VB.NET or C# –  Apr 29 '14 at 17:02
0

echoing the point made by @user3585329, to do this you may want to create a HTA application, which can display the information in a internet explorer form.

  • This article suggests it is possible: http://autohotkey.free.fr/docs/commands/MsgBox.htm – Gavin Apr 29 '14 at 13:52
  • I don't know what that relates to, but i have not seen a timeout facility on the msgbox before see [link](http://ss64.com/vb/msgbox.html) or MSDN microsoft official webpage [link] (http://msdn.microsoft.com/en-us/library/sfw6660x(v=vs.84).aspx) – AWS Cloud Architect Rob Apr 29 '14 at 13:58
  • the link you have sent is from AutoHotKey which is a custom scripting language and so is not vbscript, but very similar syntax to it. As mentioned i think you will need a HTA application to create what you need – AWS Cloud Architect Rob Apr 29 '14 at 14:10
0

FIX that like this:

CreateObject("WScript.Shell").Popup "MessageHere", 10, "Title"

TimeOut is in SECONDS, not in miliseconds!

Diego Sayron
  • 190
  • 1
  • 6