Search code examples
vbscript

Type variable string as text VBS


I'm new to VBS, just trying to figure out how I can run a script which prompts the user for input, & then types that result as part of a sentence?

I'm using WshShell.SendKeys as I want the script to be flexible & able to enter the text into different programs, would there be a way to have this type something that was entered into an InputBox?

Thanks!


Solution

  • This is a very simple example of how to get InputBox to take your input, then send the keys to a command prompt. I think you can work out how to adapt this for any other program pretty easily.

    Option Explicit
    
    Dim a
    a = InputBox("Enter your name", "Example")
    
    Dim sh
    Set sh = WScript.CreateObject("WScript.Shell")
    sh.Run "cmd"
    WScript.Sleep 200 
    sh.AppActivate "C:\Windows\system32\cmd.exe" 
    WScript.Sleep 200 
    sh.SendKeys "echo Hello " & a & "!"
    sh.SendKeys "{ENTER}"
    

    When you run this and enter the word "CrowStorm", CMD will open and echo the sentence "Hello CrowStorm!"