What is VBS and what is it good for?

VBS or Visual Basic Scripting Edition is a scripting language developed by Microsoft. The programming language that powers a script is a lighter version of Visual Basic, therefore you’ll find many similarities between the code behind a VBS script and a VBA macro.

Even though a VBS script can do some of the things a VBA macro does, the purpose it is somehow different mainly because VBA requires installation of the Microsoft Office package, while VBS doesn’t. The only thing you need to develop and launch a VBS script is a text editor (e.g. Notepad).

Okay, so what is VBS good for?”

Here are some things I use VBS for:

1. Automatically backup files/folders at a specific time interval;

2. Monitor different applications required for other automations (e.g. there were situations I needed some VBA macros to run 24/7. However, you know that Excel crashes from time to time, therefore I used VBS to check if Excel is still running and alert me if not);

3. As an extension of the above point, I use VBS to launch VBA macros when they crash;

4. Shut down PC at a specific time;

5. Keep PC from automatically getting locked on idle (very useful when I’m working for home so I can see if there is anything work-related that requires my immediate assistance);

6. As part of a VBA macro, I use a VBS script to detect if an upload window from Chrome is open and send a path to the upload file (if you create a Selenium-based VBA macro and you need to make uploads through that Upload window, VBA freezes until the window disappears and that’s where VBS is useful).

Bottom line, I mainly use VBS as an assistant that checks if everything works as expected and take the required actions if not.

How to create a simple VBS script?

Want an example of a VBS code? Here is the code I use to keep my PC from going idle.

Set WshShell = WScript.CreateObject("WScript.Shell") ‘Creating the object that can use the keyboard
Do ‘Initiating a Loop
     Wscript.Sleep 60000 * 5 ‘waiting for 5 minutes (<1000> means one second because the time is taken in milliseconds)
     WshShell.SendKeys "^" ‘hitting the CTRL key
Loop ‘Continuing the loop

Just copy the above, put it in a Notepad and save it with whatever name you want and with the .vbs extension. To run it right-click on the file and click on Open.

Note: Once the above script is launched, it cannot be stopped unless you restart your PC or you end it from Task Manager -> Details.

That’s it! I hope you learned something new today!

PS: If you want to support this website, you can do it through the Show Your Support page!

Leave a Reply