Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Vb Script_Good One

Vb Script_Good One



|Views: 418|Likes:
Published by Amit Rathi

More info:

Published by: Amit Rathi on Dec 10, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





VBScript Data Types
VBScript has only one data type called a Variant. A Variant is a special kind of data type thatcan contain different kinds of information, depending on how it is used. Because Variant is the onlydata type in VBScript, it is also the data type returned by all functions in VBScript.At its simplest, a Variant can contain either numeric or string information. A Variant behavesas a number when you use it in a numeric context and as a string when you use it in a string context.That is, if you are working with data that looks like numbers, VBScript assumes that it is numbers anddoes what is most appropriate for numbers. Similarly, if you're working with data that can only bestring data, VBScript treats it as string data. You can always make numbers behave as strings byenclosing them in quotation marks (" ").You can also declare a variable implicitly by simply using its name in your script. That is notgenerally a good practice because you could misspell the variable name in one or more places, causingunexpected results when your script is run. For that reason, theOption Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the firststatement in your script.Looping allows you to run a group of statements repeatedly. Some loops repeat statementsuntil a condition is False; others repeat statements until a condition is True. There are also loops thatrepeat statements a specific number of times.The following looping statements are available in VBScript:
Do...Loop: Loops while or until a condition is True.
While...Wend: Loops while a condition is True.
For...Next: Uses a counter to run statements a specified number of times.
For Each...Next: Repeats a group of statements for each item in a collection or eachelement of an array.
VBScript Procedures
In VBScript, there are two kinds of procedures; theSub procedure and theFunctionprocedure. Sub ProceduresA Sub procedure is a series of VBScript statements (enclosed by Sub and End Substatements) that perform actions but don't return a value. A Sub procedure can take arguments(constants, variables, or expressions that are passed by a calling procedure). If a Sub procedure hasno arguments, its Sub statement must include an empty set of parentheses ().The following Sub procedure uses two intrinsic, or built-in, VBScript functions,MsgBoxandInputBox, to prompt a user for information. It then displays the results of a calculation based on thatinformation. The calculation is performed in a Function procedure created using VBScript. The Functionprocedure is shown after the following discussion.Sub ConvertTemp()temp = InputBox("Please enter the temperature in degrees F.", 1)MsgBox "The temperature is " & Celsius(temp) & " degrees C."End Sub
Function Procedures
A Function procedure is a series of VBScript statements enclosed by the Function and EndFunction statements. A Function procedure is similar to a Sub procedure, but can also return a value. AFunction procedure can take arguments (constants, variables, or expressions that are passed to it by acalling procedure). If a Function procedure has no arguments, its Function statement must include anempty set of parentheses. A Function returns a value by assigning a value to its name in one or morestatements of the procedure. The return type of a Function is always a Variant.In the following example, the Celsius function calculates degrees Celsius from degreesFahrenheit. When the function is called from the ConvertTemp Sub procedure, a variable containing theargument value is passed to the function. The result of the calculation is returned to the callingprocedure and displayed in a message box.Sub ConvertTemp()temp = InputBox("Please enter the temperature in degrees F.", 1)MsgBox "The temperature is " & Celsius(temp) & " degrees C."End SubFunction Celsius(fDegrees)Celsius = (fDegrees - 32) * 5 / 9End Function
Getting Data into and out of Procedures
Each piece of data is passed into your procedures using an argument . Arguments serve asplaceholders for the data you want to pass into your procedure. You can name your arguments anyvalid variable name. When you create a procedure using either the Sub statement or the Functionstatement, parentheses must be included after the name of the procedure. Any arguments are placedinside these parentheses, separated by commas. For example, in the following example, fDegrees is aplaceholder for the value being passed into the Celsius function for conversion.Function Celsius(fDegrees)Celsius = (fDegrees - 32) * 5 / 9End FunctionTo get data out of a procedure, you must use a Function. Remember, a Function procedurecan return a value; a Sub procedure can't.Using Sub and Function Procedures in CodeA Function in your code must always be used on the right side of a variable assignment or inan expression. For example:Temp = Celsius(fDegrees)-or-MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."To call a Sub procedure from another procedure, type the name of the procedure along withvalues for any required arguments, each separated by a comma. TheCall statement is not required, but if you do use it, you must enclose any arguments in parentheses.The following example shows two calls to the MyProc procedure. One uses the Call statementin the code; the other doesn't. Both do exactly the same thing.Call MyProc(firstarg, secondarg)MyProc firstarg, secondargNotice that the parentheses are omitted in the call when the Call statement isn't used.CreateObject FunctionSee Also
Creates and returns a reference to an Automation object.CreateObject(servername.typename[,location]) ArgumentsservernameRequired. The name of the application providing the object.typenameRequired. The type or class of the object to create.locationOptional. The name of the network server where the object is to be created.RemarksAutomation servers provide at least one type of object. For example, a word-processingapplication may provide an application object, a document object, and a toolbar object.To create an Automation object, assign the object returned by CreateObject to an objectvariable:Dim ExcelSheetSet ExcelSheet = CreateObject("Excel.Sheet")This code starts the application that creates the object (in this case, a Microsoft Excelspreadsheet). Once an object is created, refer to it in code using the object variable you defined. Asshown in the following example, you can access properties and methods of the new object using theobject variable, ExcelSheet, and other Excel objects, including the Application object and theActiveSheet.Cells collection:' Make Excel visible through the Application object.ExcelSheet.Application.Visible = True' Place some text in the first cell of the sheet.ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row 1"' Save the sheet.ExcelSheet.SaveAs "C:\DOCS\TEST.XLS"' Close Excel with the Quit method on the Application object.ExcelSheet.Application.Quit' Release the object variable.Set ExcelSheet = NothingCreating an object on a remote server can only be accomplished when Internet security isturned off. You can create an object on a remote networked computer by passing the name of thecomputer to the servername argument of CreateObject. That name is the same as the machine nameportion of a share name. For a network share named "\\myserver\public", the servername is"myserver". In addition, you can specify servername using DNS format or an IP address.
The following code returns the version number of an instance of Excel running on a remotenetwork computer named "myserver":Function GetVersionDim XLAppSet XLApp = CreateObject("Excel.Application", "MyServer")GetVersion = XLApp.VersionEnd FunctionAn error occurs if the specified remote server does not exist or cannot be found.
Dim Statement
Declares variables and allocates storage space.Dimvarname[([subscripts])][,varname[([subscripts])]] . . . ArgumentsvarnameName of the variable; follows standard variable naming conventions.subscriptsDimensions of an array variable; up to 60 multiple dimensions may be declared. Thesubscripts argument uses the following syntax:upperbound [,upperbound] . . .The lower bound of an array is always zero.RemarksVariables declared with Dim at the script level are available to all procedures within the script.At the procedure level, variables are available only within the procedure.You can also use the Dim statement with empty parentheses to declare a dynamic array. Afterdeclaring a dynamic array, use the ReDim statement within a procedure to define the number of dimensions and elements in the array. If you try to redeclare a dimension for an array variable whosesize was explicitly specified in a Dim statement, an error occurs.Note When you use the Dim statement in a procedure, you generally put the Dim statementat the beginning of the procedure.The following examples illustrate the use of the Dim statement:Dim Names(9) ' Declare an array with 10 elements.Dim Names() ' Declare a dynamic array.Dim MyVar, MyNum ' Declare two variables.
Erase Statement
Reinitializes the elements of fixed-size arrays and deallocates dynamic-array storage space.Erase arrayThe array argument is the name of the array variable to be erased.RemarksIt is important to know whether an array is fixed-size (ordinary) or dynamic because Erasebehaves 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:Type of arrayEffect of Erase on fixed-array elementsFixed numeric arraySets each element to zero.Fixed string arraySets each element to zero-length ("").Array of objectsSets each element to the special value Nothing.Erase frees the memory used by dynamic arrays. Before your program can refer to thedynamic array again, it must redeclare the array variable's dimensions using a ReDim statement.The following example illustrates the use of the Erase statement.Dim NumArray(9)Dim DynamicArray()ReDim DynamicArray(9) ' Allocate storage space.Erase NumArray ' Each element is reinitialized.Erase DynamicArray ' Free memory used by array.
On Error Statement
Enables or disables error-handling.On Error Resume NextOn Error GoTo 0RemarksIf you don't use an On Error Resume Next statement anywhere in your code, any run-timeerror that occurs can cause an error message to be displayed and code execution stopped. However,the host running the code determines the exact behavior. The host can sometimes opt to handle sucherrors differently. In some cases, the script debugger may be invoked at the point of the error. In stillother cases, there may be no apparent indication that any error occurred because the host does not tonotify the user. Again, this is purely a function of how the host handles any errors that occur.

Activity (0)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
automationqtp123 liked this
automationqtp123 liked this
byancaa liked this
abhi_shrotriya liked this
Irfan liked this
vishh_champ liked this
guru0416 liked this
hmanastasi liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->