Sendkeys in Visual Basic Scripts

Sendkeys can also be used from a Visual Basic Scripts, which means you can ex. build a vbs file, that can be used for starting up applications, or executing steps that you often have to do.

This is an example on starting the Notepad and afterwards, as the first thing to do writing the word Hallo:

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad"

WScript.Sleep 100

WshShell.AppActivate "Notesblok"
WshShell.SendKeys "Hallo"

Set WshShell = nothing

Lets take a closer look on what the script actual do. The first step is to initiate the object:

set WshShell = WScript.CreateObject("WScript.Shell")

when the object is initiate, the Notepad application is being startet:

WshShell.Run "notepad"

To be sure that notepad is up and running, there has to be a kind of wait state before continuing. This is done be telling the script to Sleep x milliseconds:

WScript.Sleep 100

Now the Notepad application has to be activated:

WshShell.AppActivate "Notesblok"

Once Notepad has been activated, it is ready to receive keys. Which now can be send with the sendkeys command:

WshShell.SendKeys "Hallo"

Finally you have to clean up after you – which means, to stop / close the WshShell you have to set it to nothing.

Set WshShell = nothing

Thats it – now you have written the Visual Basic Script – and all you have to do is save it as a vbs file, or incorporate it into a web page using th vbscript tags.

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.