Vous êtes sur la page 1sur 34

VB Script Comments

Comments The comment argument is the text of any comment we want to include. Purpose of comments: o We can use comments for making the script understandable. o We can use comments for making one or more statements disable from execution. Syntax Rem comment (After the Rem keyword, a space is required before comment.) Or Apostrophe (') symbol before the comment Comment/Uncomment a block of statements o Select block of statement and use short cut key Ctrl + M (for comment) o Select comment block and use short cut key Ctrl + Shift + M (for uncomment)

VB Script Variables
Definition 1): Variable is a named memory location for storing program information Definition 2): A variable is a convenient placeholder that refers to a computer memory location where we can store program information that may change during the time our script is running. Purpose of Variable: a) Comparing values Example: Dim x,y,a

x=100 y=100 a=x=y Msgbox a 'It returns True b) Holding Program Result Example: Cost=Tickets*Price c) Passing parameters d) To store data that returned by functions Example: myDate=Now It returns current data & time e) To hold data Example: myName=Chakri Declaring Variables We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement. For example: Dim city Dim x We declare multiple variables by separating each variable name with a comma. For Example: Dim x, y, city, chakri We can also declare a variable implicitly by simply using its name in our script. That is not generally a good practice because we could misspell the variable name in one or more places, causing unexpected results when our script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in our script. Option Explicit Statement Forces explicit declaration of all variables in a script. Option Explicit ' Force explicit variable declaration.

Dim MyVar ' Declare variable. MyInt = 10 ' Undeclared variable generates error. MyVar = 10 ' Declared variable does not generate error. Naming Restrictions for Variables Variable names follow the standard rules for naming anything in VBScript. A variable name: a) Must begin with an alphabetic character. Dim abc Dim 9ab Dim ab9 'Right 'Wrong 'Right

b) Cannot contain an embedded period. Dim abc Dim ab.c Dim ab-c Dim ab c Dim ab_c 'Right 'worng 'wrong 'wrong 'Right

c) Must not exceed 255 characters. d) Must be unique in the scope in which it is declared. Scope of Variables A variable's scope is determined by where we declare it. When we declare a variable within a procedure, only code within that procedure can access or change the value of that variable. If we declare a variable outside a procedure, we make it recognizable to all the procedures in our script. This is a script-level variable, and it has scriptlevel scope. Example: Dim x,y,z

x=10 y=20 z=x+y msgbox z Function res Dim a,b,c a=30 b=40 c=a+b+y msgbox c End Function Call res Life Time of Variables 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. Assigning Values to Variables Values are assigned to variables creating an expression as follows: The variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example: A = 200 City = Hyderabad X=100: Y=200 Scalar Variables and Array Variables A variable containing a single value is a scalar variable. ' Returns 90 'Returns 30

A variable containing a series of values, 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. Example: Dim A(3) Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements. We assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 4, data can be assigned to the elements of an array as follows: A(0) A(1) A(2) A(3) = = = = 256 324 100 55

Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example: SomeVariable = A(4) Arrays aren't limited to a single dimension. We can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions. In the following example, the MyTable 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. Dynamic Arrays We can also declare an array whose size changes during the time our 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. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example: Dim MyArray() ReDim AnotherArray() To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25. 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. ReDim MyArray(25) ReDim Preserve MyArray(30)

VB Script Data Types


VBScript has only one data type called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used. Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript. Variant Subtypes Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. For example, we can have numeric information that represents a date or a time. When used with other date or time data, the result is always expressed as a date or a time. We can also have a rich variety of numeric information ranging in size from Boolean values to huge floating-point numbers. These different categories of information that can be contained in a Variant are called subtypes. Most of the time, we can just put the kind of data we want in a Variant, and the Variant behaves in a way that is most appropriate for the data it contains.

The following table shows subtypes of data that a Variant can contain.

VB Script Operators
Operators are used for performing mathematical, comparison and logical operations. VB Script has a full range of operators, including arithmetic operators, comparison operators, concatenation operators, and logical operators. Operator Precedence When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. We can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before others. Operations within parentheses are always performed before those outside. Within parentheses, however, standard operator precedence is maintained. 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. 1) Arithmetic Operators: Operator Description 1) Exponentiation Operator (^) Raises a number to the power of an exponent 2) Multiplication Operator (*) Multiplies two numbers. 3) Division Operator (/) Divides two numbers and returns a floating-point result. 4) Integer Division Operator (\) Divides two numbers and returns an integer result.

