Вы находитесь на странице: 1из 11

VB SCRIPTING NOTES

STRING FUNCTIONS: Lcase, Ucase, len, trim, ltrim, rtrim, left(string,length) ,


right(string,length) , mid(string,start[length]), strreverse(string),
replace(Str,find, replace with): returns the replaces string
strcomp(str1,str2[compare]): compares two strings and returns the following
0 str1=str2
1 str1>str2
-1 str1<str2
Comparison is done is two ways i) vb binary compare-0 ii) vb textual comparision-1
Instr([start], str1,str2, [compare]): returns the position of first occurrence of string1
on string2
script to display the len, ucase and lcase of a string.
str= Q spiders
Msgbox lcase(str)
Msgbox ucase(str)
Msgbox len(str)
script to display the trim, ltrim, rtrim of a string
Str=
q spiders

Msgbox * & trim(Str)&*


Msgbox *& ltrim(str) &*
Msgbox *& rtrim(str) &*
Script to display the length of the string
Str = QTP automation
Msgbox left(str,3)
Msgbox right(str,10)
Msgbox right(str,4)
Msgbox mid(str,4,4)
Msgbox mid(str,4)
Script to display reverse of the string
Str= radhika
Msgbox strreverse(str)

Script to compare two strings


Msgbox strcomp(radhika, srikanth) 1
Msgbox strcomp("radhika", "Radhika) 1

script to display the string position


Str= this is VB Scripting class
msgbox instr(Str, VB)
msgbox instr(Str, ting)
msgbox instr(2,Str, is)
msgbox instr(4, str, ik)
msgbox instr(5, str, l)
VERIFICATION FUNCTIONS: They start wish IS keyword and returns either true or
fasle
ISnumeric(value), ISdate(value), ISboolean(value), Isarray(value), Isnull(value),
accept a value and check whether it is number or not
Var=inputbox(enter a number)
If isnumeric(var) then
Msgbox valid no
Else
Msgbox not a valid number
End if
USER DEFINED FUNCTIONS:
Any reusable code can be implemented either as a function or as sub-routine
i)
Sub procedures are also called as subroutines and functions are called as
function procedures.
ii)
A sub-procedure is a series of VBS statements enclosed by subend sub
statements
Eg: sub name(num1, num2)
------------[Exit sub]
iii)
iv)

v)

End sub
A subroutine will not return any value to the calling program
A function procedure is a series of VBS statements enclosed by function
end function statements
Eg: function name(num1, num2)
------------[Exit function]
End function
A function may or may return a value to the calling function

vi)
vii)
viii)

Function can act like a subroutine where as a subroutine cannot act like a
function
If a function or sub-procedures does not have any argument, then it must
be included with an empty set of parenthesis
Sub or function can have 0-n number of arguments and these values are
separated by comma

functions to add two numbers


Function addtwonums(num1,num2)
Sum=num1+num2
addtwonums=sum
End function
Msgbox addtwonums(2,4)
sub-procedure to add two numbers
Sub add(num1,num2)
dim vsum
vsum =num1+num2
msgbox vsum
End sub
call add(2,4)
write a function to create highest among 3 numbers
Function highestthreenums(vnum1,vnum2,vnum3)
Highest=num1
If num2>highest then highest =num2
If num3>highest then highest=num3
Highestthreenums = highest
End function
Msgbox highestthreenums(89,456,-1)
script to find odd or even by using functions
Function oddoreven (a)
if (a mod 2=0) then
oddoreven= "even"
else
oddoreven= "odd"
end if
end function
vnum= cint(inputbox("enter a number"))
msgbox oddoreven(vnum)

write a script to find whether the given year is leapyear or not


function leapyear(num)
if num mod 4= 0 then
leapyear=" the entered year is leap year"
else
leapyear=" the entered year is not a leap year"
end if
end function
vnum= inputbox(" enter year")
msgbox leapyear(vnum)
FILE SYSTEM OBJECTS:
VBS will allow us to process:
a) drives b) folders and c) files in windows OS by using FSO
FSO is a default runtime object in windows OS, Which uses object method syntax
Set vfso= createobject(scripting.filesystemobject)
This creates the instance of FSO object and allows using its methods and
properties
Set var = vfso.getderive(drive path)
Set var = vfso.getfolder(path)
Set var= vfso.getfile (parth)
This provides reference to drive/folder/file and helps us in assuming these
properties.
Set var= vfso.opentextfile(_____)
Set vfso= createobject(scripting.filesystemobject)
Vfso.createfolder(D:\rads-srik)
' Script to create a folder
option explicit
dim vfso, var
set vfso = createobject("scripting.filesystemobject")
var=vfso.folderexists("D:\rads-srik")
if var then
msgbox " the folder already exists"
else
vfso.createfolder("D:\rads-srik")
msgbox " successfully created"
end if

I level: Set Vfso= createobject(scripting.filesystem object)

Set

VFSO -

II level: set var=vfso.getdrive (path)

Create folder
Create Text field
Delete folder
Delete file
Copy folder
Copy file
Move folder var.
Move file
Folder exists
File exists
Drive exists

Drive
(Get drive)

Folder
(Get folder)

File
(Get file)

- Free space
- Total size
- Drive letter
- Path
- drive type
- Available
space

- Size
- Type
- Name
- Path
- Drive
- Date last
accessed
- Date
created
- Date last
modified
- Attributes

- Size
- Type
- Name
- Path
- Drive
- Date last
accessed
- Date
created
- Date last
modified
- Attributes

III level: Set


var=vfso.opentex
tfile
- Read
- Read line
- Read all
- Write
- Write line
- Write blank lines
- At end of stream
- At end of file

Write a script to read the content of a text file


'syntax: opentextfile(filename,[mode],[create])
'filename: it takes the file path, create a variable and store
the path in it
'mode: for reading-1, writing-2, appending-8
'create: it will return True/False value
option explicit
dim vfso, var, vpath
vpath="D:\rads-srik\test1.txt"
set vfso= createobject ("scripting.filesystemobject")
set var= vfso.opentextfile(vpath,1)
msgbox var.read(4)
msgbox var.readline
msgbox var.readall

'reads first 4 characters


' reads the entire line
' reads the whole text

write the data into a file


option explicit
dim vfso, vf, vpath
vpath= "D:\rads-srik\test.txt"
set vfso= createobject("scripting.filesystemobject")
set vf= vfso.opentextfile(vpath,2)
msgbox vf.write("radhika")
msgbox vf.writeblanklines(2)
msgbox vf.writeline(" this is my name")
vf.close
set vf= nothing
set vfso= nothing to check the output click on the file.
' Script to access the following properties of a file i)size ii)type iii)datelastmodified
option explicit
dim vfso, v, vpath
vpath = "D:\rads-srik\test1.txt"
set vfso= createobject ("scripting.filesystemobject")
set v = vfso.getfile(vpath)
msgbox v.type
msgbox v.size
msgbox v.datelastmodified
Count the number of lines in a file
option explicit
dim vfso, var, vpath, count
vpath= "D:\rads-srik\test1.txt"
set vfso= createobject ("scripting.filesystemobject")
set var=vfso.opentextfile(vpath,1)
count=0
do until (var.atendofstream)
loop
msgbox count

var.readline
count=count+1

set var = nothing


set vfso = nothing

ERROR HANDLING:
Error handling is a process of handling all the run-time errors within a script so that
the control will reach the end of the script. Sometimes we need to ensure that we
provide user friendly messages to the end-users instead of default error messages.
On the above two occasions we need to initiate the error handling within our script.
Error handling in VBS is a two step process:
i)
suppress the run-time error
ii)
capture the error and take appropriate measure
ON ERROR RESUME NEXT:
On error resume next is a statement provided at the beginning of the script to
suppress the default error message.
ERROR OBJECT:
Every object n VBS will have a unique number and name associated to it. We need to
use this error number and display the appropriate user friendly message.
Error object is a default object used to capture the error code and error name.
We have the following methods in error object
i)
error.number: returns the error code number
ii)
error.description: returns the name of the error
iii)
error.clear: returns the error number to zero.
On error resume next
On error resume next
a=10
b=0
c=a/b
vtemp= err.number
if vtemp=11 then
msgbox " denominator cannot be zero"
else
msgbox C
end if
Array- on error resume next
dim varr(2)
varr(0)=10
varr(1)=20
varr(2)=30
vtemp=err.number

if vtemp=9 then
msgbox "array index cannot be greater than VB"
else
msgbox varr(2)
end if
The above scripts using functions
on error resume next
a=10
b=0
c=a/b
vtemp=err.number
call errorhandler (vtemp)
dim varr(2)
varr(0)=10
varr(1)=20
varr(2)=30
vtemp = err.number
call errorhandler (Vtemp)
function errorhandler(vtemp)
if vtemp=11 then
msgbox "denominator cannot be zero"
elseif vtemp=9 then
msgbox "array index out of range"
else
err.clear
end if
end function
EXCEL PROGRAMMING:
Excel object model:
I level:
II Level:

Excel application
> Save as
> Quit
> Work books
- Save
- Close
- Sheets/ work sheets
o Add
o Count
o Name
o Cells (rows, column)
Value
Font
Size

Bold
Color index

I LEVEL:
Set xl=createobject(excel.application)
II LEVEL:
Set wb= xl.workbooks.open(path)

Write a script to perform read, write and append operation in XL.


write as script to add a value in sheet3 and make it bold, size=20 colr as green
option explicit
dim xl,wb,vpath
vpath="D:\rads\test.xls"
set xl=createobject("excel.application")
set wb = xl.workbooks.open(vpath)
wb.sheets("sheet3").cells(1,1).value="QTP"
wb.sheets("sheet3").cells(1,1).font.size=20
wb.sheets("sheet3").cells(1,1).font.bold=true
wb.sheets("sheet3").cells(1,1).font.colorindex=4
wb.save
wb.close
xl.quit
set wb= nothing
set xl = nothing
'write a script to print "QTP" in all colors between th range 1-56 in a new sheet
option explicit
dim xl,wb,vpath,i
vpath="D:\rads\test2.xls"
set xl=createobject("excel.application")
set wb = xl.workbooks.open(vpath)
wb.sheets.add
wb.sheets("sheet4").name="colors"
for i= 1 to 56 step 1
wb.sheets("colors").cells(i,1).value=i
wb.sheets("colors").cells(i,2).value="QTP"
wb.sheets("colors").cells(i,2).font.colorindex=i
next

wb.save
wb.close
xl.quit
set wb= nothing
set xl= nothing

write a script to read values from excel and move them in to 2-d arrays

1,1

1,2

1,3

0,0

0,1

0,2

2,1

Hi

2,3

1,0

Hi

1,2

3,1

3,2

Hi

2,0

2,1

Hi

Excel
Cells (2,2)
Cells (3,3)

Arrays
array (1,1)
array (2,2)

(rc-1,cc-1)
(1,1) (0,0)
(2,2) (1,1)
(1,2) (0,1)
Redim rc(rc-1,cc-1)
Note : excel 3 script
Passing the argument:
By ref:
1. passing argument into procedur (Sub or fun)
2. arguemtns for function can be passed in two ways
- by ref (by reference)
- bv val (by value)
by reference: the procedure is passed the address of the argument variable and the
code inside the function can change the variable
by value: the procedure is passed a copy of the avg variable and the code inside
procedure cannot change the original variable value.

10

11

Вам также может понравиться