Search here for all the info you want in this Blog

Showing posts with label vbscript. Show all posts
Showing posts with label vbscript. Show all posts

write a program to display hello world

' 1) write a program to display hello world

MsgBox"Hello World"

' the above program will display a message box with text hello world
' the same above statement  can be written as

WScript.Echo "Hello word"

' the above statement will also display a message box
' the above statement will display a message box in wscript environment
' and in cscript environment it will display the message in command line interface

' to understand this more see

http://qtpcomplete.blogspot.com/2011/07/how-to-execute-vbscript-program.html

How To Create and Save a Word Document in VBScript


Set objWord = CreateObject("Word.Application")
objWord.Caption = "Test Caption"
objWord.Visible = True

Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.Font.Name = "Arial"
objSelection.Font.Size = "18"
objSelection.TypeText "Network Adapter Report"
objSelection.TypeParagraph()

objSelection.Font.Size = "14"
objSelection.TypeText "" & Date()
objSelection.TypeParagraph()
objSelection.TypeParagraph()

objSelection.Font.Size = "10"

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration")

For Each objItem in colItems

    objSelection.Font.Bold = True
    objSelection.TypeText "ARP Always Source Route: "
    objSelection.Font.Bold = False
    objSelection.TypeText "" & objItem.ArpAlwaysSourceRoute
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "ARP Use EtherSNAP: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.ArpUseEtherSNAP
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Caption: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.Caption
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Database Path: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""   & objItem.DatabasePath
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Dead GW Detection Enabled: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""   & objItem.DeadGWDetectEnabled
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Default IP Gateway: "
    objSelection.Font.Bold = False
    objSelection.TypeText "" & objItem.DefaultIPGateway
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Default TOS: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.DefaultTOS
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Default TTL: "
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.DefaultTTL
    objSelection.TypeParagraph()

    objSelection.Font.Bold = True
    objSelection.TypeText "Description: "
    objSelection.Font.Bold = True
    objSelection.Font.Bold = False
    objSelection.TypeText ""  & objItem.Description
    objSelection.TypeParagraph()

    objSelection.TypeParagraph()

Next

objDoc.SaveAs("C:\Scripts\Word\testdoc.doc")
objWord.Quit

How to Create a New Word Document


Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

How to Get the List Of Microsoft Word Document Properties


On Error Resume Next

Set objWord = CreateObject("Word.Application")
Wscript.Echo "Active Printer:", objWord.ActivePrinter

For Each objAddIn in objWord.AddIns
    Wscript.Echo "AddIn: ", objAddIn
Next

Wscript.Echo "Application:", objWord.Application
Wscript.Echo "Assistant:", objWord.Assistant

For Each objCaption in objWord.AutoCaptions
    Wscript.Echo "AutoCaptions:", objCaption
Next
Wscript.Echo "Automation Security:", objWord.AutomationSecurity
Wscript.Echo "Background Printing Status:", objWord.BackgroundPrintingStatus
Wscript.Echo "Background Saving Status:", objWord.BackgroundSavingStatus
Wscript.Echo "Browse Extra File Type:", objWord.BrowseExtraFileTypes
Wscript.Echo "Build:", objWord.Build
Wscript.Echo "Caps Lock:", objWord.CapsLock
Wscript.Echo "Caption:", objWord.Caption

For Each objLabel in objWord.CaptionLabels
    Wscript.Echo "Caption Label:", objLabel
Next

Wscript.Echo "Check Language:", objWord.CheckLanguage

For Each objAddIn in objWord.COMAddIns
    Wscript.Echo "COM AddIn:", objAddIn
Next

Wscript.Echo "Creator:", objWord.Creator

For Each objDictionary in objWord.CustomDictionaries
    Wscript.Echo "Custom Dictionary:", objDictionary
Next

Wscript.Echo "Customization Context:", objWord.CustomizationContext
Wscript.Echo "Default Legal Blackline:", objWord.DefaultLegalBlackline
Wscript.Echo "Default Save Format:", objWord.DefaultSaveFormat
Wscript.Echo "Default Table Separator:", objWord.DefaultTableSeparator

For Each objDialog in objWord.Dialogs
    Wscript.Echo "Dialog:", objDialog
Next