5) Mod Operator Divides two numbers and returns only the remainder. 6) Addition Operator (+) Sums two numbers. 7) Subtraction Operator (-) Finds the difference between two numbers or indicates the negative value of a numeric expression. 8) Concatenation Operator (&) Forces string concatenation of two expressions. Example: Dim a,b,c a=10 b=3 c=a^b msgbox c '1000 c=a*b msgbox c '30 c=a/b msgbox c '3.33333333 c=a\b msgbox c '3 c=a mod b msgbox c '1 c=a-b msgbox c '7 Dim a,b,c a=10 b=2 c=3 d=c*a^b 'c=a+b msgbox d '1000 Addition (+) operator

Dim a,b,c a=10 b=2 c=a+b msgbox c '12 (if both are numeric, then it adds) a="10" b=2 c=a+b msgbox c '12 (one is string another numeric, then it adds) a="10" b="2" c=a+b msgbox c '102 (if both are strings, then it concatenates) a="hydera" b="bad" c=a+b msgbox c 'hyderabad a="gagan" b=2 c=a+b msgbox c 'error Concatenation Operator Dim a,b,c a=10 b=2 c=a&b msgbox c '102 a="10" b=2 c=a&b msgbox c '102 a="10" b="2" c=a&b msgbox c '102

a="hydera" b="bad" c=a&b msgbox c '102 2) Comparison Operators Used to compare expressions. Operator Description 1) = (Equal to) Used to compare expressions. 2) <> (Not equal to) Used to compare expressions. 3) < Less than 4) > Grater than 5) <= Less than or equal to 6) >= Greater than or equal to 7) Is Object equivalence Example: Dim x,y,z x=10 y=20 z=x=y Msgbox z 'False x=10 y=20 z=x>y Msgbox z 'False x=10 y=20 z=x>=y Msgbox z 'False x=10 y=20 z=x<>y Msgbox z 'True x=10 y=20 z=x<y Msgbox z 'True

x=10 y=20 z=x<=y Msgbox z 'True 3) Concatenation Operators Operator Description 1) Addition Operator (+) Sums two numbers If Then 1) Both expressions are numeric Add. 2) Both expressions are strings Concatenate. 3) One expression is numeric and the other is a string Add. 2) Concatenation Operator (&) Forces string concatenation of two expressions. 4) Logical Operators Operator Description Syntax 1) Not Performs logical negation on an expression result= Not expression 2) And Performs a logical conjunction on two expressions. result= expression1 And expression2 3) Or Performs a logical disjunction on two expressions. result= expression1 Or expression2 4) Xor Performs a logical exclusion on two expressions. result= expression1 Xor expression2 5) Eqv Performs a logical equivalence on two expressions. result= expression1 Eqv expression2 6) Imp Performs a logical implication on two expressions. result= expression1 Imp expression2

Input and Out Put Operations


InputBox Function Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.

Example: Dim Input Input = InputBox("Enter your name") MsgBox ("You entered: " & Input) MsgBox Function Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked. Example: Dim MyVar MyVar = MsgBox ("Hello World!", 65, "MsgBox Example") MyVar contains either 1 or 2, depending on which button is clicked.

Flow Control (Conditional Statements)


We can control the flow of our script with conditional statements and looping statements. Using conditional statements, we can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript: 1) IfThenElse Statement 2) Select Case Statement Making Decisions Using If...Then...Else The If...Then...Else 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. If...Then...Else statements can be nested to as many levels as you need. 1) Running a Statement if a Condition is True (single statement) To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement. Dim myDate myDate = #2/13/98# If myDate < Now Then myDate = Now

2) Running Statements if a Condition is True (multiple statements) To run more than one line of code, we must use the multiple-line (or block) syntax. This syntax includes the End If statement. Dim x x= 20 If x>10 Then msgbox "x value is: "&x msgbox "Bye Bye" End If 3) Running Certain Statements if a Condition is True and Running Others if a Condition is False We can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False. Example: Dim x x= Inputbox (" Enter a value") If x>100 Then Msgbox "Hello Chakri" Msgbox "X is a Big Number" Msgbox "X value is: "&X Else Msgbox "GCR" Msgbox "X is a Small Number" Msgbox "X value is: "&X End If 4) Deciding Between Several Alternatives A variation on the If...Then...Else statement allows us to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so we can control program flow based on different possibilities. Example: Dim x x= Inputbox (" Enter a value")

