VB Script Objects:

‘**********************************************************************************************
‘VBScript Objects
‘**********************************************************************************************
*      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"
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
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 Dictionary()

'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
MsgBox(objWeek("Day.1"))
MsgBox(objWeek("Day.2"))
MsgBox(objWeek("Day.3"))
MsgBox(objWeek("Day.4"))
MsgBox(objWeek("Day.5"))
MsgBox(objWeek("Day.6"))
MsgBox(objWeek("Day.7"))

End Function
FileSystemObject (FSO) Model:

*      Description:

*      FSO Object Model comes into picture while handling with files like Create, Update, Delete etc..
*      The FSO object model gives the server-side applications the ability to create, alter, move, and delete folders, or to detect if particular folders exist, and if so, where.  
*      You can also find out information about folders, such as their names, the date they were created or last modified, and so forth.

*      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.

*      Syntax:

fsoObject.CreateTextFile (filename [, overwrite [, Unicode]]) as TextStream.

*      Parameters

*      Filename – The Filename for the file to be created  
*      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

*      Syntax for OpenTextFile () is

Object.OpenTextFile (filename[, iomode[, create[, format]]])

*      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.
*      Create – Optional. Boolean value that indicates whether a new file can be created if the specified file doesn’t exist. The value is true if new file has to be created, else false. If blank, then new file isn’t 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

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:

*      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 doesn’t insert a new line character.
*      The DeleteFile () or Delete () Method can be used to delete the file.

ForReading
1
Open a file for reading only. You can't write to this file.
ForWriting
2
Open a file for writing.
ForAppending
8
Open a file and write to the end of the file.

‘**********************************************************************************************

No comments:

Post a Comment