loading
VBS is one of the most popular programming languages on instructables so there surely will be several instructable guides on programming VBS. However, most of them rush through commands and just show the basic way to do them. They also have a lot of the same basic material. I decided to search all around instructables and w3schools.com until I had a very good list of commands. When I did I started this tutorial that really shows a lot of detail about each command. Now lets start making VBS scripts... 

You will need...

-A computer running Windows 98 or higher (No Macs)
-Notepad
-Time
-A brain

Step 1: First Things First...

Open Notepad

Start>All Programs>Accessories>Notepad

Lets get ready to script!

Step 2: Save Me!

Before we begin I should teach you how to save a vbs file in notepad.
To save a file you have to click File>Save As

Then type
name.vbs

Where it says name you can put whatever you want but it MUST end with .vbs

Step 3: Message Boxes

This makes an error message how you like it.

msgbox ("Message goes here",0+16,"Title goes here")

if the user is supposed to make a decision the variable can be added like this.

variable=msgbox ("Message goes here",0+16,"Title goes here")

The numbers in the middle vary what the messge box looks like.
Here is the list

0 - ok button only
1 - ok and cancel
2 - abort, retry and ignore
3 - yes no and cancel
4 - yes and no
5 - retry and cancel
TO CHANGE THE SYMBOL (RIGHT NUMBER)
16 - critical message icon
32 - warning icon
48 - warning message
64 - info message
DEFAULT BUTTON
0 = vbDefaultButton1 - First button is default
256 = vbDefaultButton2 - Second button is default
512 = vbDefaultButton3 - Third button is default
768 = vbDefaultButton4 - Fourth button is default
SYSTEM MODAL
4096 = System modal, alert will be on top of all aplications

Note: There are some extra numbers. You just have to add them to the numbers already there like

msgbox ("Hello World", 0+16+0+4096)

Step 4: Input Boxes

Input boxes allow the user to INPUT a text string. Here is the code for one.

variable=inputbox ("Message","Title","Default Text")

Any of the text between the commas is optional by just not putting anything there like

variable=inputbox ("Type a Message",,"Message")

Step 5: Working With Variables

Math is one of the most simple tasks for a computer. VBS can do complex math in a snap.

Vbs mainly does algebraic math but you can do other than that. First we should make a variable. That is the easiest task in the scripting world.

dim variable name here

Variables can be placed where text goes like this...

Msgbox (var)

The only rule for naming variables is no spaces allowed.
To do math, in between each part of the equation must have a space like so.

y + 5 = 6 * 8

You can also treat an equation like a variable...

Msgbox (5.5 * 3)

Note: VBS will give you an error specifically for dividing by zero so just don't do that.

Step 6: Strings

Strings are variables that hold text. You can make a string like this.

"This is a text string"

VBS will let you treat the text string as a variable but you can make a text string a variable as easy as this.

dim name

name = "This is a text string"
 
Note:You have to have the quotation marks or else the script will think your talking about a variable.

Note 2:Text strings do have a capacity limit to how much information they hold so be careful.

Step 7: Decisions, Decisions...

What if you needed the program to check if a variable was at a certain value? In programming that is extremely easy. The decision making command is called the IF THEN ELSE statement. An example program would be...

if 200 > 16 then
msgbox ("200 is greater than 16")
else
msgbox ("200 isn't greater than 16")
end if

That was a foolish example (since 200 will always be greater than 16) of how the if statement works. So now you see IF 200 > 16 THEN let the user know ELSE tell the user it isn't then END the IF statement.

So now you can make a program that will do one of two things. Can you do more? Yes! That is called a nested IF statement. For example...

if 200 > 16 then
msgbox ("200 is greater than 16")
else
if 200 = 16 then
msgbox ("200 is equal to 16")
else
msgbox ("200 isn't greater than 16")
end if
end if

This program will say if 200 is equal to, greater than or less than 16.

The same can be used with text strings.

if userinput = password then
msgbox ("Welcome")
else
msgbox ("Access denied")
end if

Note that 'userinput' and 'password' are variable holding text strings not text strings themselves.

Step 8: Doing the Loop

What if you want a program to repeat something forever or until something happens? Have I got a command for you! It's the do loop command! Its most basic use is this.

do
msgbox ("Hi")
loop

A more complex version would be

do until 24 > 8
msgbox ("Hi")
loop

A similar version of that code would be

do
msgbox ("Hi")
loop until 24 > 8

While the two codes look the same there is a difference. That is, the first code won't even go into the loop if the criteria is met before the loop is started while the second one has to go through the loop at least once because the 'loop until' is after everything in the loop.


Step 9: Important Command

One of the things that makes VBS special is it can work with other programs that are compatible with it by creating something called an object. In other words a VBS script can get more commands by controlling other programs. To use objects you have to let the script know what you are going to call it. Here is how you set an object...

set name = wscript.CreateObject("what the object is")

It is a good idea to set objects in the first lines of your code. There are two reasons for that.

1.It makes your program clearer to read.

2.It's impossible to use an object that hasn't been set. Its best to get that done first to avoid errors.

The next two steps will show two examples of objects.

Step 10: Shell

  Say you want your script to do something like run a program or DOS-ish commands. This object is perfect. It is called the Windows Shell object. Here is a simple way to use it to open a directory.

 set wshshell = wscript.CreateObject("wscript.shell")

wshshell.run C:\Windows

The C:\Windows folder should open.
Another command is sendkeys. It will, well, send keys, to applications. Say you want to send hello world into a program, just type.

set wshshell = wscript.CreateObject("wscript.shell")
wshshell.sendkeys "Hello World"

Another command is the Echo command which sends a message box with no picture, an OK button, and Windows Script Host in the title. An example is.

