VB Script
VB Script
VB Script
Overview:
VBScript is a subset of Visual Basic 4.0 language. Developed by Microsoft to provide more processing power to Web pages. Used to write both server side and client side scripting. Supports Internet Explorer and IIS (Internet Information Service). Is a fast, portable and lightweight scripting language. VB Script is Case Sensitive
IIS 212/96
Visual Studio 6
Page 1 of 43
Data Types:
VBScript supports only one data type called Variant. The variant data type is a special kind of data type that can contain different kinds of information. It is the default data type returned by all functions in VBScript. At its simplest, a Variant can contain either numeric or string information. A variant behaves as a number when it is used in a numeric context and as a string when used in a string context. Subtypes of data that a variant can contain: Empty, Boolean, Integer, Long, Single, Byte, Double, Date Time, string, Object, Error. Description Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables. Variant intentionally contains no valid data. Contains either True or False. Contains integer in the range 0 to 255. Contains integer in the range -32,768 to 32,767. -922,337,203,685,477.5808 to 922,337,203,685,477.5807. Contains integer in the range -2,147,483,648 to 2,147,483,647. Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values. Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values. Contains a number that represents a date between January 1, 100 to
Double
Date
Page 2 of 43
December 31, 9999. String Object Error Contains a variable-length string that can be up to approximately 2 billion characters in length. Contains an object. Contains an error number.
Variables:
Variable is a placeholder that refers to a memory location that stores program information that may change at run time. Declaration: Variables are explicitly declared in the script using: Dim Statement Public statement Private statement
Multiple variables can be declared by separating each variable name with a comma. Examples Dim intPassngrCnt Count 'Declaring variable for storing Passenger
Naming Convention: Must begin with an alphabetic character Cannot contain an embedded period Must not exceed 255 characters Must be unique in the scope in which it is declared
Option Explicit Statement: Declaring Variables is Optional in VB Script Declaring Variables is Optional in VBScript.
Page 3 of 43
But, that is not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. Forces explicit declaration of all variables in a script. If used, the Option Explicit statement must appear in a script before any other statements. When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error occurs.
VB Script Keywords:
Empty Null True False The False keyword has a value equal to 0. The True keyword has a value equal to -1. The Null keyword is used to indicate that a variable contains no valid data. The Empty keyword is used to indicate an uninitialized variable value.
Nothing The Nothing keyword in VBScript is used to disassociate an object variable from any actual object.
Page 4 of 43
If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. The lifetime of a variable depends on how long it exists. The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. At procedure level, a variable exists only as long as you are in the procedure. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.
Syntax: Dim Var1, Var2, etc Example: Dim strEmpName, intEmpId Note: When you use the Dim statement in a procedure, you generally put the Dim statement at the beginning of the procedure.
Private Statement:
Description: Declares private variables and allocates storage space.
Page 5 of 43
Private statement variables are available only to the script in which they are declared.
Public Statement:
Description: Declares public variables and allocates storage space. Public statement variables are available to all procedures in all scripts.
Rem Statement:
Description: Uses for adding comments to your QTP Script. We can use an apostrophe (') instead of the Rem keyword. If the Rem keyword follows other statements on a line, it must be separated from the statements by a colon. However, when you use an apostrophe, the colon is not required after other statements.
Syntax: Rem Comment Comment Example: MyStr1 = "Hello" : Rem Comment after a statement separated by a colon.
Page 6 of 43
MyStr2 = "Goodbye" ' This is also a comment; no colon is needed. Rem Comment on a line with no code; no colon is needed.
Scalar Variables:
Many of the time, you only want to assign a single value to a variable you have declared. A variable containing a single value is a scalar variable.
Array Variables:
Some times you may require assigning more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. An Array is a contiguous area in the memory referred to by a common name. Usage:
Page 7 of 43
An array is made up of two parts, the array name and the array subscript. The subscript indicates the highest index value for the elements within the array. Each element of an array has a unique identifying index number by which it can be referenced. VBScript creates zero based arrays where the first element of the array has an index value of zero.
Single-Dimensional Arrays:
For assigning multiple related values to single variable, Single-Dimensional Arrays will be useful. In the following example, a single-dimension array containing 11 elements is declared: Dim A(10) Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array. You assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 10, data can be assigned to the elements of an array as follows: Arr(0) = 256 Arr(1) = 324 Arr(2) = 100 ... Arr(10) = 55 Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example:
Page 8 of 43
intVar = Arr(8)
Multi-Dimensional Arrays:
Arrays aren't limited to a single dimension. You can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions. You can declare multiple dimensions by separating an array's size numbers in the parentheses with commas. In the following example, the MyTeble variable is a two-dimensional array consisting of 6 rows and 11 columns: Dim MyTable(5, 10) In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns. You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or using the Redim statement.
VBScript provides flexibility for declaring arrays as static or dynamic. Static Arrays A static array has a specific number of elements. The size of a static array cannot be altered at run time.
Dynamic Arrays A dynamic array can be resized at any time. Dynamic arrays are useful when size of the array cannot be determined. The array size can be changed at run time.
Page 9 of 43
For example: Dim MyArray() To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place. There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements. for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example: Dim MyArray() To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place. There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements. To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place. There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements.
Page 10 of 43
If you use the Preserve keyword, you can resize only the last array dimension, and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array
It is important to know whether an array is fixed-size (ordinary) or dynamic because Erase behaves differently depending on the type of array. Erase recovers no memory for fixed-size arrays. Erase sets the elements of a fixed array as follows: Fixed numeric array - Sets each element to zero. Fixed string array - Sets each element to zero-length (""). Array of objects - Sets each element to the special value Nothing.
Erase frees the memory used by dynamic arrays. Before your program can refer to the dynamic array again, it must redeclare the array variable's dimensions using a ReDim statement.
Array Function:
Returns a Variant containing an array. The required arglist argument is a comma-delimited list of values that are assigned to the elements of an array contained with the Variant. If no arguments are specified, an array of zero length is created.
Example:
Dim A A = Array(10,20,30)
Page 11 of 43
LBound Function:
LBound(arrayname[, dimension]) Returns the smallest available subscript for the indicated dimension of an array. The dimension argument means a whole number indicating which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed The lower bound for any dimension is always 0.
UBound Function:
UBound(arrayname[, dimension]) Returns the largest available subscript for the indicated dimension of an array. The dimension argument means a whole number indicating which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed The lower bound for any dimension is always 0.
Example:
Dim A(100,3,4) UBound (A, 1) = 100 UBound (A, 2) = 3 UBound (A, 3) = 4
Constants:
A constant is a meaningful name that takes the place of a number or a string, and never changes. VBScript itself has a number of defined intrinsic constants like vbOK, vbCancel, vbTrue, vbFalse and so on. You create user-defined constants in VBScript using the Const statement.
Page 12 of 43
Example: intEmpId = 12345678 Note: Keywords Public and Private are optional. Constants are public by default. Within procedures, constants are always private; their visibility can't be changed.
VB Script Operators:
VB Script has a Full Range of Operators for performing different types of operations. Four types of operators available in VB Script. Arithmetic Operators Comparison Operators Concatenation Operators Logical Operators
When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. You can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before others. Operations within parentheses performed before those outside. are always
When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. Arithmetic and logical operators are evaluated in the following order of precedence.
Arithmetic Operators:
Page 13 of 43
Exponentation operator Symbol : ^ Description: Raises a number to the power of an exponent. Syntax: result = number^exponent Note: If either number or exponent is a Null expression, result is also Null.
Addition operator Symbol : + Description: Sums two numbers. Syntax: result = number1+number2 Note: Although you can also use the + operator to concatenate two character strings, you should use the & operator for concatenation to eliminate ambiguity and provide self-documenting code.
Substraction operator Symbol : Description: Finds the difference between two numbers or indicates the negative value of a numeric expression. Syntax: result = number1-number2 : -number Note: If one or both expressions are Null expressions, result is Null. If an expression is Empty, it is treated as if it were 0.
Multiplication operator Symbol : * Description: Multiplies two numbers. Syntax: result = number1*number2 If one or both expressions are Null expressions, result is Null. If an expression is Empty, it is treated as if it were 0.
Description: Divides two numbers and returns a floating-point result. Syntax: result = number1/number2 Note: If one or both expressions are Null expressions, result is Null. If an expression is Empty, it is treated as if it were 0.
Integer Division operator Symbol : \ Description: Divides two numbers and returns an integer result. Syntax: result = number1\number2 Note: Before division is performed, numeric expressions are rounded to Byte, Integer, or Long subtype expressions. If any expression is Null, result is also Null. Any expression that is Empty is treated as 0.
Modulus arithmetic operator Symbol : Mod Description: Divides two numbers and returns only the remainder. Syntax: result = number1 Mod number2 Notes: The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. If any expression is Null, result is also Null. Any expression that is Empty is treated as 0.
Concatenation operator Symbol : & Description: Forces string concatenation of two expressions. Syntax: result = expression1 & expression2 Notes: Whenever an expression is not a string, it is converted to a String subtype. If both expressions are Null, result is also Null. However, if only one expression is Null, that expression is treated as a zero-length string ("") when concatenated with the other expression. Any expression that is Empty is also treated as a zero-length string.
Page 15 of 43
Comparison Operators:
Equality and Inequality operators Symbol : =, <, <=, >, >=, <> Description: Used to compare expressions. Syntax: result = expression1 comparisonoperator expression2 Note: When comparing two expressions, you may not be able to easily determine whether the expressions are being compared as numbers or as strings.
In operator Symbol : In Description: Compares two object reference variables. Syntax: result = object1 Is object2 Note: If object1 and object2 both refer to the same object, result is True; if they do not, result is False.
Logical Operators:
Logical Conjuction (And) operator Symbol : Not Description: Performs logical negation on an expression. Syntax: result = Not expression If Expression is The result is True False Null False True Null
Page 16 of 43
Description: Performs a logical conjunction on two expressions. Syntax: result = expression1 And expression2
If expression1 is True True True False False False Null Null Null
And expression2 is True False Null True False Null True False Null
The result is True False Null False False False Null False Null
Logical Disjunction (Or) operator Symbol : Or Description: Performs a logical disjunction on two expressions. Syntax: result = expression1 Or expression2
If expression1And expression2The is is is True True True False False False True False Null True False Null True True True True False Null
result
Page 17 of 43
Logical Exclusion (Xor) operator Symbol : Xor Description: Performs a logical exclusion on two expressions. Syntax: result = expression1 Xor expression2 If expression1 And expression2 The result is is is True True False False True False True False False True True False
Conditional Statements:
You can control the flow of your script with conditional statements and looping statements. Using conditional statements, you can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:
1. If Then Statement 2. If Else Then Statement 3. If Else If Else Then Statement or Nested If 4. Select Case Statement
If Then Statement:
Page 18 of 43
Description: The If ... Then statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run. Usually the condition is an expression that uses a comparison operator to compare one value or variable with another.
Syntax: If Expression Then Statement1 Statement2 .. .. StatementN End If Example: If strName = Testing Then MsgBox(Testing) End If
Page 19 of 43
.. .. StatementN Else Statement1 Statement2 .. .. StatementN End If Example: If Num1 > Num2 Then MsgBox(Num1 is Big) Else MsgBox(Num2 is Big) End If
You can add as many ElseIf clauses as you need to provide alternative choices. Extensive use of the ElseIf clauses often becomes cumbersome.
Page 20 of 43
between
several
Syntax: If Expression Then Statement1 Statement2 .. .. StatementN Else If Expression Then Statement1 Statement2 .. .. StatementN Else Statement1 Statement2 .. .. StatementN End If Example: If value = 0 Then Message = zero ElseIf value = 1 Then
Page 21 of 43
Syntax: Select Case Expression Case Expression1 Statement1 Statement2 .. .. StatementN Case Expression2 Statement1
Page 22 of 43
Statement2 .. .. StatementN Case Expression3 Statement1 Statement2 .. .. StatementN Case Expression4 Statement1 Statement2 .. .. StatementN Case Else Statement1 Statement2 .. .. StatementN End Select Example: Select Case strColor Case "Red"
Page 23 of 43
MsgBox (Red) Case "Blue" MsgBox (Blue) Case "Blue" MsgBox (Orange) Case "Blue" MsgBox (Black) Case "Blue" MsgBox (Green) Case Else MsgBox (Check the Option you Provided or Spelling) End Select
Iterative Statements:
Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; Others repeat statements until a condition are True. There are also loops that repeat statements a specific number of times. Following are the Iterative Statements available in VB Script. For Next For Each Next Do While/Until Loop Do Loop While/Until Do Loop While Wend
For Next:
Page 24 of 43
Description: Repeats a group of statements a specified number of times. You can use For ... Next statements to run a block of statements a specific number of times. For loops, use a counter variable whose value increases or decreases with each repetition of the loop
Syntax: For i = 1 To N Step Inc Statement1 Statement2 .. .. StatementN Next I is the Variable for maintaining index for the loop. N is the number of times loop should iterate. Means loop statements should get iterates N number of times. Using the Step keyword, you can increase or decrease the counter variable Inc by the value you specify.
Examples: For For i = 1 To 50 MsgBox (i) Next For For i = 1 To 50 Step 2 MsgBox (i) Next For For i = 2 To 50 Step 2 Displays Even Numbers In Between 1 to 50 Displays 1 to 50 Numbars
Page 25 of 43
Syntax: For Each I in Var Statement1 Statement2 .. .. StatementN Next Here Var is a variable of type collection of Objects/Elements/Values or Array
Examples: For Each i In [1, 2, 3] MsgBox (i) Next For Each i In arr(10) MsgBox (i) Next Displays Array Elements in Arr Displays 1, 2, 3
Page 26 of 43
Do While Loop:
Description: Repeats a block of statements while a Expression condition is True or until a condition becomes True.
Syntax: Do [While | Until Expression] Statement1 Statement2 .. .. StatementN [Exit Do] Loop
Examples: Do While iCount < 20 iCount = iCount + 1 MsgBox(iCount) Loop Do Until iCount > 20 iCount = iCount + 1 MsgBox(iCount) Loop Repeats until True Increment Counter. Displays Numbers from 1 to 20 Repeats until False Increment Counter. Displays Numbers from 1 to 19
Do Loop While:
Description: Repeats a block of statements while Expression condition is True or until a condition becomes True.
Page 27 of 43
Syntax: Do Statement1 Statement2 .. .. StatementN [Exit Do] Loop [While | Until Expression]
Examples: Do iCount = iCount + 1 MsgBox(iCount) Loop While iCount < 20 Do iCount = iCount + 1 MsgBox(iCount) Loop Until iCount > 20 Increment Counter. Displays Numbers from 1 to 20 Repeats until True Increment Counter. Displays Numbers from 1 to 19 Repeats until False
While Wend:
Description: Executes a series of statements as long as a given condition is True. The While...Wend statement is provided in VBScript for those who are familiar with its usage. However, because of the lack of flexibility in While...Wend, it is recommended that you use Do...Loop instead. The While...Wend statement is provided in VBScript for those who are familiar with its usage.
Page 28 of 43
[Exit Do] WEnd Examples: While iCount < 20 iCount = iCount + 1 MsgBox(iCount) Loop Repeats until False Increment Counter. Displays Numbers from 1 to 19
Exit Statement:
Exits a block of Do...Loop, For...Next, Function, or Sub Statements. Exit Do Provides a way to exit a Do...Loop statement. It can be used only inside a Do...Loop statement.
Exit For Provides a way to exit a For loop. It can be used only in a For...Next or For Each...Next loop.
Page 29 of 43
InputBox Function: Description: The InputBox function displays a dialog box containing a label, which prompts the user about the data you expect them to input, a text box for entering the data, an OK button, a Cancel button, and optionally, a Help button. When the user clicks OK, the function returns the contents of the text box.
Arguments: Parameter prompt title default xpos Description The message in the dialog box. The title-bar of the dialog box. String to be displayed in the text box on loading. The distance from the left side of the screen to the left side of the dialog.
Page 30 of 43
The distance from the top of the screen to the top of the dialog box. The Help file to use if the user clicks the Help button on the dialog box. The context number to use within the Help file specified in helpfile.
Notes: InputBox returns a string containing the contents of the text box from the Input-box dialog. If the user clicks Cancel, a zero-length string (" ") is returned. Prompt can contain approximately 1,000 characters, nonprinting characters like the intrinsic vbCrLf constant. including
If you don't use the default parameter to specify a default entry for the text box, the text box is shown empty; a zero-length string is returned if the user doesn't enter anything in the text box prior to clicking OK. xpos and ypos are specified in twips. A twip is a device-independent unit of measurement that equals 1/20 of a point or 1/1440 of an inch. If the xpos parameter is omitted, the dialog box is centered horizontally. If the ypos parameter is omitted, the top of the dialog box is positioned approximately one-third of the way down the screen. If the helpfile parameter is provided, the context parameter must also be provided, and vice versa. In VBScript, when both helpfile and context are passed to the InputBox function, a Help button is automatically placed on the InputBox dialog, allowing the user to click and obtain context-sensitive help.
Page 31 of 43
MsgBox Function: Description: Displays a dialog box containing a message, buttons, and optional icon to the user. The action taken by the user is returned by the function as an integer value
Arguments: Parameter prompt Buttons Title Helpfile context Description The text of the message to display in the message box dialog. The sum of the Button, Icon, Default Button, and Modality constant Values The title displayed in the Title-bar of the message box dialog. An expression specifying the name of the help file to provide help functionality for the dialog. An expression specifying a context ID within helpfile.
Page 32 of 43
Create FSO Objects FSO Objects can be created using the CreateObject Method.
Dim myfso Set myfso = CreateObject (Scripting.FileSystemObject) Creating a FSO and then assigning it to myfso For Creating Folder: Public Function FolderCreate() Set objFile = CreateObject("Scripting.FileSystemObject") Set objFolderCreate = objFile.CreateFolder("C:\QuickTest") End Function
Creating Text Files using FSO: CreateTextFile method of FSO helps to create text files.
Page 33 of 43
Overwrite Boolean value indicating whether to overwrite the file if it already exists. If the file is to be overwritten, the value set is true, else false. Blank denotes the files are not overwritten. Unicode Boolean value indicating the nature of the text file ie., whether the file to be created as a Unicode or an ASCII file. The value set is True for Unicode, else False. Blank denotes ASCII files.
Crating Text File: Public Function CreateFile() Dim objFile, strFilePah strFilePath = "C:\VBSPractive.txt" Set objFile = CreateObject("Scripting.FileSystemObject") If objFile.FileExists(strFilePath) Then MsgBox strFilePath & " Exists" Else Set MyFile = objFile.CreateTextFile (strFilePath) MyFile.Close End If End Function
Parameters: Filename Name of the file to be opened. Iomode - Optional. The Mode in which the file has to be opened. The value can toggle between these three modes: ForReading, ForWriting, ForAppending.
Page 34 of 43
Create Optional. Boolean value that indicates whether a new file can be created if the specified file doesnt exist. The value is true if new file has to be created, else false. If blank, then new file isnt created. Format Optional. Values toggle between 3 states. Whether the file has to be opened as Unicode opened as ASCII or use the system default.
NOTE: ForReading the file is opened in Read Only Mode. ForWriting the file is opened in ReadWrite Mode For Appending the file is opened in ReadAppend Mode
Writing Text File: Public Function WriteFile() Dim objFile, strFilePah Const ForWriting = 2 strFilePath = "C:\VBSPractive.txt" Set objFile = CreateObject("Scripting.FileSystemObject") If objFile.FileExists(strFilePath) Then MsgBox strFilePath & " Exists" Else Set MyFile = objFile.CreateTextFile (strFilePath) End If MyFile.Close Set MyFile = objFile.OpenTextFile(strFilePath, ForWriting, True) MyFile.Write("WELCOME TO VB SCRIPT PRACTICE") MyFile.WriteLine("WELCOME TO VB SCRIPT PRACTICE") MyFile.Write("WELCOME TO VB SCRIPT PRACTICE") MyFile.WriteBlankLines 1 MyFile.Close
Page 35 of 43
End Function Reading Text File: Public Function ReadFile() Dim objFile, strFilePah Const ForWriting = 2 strFilePath = "C:\VBSPractive.txt" Set objFile = CreateObject("Scripting.FileSystemObject") If objFile.FileExists(strFilePath) Then MsgBox strFilePath & " Exists" Else Set MyFile = objFile.CreateTextFile (strFilePath) End If MyFile.Close Set MyFile = objFile.OpenTextFile(strFilePath, ForWriting, True) MyFile.Write("WELCOME TO VB SCRIPT PRACTICE") MyFile.WriteLine("WELCOME TO VB SCRIPT PRACTICE") MyFile.Write("WELCOME TO VB SCRIPT PRACTICE") MyFile.WriteBlankLines 1 MyFile.Close Set MyFile = objFile.OpenTextFile(strFilePath, 1, True) LineData = MyFile.ReadLine() MsgBox LineData MyFile.Close objFile.DeleteFile(strFilePath) End Function NOTE:
Page 36 of 43
The ReadLine () Method is used to read the contents of a text file. Alternatively, Read () and ReadAll () methods can be used to achieve the same objective. ReadAll () reads all the characters till the newline character is encountered. I.e., it reads an entire line. The Write () and WriteLine () Methods are used to write text into a file. The difference between the Write () and WriteLine () Method is that the latter automatically inserts a new line character while the former doesnt insert a new line character. The DeleteFile () or Delete () Method can be used to delete the file.
1 2 8
Open a file for reading only. You can't write to this file. Open a file for writing. Open a file and write to the end of the file.
QuickTest Object:
Public Function QuickTest() Set objQuickTest = CreateObject ("QuickTest.Application") objQuickTest.Visible = True objQuickTest.Launch End Function
Excel Object:
Public Function CreateExcel()
dtmDate = Date strMonth = Month(Date) strDay = Day(Date) strYear = Right(Year(Date),2) strFileName = "C:\" & strMonth & "-" & strDay & "-" & strYear & ".xls"
Page 37 of 43
Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True Set objWorkbook = objExcel.Workbooks.Add() objWorkbook.SaveAs(strFileName) objExcel.Quit End Function
InternetExplorer Object:
Public Function IE() Set objIE = CreateObject ("InternetExplorer.Application") objIE.Visible = True objIE.Navigate ("www.gmail.com") End Function
ADODB Object:
Function for Connecting the Data Base: Public Function DBConnectString() DBConnectString = "DRIVER={Microsoft ODBC for Oracle};UID=dffddfdf;PWD=sdfsd;SERVER=sdsdfsd;" End Function Running the Query: Function RunQuery(SQL) Set cn = CreateObject("ADODB.Connection") cn.Open(DBConnectString) Set rs = cn.Execute(SQL) Set rs = cn.Execute("commit") cn.Close Set cn = Nothing
Page 38 of 43
Set rs = Nothing End Function Getting the Column Value from DataBase: Public Function GetCOLValue(SQL, COL) Set cn = CreateObject("ADODB.Connection") cn.Open(DBConnectString) Set rs = cn.Execute(SQL) GetCOLValue= rs(COL) rs.Close cn.Close Set cn = Nothing Set rs = Nothing End Function
Dictionay Object:
Public Function IE() 'Creating the Dictionary Object Set objWeek = CreateObject("Scripting.Dictionary") 'Adding the Different Values to Dictionary Object objWeek.Add "Day.1", "Monday" objWeek.Add "Day.2", "Tuesday" objWeek.Add "Day.3", "Wednesday" objWeek.Add "Day.4", "Thursday" objWeek.Add "Day.5", "Friday" objWeek.Add "Day.6", "Saturday" objWeek.Add "Day.7", "SUnday" 'Displaying the Values in the Dictionary Object
Page 39 of 43
Page 40 of 43
Function for Login: Public Function Login() Browser("title:=Gmail: Email from Google").Page("title:=Gmail: Email from Google").WebEdit("name:=Email").Set "balaraju.s" Browser("title:=Gmail: Email from Google").Page("title:=Gmail: Email from Google").WebEdit("name:=Passwd").SetSecure "4bb2f19db7f5c050ac18ca29f3cb094e38986be6f7404b0a" Browser("title:=Gmail: Email from Google").Page("title:=Gmail: Email from Google").WebButton("name:=Sign in").Click End Function Example for Description Object:
Page 41 of 43
Public Function DescLogin() Set objBrowser = Description.Create() objBrowser("micclass").Value = "Browser" objBrowser("title").Value = "Gmail: Email from Google"
Set objPage = Description.Create() objPage("micclass").Value = "Page" objPage("title").Value = "Gmail: Email from Google"
Set objEdit = Description.Create() objEdit("micclass").Value = "WebEdit" objEdit("name").Value = "Email" objEdit("html id").Value = "Email"
Set objEditPwd = Description.Create() objEditPwd("micclass").Value = "WebEdit" objEditPwd("name").Value = "Passwd" objEditPwd("html id").Value = "Passwd"
Page 42 of 43
End Function Function for Click a link: Public Function ClickLink() Set objDesc = Description.Create() objDesc("micclass").Value = "Link" objDesc("html tag").value="A" Set objLink = Browser("Gmail: Email from Google").Page("Gmail: Email from Google_2").ChildObjects(objDesc) cnt = objLink.Count For i = 0 to cnt strVar = objLink(i).getroproperty("innertext") If strVar = "About Gmail" Then objLink(i).Click Exit For End If Next End Function
Page 43 of 43