Wscript.Echo "Display Alerts:", objWord.DisplayAlerts
Wscript.Echo "Display Recent Files:", objWord.DisplayRecentFiles
Wscript.Echo "Display Screen Tips:", objWord.DisplayScreenTips
Wscript.Echo "Display Scroll Bars:", objWord.DisplayScrollBars

For Each objDocument in objWord.Documents
    Wscript.Echo "Document:", objDocument
Next

Wscript.Echo "Email Template:", objWord.EmailTemplate
Wscript.Echo "Enable Cancel Key:", objWord.EnableCancelKey
Wscript.Echo "Feature Install:", objWord.FeatureInstall

For Each objConverter in objWord.FileConverters
    Wscript.Echo "File Converter:", objConverter
Next

Wscript.Echo "Focus In MailHeader:", objWord.FocusInMailHeader

For Each objFont in objWord.FontNames
    Wscript.Echo "Font Name:", objFont
Next

Wscript.Echo "Height", objWord.Height

For Each objBinding in objWord.KeyBindings
    Wscript.Echo "Key Binding:", objBinding
Next

For Each objFont in objWord.LandscapeFontNames
    Wscript.Echo "Landscape Font Name:", objFont
Next

Wscript.Echo "Language", objWord.Language

For Each objLanguage in objWord.Languages
    Wscript.Echo "Language:", objLanguage
Next

Wscript.Echo "Left", objWord.Left
Wscript.Echo "Mail System:", objWord.MailSystem
Wscript.Echo "MAPI Available:", objWord.MAPIAvailable
Wscript.Echo "Math Coprocessor Available:", objWord.MathCoprocessorAvailable
Wscript.Echo "Mouse Available:", objWord.MouseAvailable
Wscript.Echo "Name:", objWord.Name
Wscript.Echo "Normal Template:", objWord.NormalTemplate
Wscript.Echo "Num Lock:", objWord.NumLock
Wscript.Echo "Parent:", objWord.Parent
Wscript.Echo "Path:", objWord.Path
Wscript.Echo "Path Separator:", objWord.PathSeparator
Wscript.Echo "Print Preview:", objWord.PrintPreview

For Each objFile in objWord.RecentFiles
    Wscript.Echo "Recent File:", objFile
Next

Wscript.Echo "Screen Updating:", objWord.ScreenUpdating
Wscript.Echo "Show Visual Basic Editor:", objWord.ShowVisualBasicEditor
Wscript.Echo "Special Mode:", objWord.SpecialMode
Wscript.Echo "Startup Path:", objWord.StartupPath

For Each objTask in objWord.Tasks
    Wscript.Echo "Task:", objTask
Next

For Each objTemplate in objWord.Templates
    Wscript.Echo "Template:", objTemplate
Next

Wscript.Echo "Top:", objWord.Top
Wscript.Echo "Usable Height:", objWord.UsableHeight
Wscript.Echo "Usable Width:", objWord.UsableWidth
Wscript.Echo "User Address:", objWord.UserAddress
Wscript.Echo "User Control:", objWord.UserControl
Wscript.Echo "User Initials:", objWord.UserInitials
Wscript.Echo "User Name:", objWord.UserName
Wscript.Echo "Version:", objWord.Version
Wscript.Echo "Visible:", objWord.Visible
Wscript.Echo "Width:", objWord.Width

For Each objWindow in objWord.Windows
    Wscript.Echo "Window:", objWindow
Next

Wscript.Echo "Window State:", objWord.WindowState
objWord.Quit

How to Open an Excel Spreadsheet in Vbscript


Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\New_users.xls")

How to Read an Excel Spreadsheet in VBScript


Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open _
    ("C:\Scripts\New_users.xls")

intRow = 2

Do Until objExcel.Cells(intRow,1).Value = ""
    Wscript.Echo "CN: " & objExcel.Cells(intRow, 1).Value
    Wscript.Echo "sAMAccountName: " & objExcel.Cells(intRow, 2).Value
    Wscript.Echo "GivenName: " & objExcel.Cells(intRow, 3).Value
    Wscript.Echo "LastName: " & objExcel.Cells(intRow, 4).Value
    intRow = intRow + 1
Loop

objExcel.Quit

