0% found this document useful (0 votes)
136 views

VB Script

VBScript is a scripting language developed by Microsoft to be used with Microsoft products, mainly Internet Explorer. It is a lighter version of Visual Basic. The key differences between VBScript and JavaScript are that VBScript is not case sensitive, uses Function for functions instead of curly braces, and is capable of both client-side and server-side scripting while JavaScript is only client-side. VBScript code can be embedded within HTML using the <script> tag.

Uploaded by

jaya2090
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

VB Script

VBScript is a scripting language developed by Microsoft to be used with Microsoft products, mainly Internet Explorer. It is a lighter version of Visual Basic. The key differences between VBScript and JavaScript are that VBScript is not case sensitive, uses Function for functions instead of curly braces, and is capable of both client-side and server-side scripting while JavaScript is only client-side. VBScript code can be embedded within HTML using the <script> tag.

Uploaded by

jaya2090
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

VBScript

VBScript stands for Visual Basic


Script, a scripting language
developed by Microsoft to be used
with Microsoft products, mainly
Internet Explorer.
It is a light version of Microsoft Visual
Basic and the VBScript syntax is very
similar to that of Visual Basic

Difference between JavaScript and


VB script

The syntax for JavaScript is similar to the syntax for the C+


+ programming language.
Since VBScript is a subset of Visual Basic for Applications,
JavaScript is case sensitive, while VBScript is not
JavaScript uses curly braces to denote functions, whereas
VBScript uses FunctionEnd Function .
VBScript is both a client and server-side scripting language,
while JavaScript is only a client-side scripting language.
The file extension for VBScript is .vbs ; the file extension for
JavaScript is .js.
JavaScript is the default scripting language for most
browsers, while VBScript is not. It must be specified as the
scripting language.

