Active Topics Memberlist Calendar Search Help | |
Register Login |
One Stop Testing Forum : Testing Tools @ OneStopTesting : QuickTest Pro @ OneStopTesting |
Topic: Using Microsoft Excel Objects in QTP |
|
Author | Message |
Mithi25
Senior Member Joined: 23Jun2009 Online Status: Offline Posts: 288 |
Topic: Using Microsoft Excel Objects in QTP Posted: 23Oct2009 at 11:24pm |
The
following code includes a set of complex and simple functions to serve
as examples of the possible uses and applications of Microsoft Excel
objects. Using Microsoft Excel Objects in QTP The
following code includes a set of complex and simple functions to serve
as examples of the possible uses and applications of Microsoft Excel
objects.
Dim ExcellApp 'As Excel.Application
Dim excelSheet1 'As Excel.worksheet
Dim excelSheet2 'As Excel.worksheet
'Create a workbook with two worksheets
ret = RenameWorksheet(ExcelApp, "Book1", "Sheet1", "Example1 Sheet Name")
ret = RenameWorksheet(ExcelApp, "Book1", "Sheet2", "Example2 Sheet Name")
ret = RemoveWorksheet(ExcelApp, "Book1", "Sheet3")
'Save as the workbook under a different name
ret = SaveWorkbook(ExcelApp, "Book1", "D:\Example1.xls")
Set excelSheet1 = GetSheet(ExcelApp, "Example1 Sheet Name")
Set excelSheet2 = GetSheet(ExcelApp, "Example2 Sheet Name")
SetCellValue excelSheet1, row, column, row + column
SetCellValue excelSheet2, row, column, row + column
ret = CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, False)
MsgBox "The two worksheets are identical"
'Change the values in one sheet
SetCellValue excelSheet1, 1, 1, "Yellow"
SetCellValue excelSheet2, 2, 2, "Hello"
ret = CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, True)
MsgBox "The two worksheets are not identical"
'Save the workbook by index identifier
'Close the Microsoft Excel application
' *************************** Function Library **************************************
Dim ExcelApp 'As Excel.Application
Dim excelSheet 'As Excel.worksheet
Dim excelBook 'As Excel.workbook
Dim fso 'As Scripting.FileSystemObject
' This function returns a new Microsoft Excel object with a default new workbook
Function CreateExcel() 'As Excel.Application
Dim excelSheet 'As Excel.worksheet
Set ExcelApp = CreateObject("Excel.Application") 'Create a new Microsoft Excel object
'This function closes the given Microsoft Excel object
'excelApp - an Excel application object to be closed
Set excelSheet = ExcelApp.ActiveSheet
Set excelBook = ExcelApp.ActiveWorkbook
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\Temp\ExcelExamples.xls"
excelBook.SaveAs "C:\Temp\ExcelExamples.xls"
'The SaveWorkbook method saves a workbook according to the workbook identifier.
'The method overwrites the previously saved file in the given path.
'excelApp - a reference to the Microsoft Excel application
'workbookIdentifier - The name or number of the requested workbook
'path - The location to which the workbook should be saved
'Returns "OK" on success and "Bad Workbook Identifier" on failure
Function SaveWorkbook(ExcelApp, workbookIdentifier, path) 'As String
Dim workbook 'As Excel.workbook
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
If Not workbook Is Nothing Then
If path = "" Or path = workbook.FullName Or path = workbook.Name Then
Set fso = CreateObject("Scripting.FileSystemObject")
'If the path has no file extension then add the 'xls' extension
SaveWorkbook = "Bad Workbook Identifier"
'The SetCellValue method sets the given 'value' in the cell which is identified by
'its row, column, and parent Microsoft Excel sheet
'excelSheet - The Microsoft Excel sheet that is the parent of the requested cell
'row - the cell's row in the excelSheet
'column - the cell's column in the excelSheet
'value - the value to be set in the cell
Sub SetCellValue(excelSheet, row, column, value)
excelSheet.Cells(row, column) = value
'The GetCellValue returns the cell's value according to its row, column, and sheet
'excelSheet - The Microsoft Excel sheet in which the cell exists
'return 0 if the cell cannot be found
Function GetCellValue(excelSheet, row, column)
tempValue = excelSheet.Cells(row, column)
'The GetSheet method returns a Microsoft Excel sheet according to the sheet Identifier
'ExcelApp - The Microsoft Excel application which is the parent of the requested sheet
'sheetIdentifier - The name or the number of the requested Microsofr Excel sheet
Function GetSheet(ExcelApp, sheetIdentifier) 'As Excel.worksheet
Set GetSheet = ExcelApp.Worksheets.Item(sheetIdentifier)
'The InsertNewWorksheet method inserts a new worksheet into the active workbook or
'the workbook identified by the workbookIdentifier. The new worksheet will get a default
'name if the sheetName parameter is empty, otherwise the sheet has the sheetName
'Return - The new sheet as an object
'ExcelApp - The Microsoft Excel application object into which the new worksheet should be added
'workbookIdentifier - An optional identifier of the worksheet into which the new worksheet should be added
'sheetName - The optional name of the new worksheet.
Function InsertNewWorksheet(ExcelApp, workbookIdentifier, sheetName) 'As Excel.worksheet
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
'If the workbookIdentifier is empty, work on the active workbook
If workbookIdentifier = "" Then
Set workbook = ExcelApp.ActiveWorkbook
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
Set InsertNewWorksheet = Nothing
sheetCount = workbook.Sheets.Count
workbook.Sheets.Add , sheetCount
Set worksheet = workbook.Sheets(sheetCount + 1)
'If the sheetName is not empty, set the new sheet's name to sheetName
Set InsertNewWorksheet = worksheet
'The RenameWorksheet method renames a worksheet'
'ExcelApp - The Microsoft Excel application that is the worksheet's parent
'workbookIdentifier - The worksheet's parent workbook identifier
'worksheetIdentifier - The worksheet's identifier
'sheetName - The new name for the worksheet
Function RenameWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier, sheetName) 'As String
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
RenameWorksheet = "Bad Workbook Identifier"
Set worksheet = workbook.Sheets(worksheetIdentifier)
RenameWorksheet = "Bad Worksheet Identifier"
'The RemoveWorksheet method removes a worksheet from a workbook
'ExcelApp - The Microsoft Excel application that is the worksheet's parent
'workbookIdentifier - The worksheet's parent workbook identifier
'worksheetIdentifier - The worksheet's identifier
Function RemoveWorksheet(ExcelApp, workbookIdentifier, worksheetIdentifier) 'As String
Dim workbook 'As Excel.workbook
Dim worksheet 'As Excel.worksheet
Set workbook = ExcelApp.Workbooks(workbookIdentifier)
RemoveWorksheet = "Bad Workbook Identifier"
Set worksheet = workbook.Sheets(worksheetIdentifier)
RemoveWorksheet = "Bad Worksheet Identifier"
'The CreateNewWorkbook method creates a new workbook in the Microsoft Excel application
'ExcelApp - The Microsoft Excel application to which an new Microsoft Excel workbook will be added
Function CreateNewWorkbook(ExcelApp)
Set NewWorkbook = ExcelApp.Workbooks.Add()
Set CreateNewWorkbook = NewWorkbook
'The OpenWorkbook method opens a previously saved Microsoft Excel workbook and adds it to the Application
'excelApp - The Microsoft Excel application to which the workbook will be added.
'path - The path of the workbook that will be opened
Function OpenWorkbook(ExcelApp, path)
Set NewWorkbook = ExcelApp.Workbooks.Open(path)
Set OpenWorkbook = NewWorkbook
'The ActivateWorkbook method sets one of the workbooks in the application as the active workbook
'ExcelApp - The workbook's parent Microsft Excel application
'workbookIdentifier - The name or the number of the workbook
Sub ActivateWorkbook(ExcelApp, workbookIdentifier)
ExcelApp.Workbooks(workbookIdentifier).Activate
'The CloseWorkbook method closes an open workbook
'ExcelApp - The parent Microsoft Excel application of the workbook
'workbookIdentifier - The name or the number of the workbook
Sub CloseWorkbook(ExcelApp, workbookIdentifier)
ExcelApp.Workbooks(workbookIdentifier).Close
'The CompareSheets method compares two sheets.
'If there is a difference between the two sheets then the value in the second sheet
'will be changed to red and contain the string:
'"Compare conflict - Value was 'Value2', Expected value is 'value2'"
'sheet1, sheet2 - The Microsoft Excel sheets to be compared
'startColumn - The column to start comparing in the two sheets
'numberOfColumns - The number of columns to be compared
'startRow - The row to start comparing in the two sheets
'numberOfRows - The number of rows to be compared
Function CompareSheets(sheet1, sheet2, startColumn, numberOfColumns, startRow, numberOfRows, trimed) 'As Boolean
'If one of the sheets does not exist, do not continue the process
If sheet1 Is Nothing Or sheet2 Is Nothing Then
'Loop through the table and fill values into the two worksheets
For r = startRow to (startRow + (numberOfRows - 1))
For c = startColumn to (startColumn + (numberOfColumns - 1))
'If 'trimed' equals True then user wants to ignore blank spaces
'if the values of a cell are not equal in the two worksheets
'create an indicator that the values are not equal and set the return value
sheet2.Cells(r, c) = "Compare conflict -
Value was '" & Value2 & "', Expected value is '" & Value1
& "'."
Edited by Mithi25 - 23Oct2009 at 11:28pm |
|
Post Resume: Click here to Upload your Resume & Apply for Jobs |
|
IP Logged | |
Forum Jump |
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |
© Vyom Technosoft Pvt. Ltd. All Rights Reserved.