How To Format a Range of Cells in Excel sheet


Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True
objExcel.Workbooks.Add

objExcel.Cells(1, 1).Value = "Name"
objExcel.Cells(1, 1).Font.Bold = TRUE
objExcel.Cells(1, 1).Interior.ColorIndex = 30
objExcel.Cells(1, 1).Font.ColorIndex = 2
objExcel.Cells(2, 1).Value = "Test value 1"
objExcel.Cells(3, 1).Value = "Test value 2"
objExcel.Cells(4, 1).Value = "Tets value 3"
objExcel.Cells(5, 1).Value = "Test value 4"

Set objRange = objExcel.Range("A1","A5")
objRange.Font.Size = 14

Set objRange = objExcel.Range("A2","A5")
objRange.Interior.ColorIndex = 36

Set objRange = objExcel.ActiveCell.EntireColumn
objRange.AutoFit()

How To Add Data to a Spreadsheet Cell


Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "Test value"

Pass byval and byref

While calling a subroutine or function, each argument can be passed by reference or by value. When an argument is passed by reference, the called procedure can change the value of the variable. The change persists after the procedure is called. When an argument is passed by value, any changes that the called procedure makes to the value of the variable do not persist after the procedure is called.
In a Sub or Function declaration, each parameter can be specified as ByRef or ByVal. If neither is specified, the default is ByRef.
If ByVal is specified, the corresponding argument is always passed by value when the subroutine is called.
If ByRef (or neither) is specified, the argument can be passed by reference or by value when the subroutine is called. The argument is passed by value if it is enclosed in parentheses, and if the parentheses do not apply to the parameter list. The argument is also passed by value if the variable sent as an argument is in a class. Otherwise, it is passed by reference.
The following table summarizes this.
Keyword specified
Argument is passed by
ByVal
Value.
ByRef (default)
Reference or value, depending on other factors.


In the following example, the ByRef keyword is specified for the MyParam variable. When the subroutine is called, a reference is passed to the variable MyArg. When the subroutine changes the value of MyParam, this also changes the value of MyArg. The local variable MyParam becomes an alias for MyArg.
Sub TestSub(ByRef MyParam)
MyParam = 5
End Sub
Dim MyArg
MyArg = 123
TestSub MyArg
' MyArg in changed in TestSub to 5.
In the following example, the ByVal keyword is used. Therefore, the argument is passed by value. The subroutine changes the value of MyParam. However, the value of MyArg remains unchanged.
Sub TestSub(ByVal MyParam)
MyParam = 5
End Sub
Dim MyArg
MyArg = 123
TestSub MyArg
' MyArg is still 123.

VBScript Procedures

There are two types of procedure statements; the Sub procedure and the Function procedure. Both of
these procedure statements are intended to encapsulate a set of statements that provide functionality
that can be repeatedly called, but the difference between the two is how arguments are passed and
results returned.
The Sub procedure is a series of VBScript statements (enclosed by Sub and End Sub statements) that
perform actions but don't return a value as part of the Sub name. A Sub procedure can take arguments
(constants, variables, or expressions that are passed by a calling procedure). A resultant value or set of
values can be returned through the arguments. If a Sub procedure has no arguments, its Sub
statement must include an empty set of parentheses ().
The Function procedure is a series of VBScript statements enclosed by the Function and End
Function statements. A Function procedure is similar to a Sub procedure, but can also return a value
in the Function name. A Function procedure can take arguments (constants, variables, or expressions
that are passed to it by a calling procedure). If a Function procedure has no arguments, its Function
statement must include an empty set of parentheses. A Function returns a value by assigning a value
to its name in one or more statements of the procedure. The return type of a Function is always a
Variant.

VBSCRIPT FINCTIONS