<html>
<head>
<title>My First VBScript Code!</title>
</head>
<body>
<script type="text/vbscript">
document.write(Hello my name is amrit")
</script>
</body>
</html>

<!DOCTYPE html>
<html>
<body style="background-color:lightgrey;">
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<a href="http://www.w3schools.com">Contact us </a>
<img src="w3schools.jpg" alt="W3Schools.com" width="104"
height="142">
</body>
</html>

Form
<form>
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<inputtype="radio"name="gender"value="male"checked>Male<
br>
<inputtype="radio"name="gender"value="female">Female<br>
<button type="button" >Click Me!
</form>

String Concatenation
example
Dim x
X =inputbox (what is your name)
Msgbox the name you entered is &
x & .

HTML
html>
<head>
<title>
</title>
</head>

<body>
<

</body>
</html>

Script tag in body


< Script type = text/ vbscript>

.
</script>

<html>
<head>
<title>My First VBScript Code!</title>
<meta http-equiv="x-ua-compatible" content="IE=10">

</head>
<body>
<script type="text/vbscript">
document.write(Hello my name is amrit")
</script>
</body>
</html>

Option explicit
You can assign value to the variable
without declaring it, its not good
programing practise
Option Explicit statement at the
beginning of your code so that you will
be forced to declare all your variables

<html>
<body>
<script type="text/vbscript">
Option Explicit
Dim markE, markM, markT
markE=90
markM=86
markT=markE+markM
document.write("Your marks in English is " & markE & "." &
"")
document.write("Your marks in Mathematics is " & markM & "."
& "")
document.write("Your total marks is " & markT & ".")
</script>

</body>
</html>

Dim statement for declaring (Javascript


use var statement)
Variable name must begin with a letter.
Variables beginning with numbers or
special characters are not allowed.
Variable name should not contain a period
(.)

Vbscript syntax
No semicolon
Not case sensitive, Javscript is case
senstive
Double slashes for comments in Java //
or /* .. */

+ and &
Dim x,y,result
X = 12 both are string so it concatenate
Y = 4
Result =x+y
Msgbox result
124
----------------------------------------------------------------------X =12
Y=4
both are integers so addition
Result = x+y
Msgbox result

Dim x,y,result
X = 12
one is string and one is integer
Y = 4
Result =x+y
Msgbox result
16
---------------------------------------------------Dim x,y,result
X = 12
one is string and one is integer
Y = hello
Result =x+y
Msgbox result
type mismatch error

Calculate area of circle


Dim r, result
Const pie = 3.14
R = inputbox (enter the radius)
Result = pie * r ^2
Msgbox result
Msgbox the area of circle is & result

IF ..THEN

dim x
X =3
If x > 10 then
Msgbox (hello class)
End if

IF then else
If time > 12 then
MSGBOX (good afternoon)
Else
msgbox (good morning)
End if

Option explicit
Dim age
Age = inputbox (enter your age)
If age < 16 then
Msgbox you are baby
Else
Msgbox ya you can drive
End if

If.....Then.......ElseIf
If.....Then.......ElseIf
if you have to select one of many
blocks of code to execute.

example
option explicit

dim age
age = inputbox ("enter your age")
if age <16 then
msgbox "stay home"
elseif age > 16 and age <60 then
msgbox "ya u can drive"
elseif age > 60 and age <80 then

msgbox "eye sight exam needed to be performed"


else
msgbox "you shld be dead"
end if

Select case statement


example
Select Case today
Case "Sunday"
document.write("Today
Case "Monday"
document.write("Today
Case "Tuesday"
document.write("Today
Case "Wednesday"
document.write("Today
Case "Thursday"
document.write("Today
Case "Friday"
document.write("Today
Case "Saturday"
document.write("Today
End Select

is Sunday")
is Monday")
is Tuesday")
is Wednesday")
is Thursday")
is Friday")
is Saturday")

<script type="text/vbscript">
Dim myName
myName = "Charles"
Select Case myName
Case "Bob"
document.write("Been busy Bob?")
Case "Sara"
document.write("Seen any slick sunglasses Sara?")
Case "Charles"
document.write("Did you chuck your chowder Charles?")
End Select
</script>

Dim a
A = inputbox (what is amrit fav fruit)
Select case a
Case Mango
Msgbox Correct good job
Case Else
Msgincorrect
End select

Conditional statements- Do while


loop
<script
type="text/vbscript">
Dim x
x=1
Do While x<5
Mxgbox hello & x
x=x+1
x+=1
Loop
</script>

For-Next Loop

For i = 1 To 5 Step 1
document.write("The number is " & i &
"")
Next

Do until
Dim x
x=1
Do
Msgbox x
X = x+1
Loop until x = 5

instr
Dim x
x = you rock the world
Instr (x, rock)
Output 5

right
Syntax
Right(string,length)

Example
Dim rose
Rose = one is all love
Msgbox RIGHT (rose, 3)
Output ove

txt="This is a beautiful day!


Msgbox Left(txt,7)
Output
This is

MORE
ucase
dim z
Z = have a good day
Msgbox ucase
Output : HAVE A GOOD DAY
Len
dim z
Z = have a good day
Msgbox len (z)
Output 15

Dim x
X = 12
Msgbox typename integer also try x
=12.56,
Dim y
Y = sjss
Msgbox typename string also try x =true

Dim x
X = inputbox (enter the value)
Msgbox typename (x)
//string
Y =cint (x)
Msgbox typename (y)
//integer

Class: Scripting.
FileSystemObject
It is used to gain access to the computer file
system
To deal with Microsoft file system, it is required to
use this class
Create folder in file system
dim fobj1
set fobj1 =createobject
("Scripting.filesystemobject")
fobj1.createfolder (path)
set fobj1 = nothing

Write text file


Dim fobj1, fobj2
Set fobj1 = createobject
(scripting.FileSystemObject)
Set fobj2= fobj1.createTextFile (path)
fObj2.writeline msg
fObj2.close
Set fObj2=nothing
Set fObj1= nothing

Read char
Prerequite: text file created
dim objfso, objtextstream, mychar
set objfso= createobject ("Scripting.filesystemobject")
set objtextstream = objfso.opentextfile (path")
do while objtextstream.atendofstream = false
mychar = objtextstream.read (1)
msgbox mychar
loop
objtextstream.close
set objtextstream= nothing

readLine
Prerequite: text file created
dim objfso, objtextstream, mychar
set objfso= createobject ("Scripting.filesystemobject")
set objtextstream = objfso.opentextfile (path")
do while objtextstream.atendofstream = false
mychar = objtextstream.readline
msgbox mychar
loop
objtextstream.close
set objtextstream= nothing

readAll
Prerequite: text file created
dim objfso, objtextstream, mychar
set objfso= createobject ("Scripting.filesystemobject")
set objtextstream = objfso.opentextfile (path")
do while objtextstream.atendofstream = false
mychar = objtextstream.readline
msgbox mychar
loop
objtextstream.close
set objtextstream= nothing

Excel (create excel file)


'create variable
dim objexcel
'create excel object
set objexcel = createobject ("Excel.application")
Objexcel.visible = true
objexcel.workbooks.add

'save excel file


objexcel.activeworkbook.Saveas filepath"
'quit app
Objexcel.quit
set objexcel = nothing

dim objexcel, obj2


set objexcel = createobject ("Excel.application")
Objexcel.visible = true
objexcel.workbooks.open path
Set Obj1 = objexcel.
Activeworkbook.worksheets(sheet1)
Sheet1.cells (1,1).value =name
Sheet1.cells(1,2).value = age

objexcel.workbooks.Save
Objexcel.activeworkbook.close
Objexcel.quit
set objexcel = nothing

You might also like