If x>0 and x<=100 Then Msgbox "Hello Chakri" Msgbox "X is a Small Number" Msgbox "X value is "&x

Else IF x>100 and x<=500 Then Msgbox "Hello GCR" Msgbox "X is a Medium Number" Else IF x>500 and x<=1000 Then Msgbox "Hello Chandra Mohan Reddy" Msgbox "X is a Large Number" Else Msgbox "Hello Sir" Msgbox "X is a Grand Number" End If End If End If 5) Executing a certain block of statements when two / more conditions are True (Nested If...) Example: Dim State, Region State=Inputbox ("Enter a State") Region=Inputbox ("Enter a Region") If state= "AP" Then If Region= "Telangana" Then msgbox "Hello Chakri" msgbox "Dist count is 10" Else if Region= "Rayalasema" Then msgbox "Hello GCR" msgbox "Dist count is 4" Else If Region= "Costal" Then msgbox "Hello Chandra mohan Reddy" msgbox "Dist count is 9" End If End If End If End If Making Decisions with Select Case The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement

provides capability similar to the If...Then...Else statement, but it makes code more efficient and readable. Example: Option explicit Dim x,y, Operation, Result x= Inputbox (" Enter x value") y= Inputbox ("Enter y value") Operation= Inputbox ("Enter an Operation") Select Case Operation Case "add" Result= cdbl (x)+cdbl (y) Msgbox "Hello Chakri" Msgbox "Addition of x,y values is "&Result Case "sub" Result= x-y Msgbox "Hello Chakri" Msgbox "Substraction of x,y values is "&Result Case "mul" Result= x*y Msgbox "Hello Chakri" Msgbox "Multiplication of x,y values is "&Result Case "div" Result= x/y Msgbox "Hello Chakri" Msgbox "Division of x,y values is "&Result Case "mod" Result= x mod y Msgbox "Hello Chakri" Msgbox "Mod of x,y values is "&Result Case "expo" Result= x^y Msgbox "Hello Chakri" Msgbox"Exponentation of x,y values is "&Result Case Else Msgbox "Hello Chakri" msgbox "Wrong Operation"

End Select

Flow Control (Loop Statements)


o Looping allows us to run a group of statements repeatedly. o Some loops repeat statements until a condition is False; o Others repeat statements until a condition is True. o There are also loops that repeat statements a specific number of times. The following looping statements are available in VBScript: o Do...Loop: Loops while or until a condition is True. o While...Wend: Loops while a condition is True. o For...Next: Uses a counter to run statements a specified number of times. o For Each...Next: Repeats a group of statements for each item in a collection or each element of an array. 1) Using Do Loops We can use Do...Loop statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True. a) Repeating Statements While a Condition is True Repeats a block of statements while a condition is True or until a condition becomes True i) Do While condition Statements --------------------Loop Or, we can use this below syntax: Example: Dim x Do While x<5 x=x+1 Msgbox "Hello Chakri" Msgbox "Hello QTP" Loop ii) Do Statements --------------------Loop While condition

Example: Dim x x=1 Do Msgbox "Hello Chakri" Msgbox "Hello QTP" x=x+1 Loop While x<5 b) Repeating a Statement Until a Condition Becomes True iii) Do Until condition Statements --------------------Loop Or, we can use this below syntax: Example: Dim x Do Until x=5 x=x+1 Msgbox "Chakri" Msgbox "Hello QTP" Loop Or, we can use this below syntax: iv) Do Statements --------------------Loop Until condition Or, we can use this below syntax: Example: Dim x x=1 Do Msgbox Hello Chakri Msgbox "Hello QTP" x=x+1 Loop Until x=5