VBSCRIPT BUILT-IN FINCTIONS
Date/Time Functions
Function
Description
Converts a valid date and time expression to the variant of subtype Date
Returns the current system date
Returns a date to which a specified time interval has been added
Returns the number of intervals between two dates
Returns the specified part of a given date
Returns the date for a specified year, month, and day
Returns a date
Returns a number that represents the day of the month (between 1 and 31, inclusive)
Returns an expression formatted as a date or time
Returns a number that represents the hour of the day (between 0 and 23, inclusive)
Returns a Boolean value that indicates if the evaluated expression can be converted to a date
Returns a number that represents the minute of the hour (between 0 and 59, inclusive)
Returns a number that represents the month of the year (between 1 and 12, inclusive)
Returns the name of a specified month
Returns the current system date and time
Returns a number that represents the second of the minute (between 0 and 59, inclusive)
Returns the current system time
Returns the number of seconds since 12:00 AM
Returns the time for a specific hour, minute, and second
Returns a time
Returns a number that represents the day of the week (between 1 and 7, inclusive)
Returns the weekday name of a specified day of the week
Returns a number that represents the year

Conversion Functions

Function
Description
Converts the first letter in a string to ANSI code
Converts an expression to a variant of subtype Boolean
Converts an expression to a variant of subtype Byte
Converts an expression to a variant of subtype Currency
Converts a valid date and time expression to the variant of subtype Date
Converts an expression to a variant of subtype Double
Converts the specified ANSI code to a character
Converts an expression to a variant of subtype Integer
Converts an expression to a variant of subtype Long
Converts an expression to a variant of subtype Single
Converts an expression to a variant of subtype String
Returns the hexadecimal value of a specified number
Returns the octal value of a specified number

Format Functions

Function
Description
Returns an expression formatted as a currency value
Returns an expression formatted as a date or time
Returns an expression formatted as a number
Returns an expression formatted as a percentage

Math Functions

Function
Description
Returns the absolute value of a specified number
Returns the arctangent of a specified number
Returns the cosine of a specified number (angle)
Returns e raised to a power
Returns the hexadecimal value of a specified number
Returns the integer part of a specified number
Returns the integer part of a specified number
Returns the natural logarithm of a specified number
Returns the octal value of a specified number
Returns a random number less than 1 but greater or equal to 0
Returns an integer that indicates the sign of a specified number
Returns the sine of a specified number (angle)
Returns the square root of a specified number
Returns the tangent of a specified number (angle)

Array Functions

Function
Description
Returns a variant containing an array
Returns a zero-based array that contains a subset of a string array based on a filter criteria
Returns a Boolean value that indicates whether a specified variable is an array
Returns a string that consists of a number of substrings in an array
Returns the smallest subscript for the indicated dimension of an array
Returns a zero-based, one-dimensional array that contains a specified number of substrings
Returns the largest subscript for the indicated dimension of an array

String Functions

Function
Description
Returns the position of the first occurrence of one string within another. The search begins at the first character of the string
Returns the position of the first occurrence of one string within another. The search begins at the last character of the string
Converts a specified string to lowercase
Returns a specified number of characters from the left side of a string
Returns the number of characters in a string
Removes spaces on the left side of a string
Removes spaces on the right side of a string
Removes spaces on both the left and the right side of a string
Returns a specified number of characters from a string
Replaces a specified part of a string with another string a specified number of times
Returns a specified number of characters from the right side of a string
Returns a string that consists of a specified number of spaces
Compares two strings and returns a value that represents the result of the comparison
Returns a string that contains a repeating character of a specified length
Reverses a string
Converts a specified string to uppercase

Other Functions

Function
Description
Creates an object of a specified type
Eval
Evaluates an expression and returns the result
Returns the current locale ID
Returns a reference to an automation object from a file
Allows you to connect a VBScript procedure to a DHTML event on your pages
Displays a dialog box, where the user can write some input and/or click on a button, and returns the contents
Returns a Boolean value that indicates whether a specified variable has been initialized or not
Returns a Boolean value that indicates whether a specified expression contains no valid data (Null)
Returns a Boolean value that indicates whether a specified expression can be evaluated as a number
Returns a Boolean value that indicates whether the specified expression is an automation object
Returns a picture object. Available only on 32-bit platforms
Displays a message box, waits for the user to click a button, and returns a value that indicates which button the user clicked
Returns a number that represents an RGB color value
Rounds a number
Returns the scripting language in use
Returns the build version number of the scripting engine in use
Returns the major version number of the scripting engine in use
Returns the minor version number of the scripting engine in use
Sets the locale ID and returns the previous locale ID
Returns the subtype of a specified variable
Returns a value that indicates the subtype of a specified variable