4

Just wondering how i could have a MSgbox that displays the value of a variable as it constantly changes. Basically a number has one added to it everytime it loops. I want to display that in a MSGbox that doesnt have to open a million windows

Monke
  • 45
  • 1
  • 3
  • Both `MsgBox` and `InputBox` are modal. If you want to display a modeless window, you'll need to launch an ActiveX server or OLE window (like Internet Explorer) or create an HTA. Alternatively, you could output the variable's value to STDOUT using CSCRIPT. – Bond Mar 19 '14 at 17:27
  • Check [this WSH VBS GUI](https://stackoverflow.com/a/47111556/2165759) solution. [This answer](https://stackoverflow.com/a/22546774/2165759) may be helpful also. – omegastripes Nov 09 '17 at 15:24

4 Answers4

7

A workaround would be to use PopUp

Set objShell = WScript.CreateObject("WScript.Shell")
For i = 1 To 3
    objShell.Popup i, 1, "AutoClose MsgBox Simulation", vbInformation+vbOKOnly
Next

This will "autoclose" the MsgBox lookalike after 1 second

Pankaj Jaju
  • 5,371
  • 2
  • 25
  • 41
3

You can't do this with the default VBScript dialog elements, like MsgBox, WScript.Echo or Popup. You need to build a custom dialog using the Internet Explorer COM object:

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "about:blank"

While ie.ReadyState <> 4 : WScript.Sleep 100 : Wend

ie.ToolBar   = False
ie.StatusBar = False
ie.Width     = 300
ie.Height    = 200

ie.document.body.innerHTML = "<p id='msg'>0</p>"

Set style = ie.document.CreateStyleSheet
style.AddRule "p", "text-align: center;"

ie.Visible = True

i = 1
Do
  ie.document.getElementById("msg").innerText = i
  i = i + 1
  WScript.Sleep 2000
Loop Until i > 10

or use an HTA instead of plain VBScript:

<head>
<title>Test</title>
<HTA:APPLICATION ID="oHTA"
  APPLICATIONNAME="Test"
  SCROLL="no"
>
</head>

<style type="text/css">
  p {text-align: center;}
</style>

<script language="VBScript">
  window.resizeTo 300, 200

  Set sh = CreateObject("WScript.Shell")

  Sub Window_onLoad
    For i = 1 To 10
      msg.innerText = i
      Sleep 2
    Next
  End Sub

  Sub Sleep(t)
    sh.Run "ping -n " & (t+1) & " 127.0.0.1", 0, True
  End Sub
</script>

<body>
<p id="msg">0</p>
</body>
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
2

One more solution, uses HTA window, without temp files:

dim window, i

set window = createwindow()
window.document.write "<html><body bgcolor=buttonface>Current loop is: <span id='output'></span></body></html>"
window.document.title = "Processing..."
window.resizeto 300, 150
window.moveto 200, 200

for i = 0 to 32767
    show i
    ' your code here
next

window.close

function show(value)
    on error resume next
    window.output.innerhtml = value
    if err then wscript.quit
end function

function createwindow()
    ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356
    dim signature, shellwnd, proc
    on error resume next
    set createwindow = nothing
    signature = left(createobject("Scriptlet.TypeLib").guid, 38)
    set proc = createobject("WScript.Shell").exec("mshta about:""<script>moveTo(-32000,-32000);</script><hta:application id=app border=dialog minimizebutton=no maximizebutton=no scroll=no showintaskbar=yes contextmenu=no selection=no innerborder=no /><object id='shellwindow' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shellwindow.putproperty('" & signature & "',document.parentWindow);</script>""")
    do
        if proc.status > 0 then exit function
        for each shellwnd in createobject("Shell.Application").windows
            set createwindow = shellwnd.getproperty(signature)
            if err.number = 0 then exit function
            err.clear
        next
    loop
end function
omegastripes
  • 12,351
  • 4
  • 45
  • 96
0

This script will display how many loops it took and the value of a variable during the loop, and display the results in 1 message box after the loop is complete.

Dim RateOfChange, roc, watchvariable : roc = 1 : watchvariable = 1

for i=1 to 25
    watchvariable = (watchvariable * i) * 16
    RateOfChange = RateOfChange & "Iteration[" & roc & "]" & " - Value[" & watchvariable & "]" & vbcrlf
    roc = roc + 1
next

'NOTE uncomment this below to activate msgbox. 
'msgbox rateofchange
wscript.echo rateofchange
Rich
  • 4,134
  • 3
  • 26
  • 45