2 While...Wend Statement Executes a series of statements as long as a given condition is True. Syntax: While condition Statements --------------------Wend Example: Dim x x=0 While x<5 x=x+1 msgbox "Hello Chakri" msgbox "Hello QTP" Wend 3) For...Next Statement Repeats a group of statements a specified number of times. Syntax: For counter = start to end [Step step] statements Next Example: Dim x For x= 1 to 5 step 1 Msgbox "Hello Chakri" Next 4) For Each...Next Statement Repeats a group of statements for each element in an array or collection. Syntax: For Each item In array Statements Next Example: (1 Dim a,b,x (3)

a=20 b=30 x(0)= "Addition is "& a+b x(1)="Substraction is " & a-b x(2)= "Multiplication is " & a*b x(3)= "Division is " & a/b For Each element In x msgbox element Next Example: (2 MyArray = Array("one","two","three","four","five") For Each element In MyArray msgbox element Next

Built-In Functions of VB Script


o Conversions (25) o Dates/Times (19) o Formatting Strings (4) o Input/Output (3) o Math (9) o Miscellaneous (3) o Rounding (5) o Strings (30) o Variants (8)

Important Functions
1) Abs Function Returns the absolute value of a number.

Dim num num=abs(-50.33) msgbox num 2) Array Function Returns a variant containing an Array Dim A A=Array("hyderabad","chennai","mumbai") msgbox A(0) ReDim A(5) A(4)="nellore" msgbox A(4) 3) Asc Function Returns the ANSI character code corresponding to the first letter in a string. Dim num num=Asc("A") msgbox num * It returns the value 65 * 4) Chr Function Returns the character associated with the specified ANSI character code. Dim char Char=Chr(65) msgbox char * It returns A * 5) CInt Function Returns an expression that has been converted to a Variant of subtype Integer. Dim num num=123.45 myInt=CInt(num) msgbox MyInt 6) Date Function Returns the Current System Date. Dim mydate mydate=Date

msgbox mydate 7) Day Function Ex1) Dim myday myday=Day("17,December,2009") msgbox myday Ex2) Dim myday mydate=date myday=Day(Mydate) msgbox myday 8) DateDiff Function Returns the number of intervals between two dates. Dim Date1, Date2, x Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("yyyy", Date1, Date2) Msgbox x 'Differnce in Years Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("q", Date1, Date2) Msgbox x 'Differnce in Quarters Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("m", Date1, Date2) Msgbox x 'Differnce in Months Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("w", Date1, Date2) Msgbox x 'Differnce in weeks Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("d", Date1, Date2) Msgbox x 'Differnce in days Date1=#10-10-09# Date2=#10-10-11#

x=DateDiff("h", Date1, Date2) Msgbox x 'Differnce in Hours Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("n", Date1, Date2) Msgbox x 'Differnce in Minutes Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("s", Date1, Date2) Msgbox x 'Differnce in Seconds Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("y", Date1, Date2) Msgbox x 'Differnce in day of years Date1=#10-10-09# Date2=#10-10-11# x=DateDiff("a", Date1, Date2) Msgbox x 'Error Date1=#10-10-09# Date2=#10-10-11# x=DateDiff(Date1, Date2) Msgbox x 'Error 9) Hour Function Returns a whole number between 0 and 23, inclusive, representing the hour of the day. Dim mytime, Myhour mytime=Now myhour=hour (mytime) msgbox myhour 10) Join Function Returns a string created by joining a number of substrings contained in an array. Dim mystring, myarray(3) myarray(0)="Chandra " myarray(1)="Mohan "

myarray(2)="Reddy" mystring=Join(MyArray) msgbox mystring 11) Eval Function Evaluates an expression and returns the result. 12) Time Function Returns a Variant of subtype Date indicating the current system time. Dim mytime mytime=Time msgbox mytime 13) VarType Function Returns a value indicating the subtype of a variable. Dim x,y x=100 y=VarType(x) Msgbox y '2 (Integer) x="Hyderabad" y=VarType(x) Msgbox y '8 (String) x=#10-10-10# y=VarType(x) Msgbox y '7(Date format) x=100.56 y=VarType(x) Msgbox y ' 5(Double) y=VarType(a) Msgbox y '0 (Empty) Set x =CreateObject("Scripting.FileSystemObject") y=VarType(x) Msgbox y '9(Automation Object)