set wshshell = wscript.CreateObject("wscript.shell")
wshshell.echo "Hello World"

Step 11: Sapi

Did you ever want to make a computer talk? Sure you could open the text to speech window, but what if you want a computer to read results without any user interface?  Just use the SAPI (Speech Application Programming Interface) object.


Set Sapi = Wscript.CreateObject("SAPI.SpVoice")
Sapi.speak "Hello world"

The speak command can also use variables.


Set Sapi = Wscript.CreateObject("SAPI.SpVoice")
Sapi.speak variable

Step 12: Date and Time

These commands will show the date or time if used correctly. They can be treated like variables.

--------------------(Date)-------------------------
Date (Returns system date)
Day (Returns the day (1-31)from the date command) ex. Day (Date)
Month (Returns the month (1-12) from the date command) ex. Month (Date)
Year (Returns the year from the date command) ex. Year (Date)
--------------------(Time)------------------------
Time (Returns the current system time to the second)
Second (Returns the second (0-59) from the time command) ex. Second (Time)
Minute (Returns the minute (0-59) from the time command) ex. Minute (Time)
Hour (Returns the hour (0-23) from the time command) ex. hour (Time)

For example 

msgbox (Date)
msgbox (Time)

That is a simple program that shows the use of the Date and Time commands. 

A more elaborate program that uses all of the commands listed here and some extra can be found here .

Step 13: Reading Plain Text

Say you want the computer to read from a file and store the contents as a text string. Here is a code that does just that.


Dim Textstring

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objText = objFSO.OpenTextFile("File.txt", 1)

Textstring = objText.ReadAll

Step 14: Final Exam...

This time take what you have learned from this instructable and write a program by yourself. Comment it so we all can enjoy it.

Step 15: Bye

Thank you for reading this instructable. Comment anything cool you come up with so we can all share our discoveries. 
                                                                                                                                                            -Super_Nerd-

Step 16: Additional Steps: the String Function "Replace"

mohamadelmoussa asked for a command that would replace certain words with others in text strings. The replace command is definitely, what he was looking for. Here is the basic syntax.

Replace(string,"change this to","this")

Here is an example of using the replace function...

text="This is a good instructable!"
msgbox (Replace(text,"a good","an awesome"))

Thanks for the compliment example. 
<p>What is wrong with my code?</p><p>Set WshShell = WScript.CreateObject (&quot;WScript.Shell&quot;)<br>WshShell.SendKeys &quot;FirstWord&quot;<br>WScript.Sleep &quot;200&quot;<br>WshShell.SendKeys &quot;SecondWord&quot;</p>
<p>Input boxes allow the user to INPUT a text string. Here is the code for one.</p><p><br>variable=inputbox (&quot;Message&quot;,&quot;Title&quot;,&quot;Default Text&quot;)<br><br>Any of the text between the commas is optional by just not putting anything there like<br><br>variable=inputbox (&quot;Type a Message&quot;,,&quot;Message&quot;)</p><p>How do you make that certain text that you type in the box acctivates a command, like:</p><p>&quot;sing me a song&quot; and that opens song.mp3</p>
<p>How do you make a quiz, like with the input boxes and using &quot;if then else&quot;</p>
Wow thanx for it
<p>Take the line:<br>variable=msgbox (&quot;Message goes here&quot;,0+16,&quot;Title goes here&quot;)</p><p>For whatever message box you want the input on. You may want to change &quot;variable&quot; to something more relevant. Step 7 has more information on if-then conditionals, but the idea is, you want to make the conditional check to see if your variable is equal to a constant defined by the script host. It should be the button's text with &quot;vb&quot; in front of it, for example...</p><p>if variable = vbYes then</p><p> [your code here]</p><p>end</p><p>In that example, the code will only run if the yes button is clicked.</p>
<p>is there any way that when the you click yes on msgbox its makes a custom response, I have searched online, but it gives me error...</p>
<p>n = inputbox (&quot;Number to loop&quot;,&quot;Text2Speech&quot;,&quot;&quot;)<br><br>do While n &gt; 0<br>speech = inputbox (&quot;Text to speak&quot;,&quot;Text2Speech&quot;,&quot;&quot;)<br>Set Sapi = Wscript.CreateObject(&quot;SAPI.SpVoice&quot;)<br>Sapi.speak speech<br>n = n - 1<br>loop</p><p>or, to make someone go crazy make it loop forever</p><p>do<br>speech = inputbox (&quot;Text to speak&quot;,&quot;Text2Speech&quot;,&quot;&quot;)<br>Set Sapi = Wscript.CreateObject(&quot;SAPI.SpVoice&quot;)<br>Sapi.speak speech<br>loop</p>
Thank you for making this instructable. It has been very informative. I can't wait for you to make another one or add to this one.
very good job.<br>but I want some vb scripts about replacing words and sentences in a text,so can you provide it for me please.
I'll add that to this instructable soon.
I know you touched on this in the tutorial but can you please update (or message me) on how to make one of the text input msgboxes where it only goes away if they enter the user that it running the file's password
So you mean an inputbox that won't go away until the correct password is entered?
Yeah
Check my instructable<br> <a href="https://www.instructables.com/id/How-to-make-a-Computer-Lock/" rel="nofollow">How to make a computer lock</a>
Cheers
Your welcome.
A brain <br>LOL!
cool! <br>

About This Instructable

46,689views

27favorites

License:

Bio: I find a quote from Bill Gates makes a very good description. "Be nice to nerds. Chances are you'll end up working for one."
More by Super_Nerd:Make an assistant for your computer in 5 minutes Track Computer Usage in 2.5 minutes How to be a MAD Scientist! MU-AH HA HA HA! 
Add instructable to: