Tuesday, April 27, 2010
Few Sample script for VB
To sun any application form CMD (command prompt / MS DOS) one has to use either “cscript” or “Wscript” to provide interpreter environment to script to get execute
Sample script to open & read excel sheet:
Dim objExcel
Set objExcel = WScript.CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\test1.xls")
objWorkbook.Activate
Dim intRow , intCol
intRow = 1 : intCol = 1
WScript.Echo " Coloumn : Data "
Do Until objExcel.Cells(intRow,intCol).Value = ""
Do Until objExcel.Cells(intRow, intCol) = ""
WScript.Echo objExcel.Cells(intRow, intCol).Value
intRow = intRow + 1
Loop
WScript.Echo " "
intCol = intCol + 1
intRow = 1
Loop
objExcel.Workbooks.Close ()
objExcel.Quit
Sample script to work around Drive (C:\, D:\,etc):
Dim filesys
set filesys = CreateObject("Scripting.FileSystemObject")
Set drv = filesys.GetDrive("e")
select case drv.DriveType
Case 0: drtype = "Unknown"
Case 1: drtype = "Removable"
Case 2: drtype = "Fixed"
Case 3: drtype = "Network"
Case 4: drtype = "CD-ROM"
Case 5: drtype = "RAM Disk"
End Select
WScript.Echo "The specified drive is a " & drtype & " type disk."
WScript.Echo " "
WScript.Echo "Total size is " & drv.TotalSize & ". "
WScript.Echo "Available space is " & drv.AvailableSpace & ". "
WScript.Echo "Type is " & drv.DriveType & " "
WScript.Echo "Path is " & drv.Path & " "
Dim filesyst, drive
Set filesyst = CreateObject("Scripting.FileSystemObject")
WScript.Echo "Before Condtion"
drive = filesyst.DriveExists("z")
WScript.Echo drive
If filesyst.DriveExists("z") Then
WScript.Echo("The specified drive does exist.")
Else
WScript.Echo("The specified drive does not exist.")
End If
Sample script for file handling:
'This program will create a file E:\file_location.txt, write and read the date and then it will delete'
'it'
Dim fso, file, file_location, FileName
file_location = "E:\file_location.txt"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("E:\file_location.txt", ForWriting, True)
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
Set file = fso.OpenTextFile("E:\file_location.txt", ForReading, True)
Do while file.AtEndofStream <> True
data = file.ReadLine()
msgbox data
Loop
file.Close()
fso.DeleteFile("E:\file_location.txt")
Sample script to open any application:
Set objShell = WScript.CreateObject("WScript.Shell")
'ObjShell.Exec("C:\Program Files\Internet Explorer\IEXPLORE.EXE")
Set IE = CreateObject("InternetExplorer.Application")
IE.
IE.visible = 1
IE.navigate("https://" & URL & "www.google.com")
Friends let me know if you want any specific generic script I would really happy to help.
Classes in VB script for QTP
Creating classes in VBScript is really no different than creating classes in any other programming language. The concept of an object is usually described as just about anything. When you think about it every thing around you is some sort of object. For example a house is an object. It has properties and methods. The properties could be that the house has 3 bedrooms, a garage, a pool, air conditioning etc. You can even break down the house into smaller objects, such as doors windows etc. A garage door may have methods that are controlled by a remote. The methods may be something like garageDoor.open or garageDoor.close. The garage door would be an object, and the method would be to open or close. Getting back to the Vb Script, lets see how we would write a simple class called Math that adds or subtracts two numbers. First let’s look at our parameters for the class.
Class Name: Math
Methods: add and subtract
Now before we actually create the class, lets just see how we would write the two methods:
Public Function add(afirst, asecond)
Dim output
output = afirst + asecond
add = output
End Function
Public Function subtract(sfirst, ssecond)
Dim output
output = sfirst - ssecond
subtract = output
End Function
Now we can define the class:
Class Math
End Class
Then just put them together:
Class Math
Public Function add(afirst, asecond)
Dim output
output = afirst + asecond
add = output
End Function
Public Function subtract(sfirst, ssecond)
Dim output
output = sfirst - ssecond
subtract = output
End Function
End Class
Subscribe to:
Posts (Atom)