14) Left Function Dim Val, x,y Val="Hyderabad" x=Left(Val,3) Msgbox x 'Hyd Val=100 x=Left(Val,1) Msgbox x '1 Val="Hyderabad" x=Left(Val,0) Msgbox x 'Null Val="Hyderabad" x=Left(Val,12) Msgbox x 'Hyderabad Val=#10-10-10# x=Left(Val,3) Msgbox x '10/ Val="Hyderabad" x=Left(Val) Msgbox x 'Error (Lengnth is Manditory) 14) Right Function Dim AnyString, MyStr AnyString = "Hello World" ' Define string. MyStr = Right(AnyString, 1) ' Returns "d". MyStr = Right(AnyString, 6) ' Returns " World". MyStr = Right(AnyString, 20) ' Returns "Hello World". 15) Len Function Returns the number of characters in a string or the number of bytes required to store a variable. Ex 1): Dim Mystring mystring=Len("Chakri") msgbox mystring Ex 2):

Dim Mystring Mystring=Inputbox("Enter a Value") Mystring=Len(Mystring) Msgbox Mystring 16) Mid Function Returns a specified number of characters from a string. Dim Val, x,y Val="Hyderabad" x=Mid(Val,3,4) Msgbox x 'dera Val=100 x=Mid(Val,1) Msgbox x '100 Val="Hyderabad" x=Mid(Val,6,7) Msgbox x 'abad Val="Hyderabad" x=Mid(Val,6,1) Msgbox x 'a Val="Hyderabad" x=Mid(Val,6,0) Msgbox x 'Null Val="Hyderabad" x=Mid(Val,12) Msgbox x 'Null Val=#10-10-10# x=Mid(Val,3,3) Msgbox x '/10 Val=#2010-10-10# x=Mid(Val,5) Msgbox x '/2010 Val="Hyderabad" x=Mid(Val) Msgbox x 'Error

17) Timer Function Returns the number of seconds that have elapsed since 12:00 AM (midnight). Function myTime(N) Dim StartTime, EndTime StartTime = Timer For I = 1 To N Next EndTime = Timer myTime= EndTime - StartTime msgbox myTime End Function Call myTime(2000) 17) isNumeric Function Dim MyVar, MyCheck MyVar = 53 MyCheck = IsNumeric(MyVar) msgbox MyCheck MyVar = "459.95" MyCheck = IsNumeric(MyVar) msgbox MyCheck MyVar = "45 Help" MyCheck = IsNumeric(MyVar) msgbox MyCheck * It Returns True/False like Result * 18) Inputbox Function Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box. Dim Input Input = InputBox("Enter your name") MsgBox ("You entered: " & Input) 19) Msgbox Function Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.

Dim MyVar MyVar = MsgBox ("Hello World!", 65, "MsgBox Example") 20) CreateObject creates and returns reference of the filesytemobject to an Automation object. It can be used for performing operations on computer file system Set objFso=createobject ("Scripting.FileSystemObject") 'creates and returns reference of the Excel bject to an Automation object. It can be used for performing operations on Spreed sheet (Ms-Excel files) Set objExcel = CreateObject("Excel.Application") 'creates and returns reference of the Word Object to an Automation object. It can be used for performing operations on Ms-Word documents Set objWord = CreateObject("Word.Application") 'creates and returns reference of the Database Connection to an Automation object. It can be used for Connecting, opening and Closing databases Set objConnection = CreateObject("ADODB.Connection") 'creates and returns reference of the Database Recordset to an Automation object. It can be used for performing operations on database tables(Records) Set objRecordSet = CreateObject("ADODB.Recordset") 'creates and returns reference of the Ms-Power point object to an Automation object. It can be used for performing operations on Power point presentations Set objPPT = CreateObject("PowerPoint.Application") Set xmldoc = WScript.CreateObject("msxml2.domdocument") 21) Round Returns a number rounded to a specified number of decimal places.

