VBScript to Notepad/Wordpad

Eugene picture Eugene · Feb 22, 2012 · Viewed 8.8k times · Source

I'd like to write output from VBScript to notepad/wordpad in realtime. What's the best way to do this? I'm aware of sendkeys, but it requires that I parse the input for special commands.

Answer

Nilpo picture Nilpo · Feb 22, 2012

SendKeys is the only method for writing to a third-party application in realtime. Why don't you use CScript and write to the standard output instead? That is what it is meant for.

' Force the script to run in the CScript engine
If LCase(Right(WScript.FullName, 11)) <> "cscript.exe" Then
  strPath = WScript.ScriptFullName
  strCommand = "%comspec% /k cscript " & Chr(34) & strPath & chr(34)
  CreateObject("WScript.Shell").Run(strCommand)
  WScript.Quit
End If

For i = 1 to 10
  For j = 0 to 25
    WScript.StdOut.WriteLine String(j, " ") & "."
    WScript.Sleep 50
  Next

  For j = 24 to 1 Step - 1
    WScript.StdOut.WriteLine String(j, " ") & "."
    WScript.Sleep 50
  Next
Next