Dim num num=172.499 num=Round(num) msgbox num 22) StrReverse It returns reverse value of the given sring x=strreverse ("dabaraedyh") msgbox x 23) strComp It compares two strings based on ASCII Values and Returens -1 (1st less than 2nd ), 0 (Equal) and 1 (1st greater than 2nd) Dim x, y x="cd": y="bcd" comp=strcomp(x,y) msgbox comp 24) Replace It replace a sub string with given value (another sub string) mystring=Replace("kb script", "k","v") msgbox mystring a) FileSystemObject Scripting allows us to process drives, folders, and files using the FileSystemObject (FSO) object model. Creating FileSystemObject: Set Variable=CreateObject("Scripting.FileSystemObject") Example: Dim objFso Set objFso=CreateObject("Scripting.FileSystemObject") objFso.CteateTextFile("D:\chakri.txt") b) Dictionary Creating Dictionary Object: Set Variable=CreateObject("Scripting.Dictionary")

Example: c) Excel Application Creating Excel Object: Set Variable=CreateObject("Excel.Application") Example: d) Word Application Creating Word Object: Set Variable=CreateObject("Word.Application") Example: e) Shell Creating Shell Object: Set Variable= WScript.CreateObject("Wscript.Shell") Example: f) Network Creating Network Object: Set Variable= WScript.CreateObject("WScript.Network") Example: g) PowerPoint Creating PowerPointObject: Set Variable=CreateObject("PowerPoint.Application") Example: h) ADODB Connection

The ADO Connection Object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database. Creating Database Connection Object: Set Variable=CreateObject("ADODB.Connection") Example: i) ADODB RecordSet The ADO Recordset object is used to hold a set of records from a database table. A Recordset object consist of records and columns (fields). Creating Database RecordSet Object: Set Variable=CreateObject("ADODB.RecordSet") Example: j) ADODB Command The ADO Command object is used to execute a single query against a database. The query can perform actions like creating, adding, retrieving, deleting or updating records. Creating Database Command Object: Set Variable=CreateObject("ADODB.Command") Example: k) Error Creating Error Object: l) RegExp Creating RegExp Object: Set objReg=CreateObject("vbscript.regexp")

m) Internet Explorer

n) Outlook Express a) Dictionary Object Dictionary Object that stores data key, item pairs. A Dictionary object is the equivalent of a PERL associative array/Hash Variable. Items can be any form of data, and are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.

Creating a Dictionary Object:


Set objDictionary = CreateObject("Scripting.Dictionary")

Dictionary Objects Methods:

Add Method Adds a key and item pair to a Dictionary object Exists Method Returns true if a specified key exists in the Dictionary object, false if it does not. Items Method Returns an array containing all the items in a Dictionary object. Keys Method Returns an array containing all existing keys in a Dictionary object.

Remove Method Removes a key, item pair from a Dictionary object. RemoveAll Method The RemoveAll method removes all key, item pairs from a Dictionary object. Example: Dim cities Set cities = CreateObject("Scripting.Dictionary") cities.Add "h", "Hyderabad" cities.Add "b", "Bangalore" cities.Add "c", "Chennai" Dictionary Objects Properties: Count Property Returns the number of items in a collection or Dictionary object. Read-only. CompareMode Property Sets and returns the comparison mode for comparing string keys in a Dictionary object. Key Property Sets a key in a Dictionary object. Item Property Sets or returns an item for a specified key in a Dictionary object. For collections, returns an item based on the specified key. Read/write. Examples: a to Elements 1) AddDictionary

Set objDictionary = CreateObject("Scripting.Dictionary")

objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" 2) Delete All Elements a fromDictionary Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" colKeys = objDictionary.Keys Wscript.Echo "First run: " For Each strKey in colKeys Wscript.Echo strKey Next objDictionary.RemoveAll colKeys = objDictionary.Keys Wscript.Echo VbCrLf & "Second run: " For Each strKey in colKeys Wscript.Echo strKey Next 3) Delete One Element from a Dictionary Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" colKeys = objDictionary.Keys Wscript.Echo "First run: " For Each strKey in colKeys Wscript.Echo strKey Next objDictionary.Remove("Printer 2")

colKeys = objDictionary.Keys Wscript.Echo VbCrLf & "Second run: " For Each strKey in colKeys Wscript.Echo strKey Next 4) List the Number of Items in a Dictionary Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" Wscript.Echo objDictionary.Count 5) Verify the Existence of a Dictionary Key Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" If objDictionary.Exists("Printer 4") Then Wscript.Echo "Printer 4 is in the Dictionary." Else Wscript.Echo "Printer 4 is not in the Dictionary." End If

Vous aimerez peut-être aussi