QTP基础代码收集《二》
来源:互联网 发布:linux 多系统开机过程 编辑:程序博客网 时间:2024/06/13 23:44
3、 使用qtp发mail
' Example 1
Function SendMail(SendTo,Subject, Body, Attachment)
Setōl=CreateObject("Outlook.Application")
Set Mail=ol.CreateItem(0)
Mail.to=SendTo
Mail.Subject=Subject
Mail.Body=Body
If (Attachment<> "") Then
Mail.Attachments.Add(Attachment)
End If
Mail.Send
ol.Quit
Set Mail = Nothing
Set ōl = Nothing
End Function
' Example 2
Function SendMail(SendFrom,SendTo, Subject, Body)
SetōbjMail=CreateObject("CDONTS.Newmail")
ObjMail.From = SendFrom
ObjMail.To = SendTo
ObjMail.Subject = Subject
ObjMail.Body = Body
ObjMail.Send
Set ōbjMail = Nothing
End Function
4、Excel操作函数集合:
Dim ExcellApp 'As Excel.Application
Dim excelSheet1 'AsExcel.worksheet
Dim excelSheet2 'AsExcel.worksheet
Set ExcelApp =CreateExcel()
'Create a workbook with twoworksheets
ret = RenameWorksheet(ExcelApp,"Book1", "Sheet1", "Example1 Sheet Name")
ret = RenameWorksheet(ExcelApp,"Book1", "Sheet2", "Example2 Sheet Name")
ret = RemoveWorksheet(ExcelApp,"Book1", "Sheet3")
'SaveAs the work book
ret = SaveWorkbook(ExcelApp,"Book1", "D:\Example1.xls")
'Fill worksheets
Set excelSheet1 =GetSheet(ExcelApp, "Example1 Sheet Name")
Set excelSheet2 =GetSheet(ExcelApp, "Example2 Sheet Name")
For column = 1 to 10
For row = 1 to 10
SetCellValue excelSheet1, row,column, row + column
SetCellValue excelSheet2, row,column, row + column
Next
Next
'Compare the twoworksheets
ret =CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, False)
If ret Then
MsgBox "The two worksheets areidentical"
End If
'Change the values in onesheet
SetCellValue excelSheet1, 1, 1,"Yellow"
SetCellValue excelSheet2, 2, 2,"Hello"
'Compare the worksheetsagain
ret =CompareSheets(excelSheet1, excelSheet2, 1, 10, 1, 10, True)
If Not ret Then
MsgBox "The two worksheets arenot identical"
End If
'save the workbook by indexidentifier
SaveWorkbook ExcelApp, 1,""
'Close the Excelapplication
CloseExcel ExcelApp
'****************************************** Function Library***********************************************************
Dim ExcelApp 'As Excel.Application
Dim excelSheet 'AsExcel.worksheet
Dim excelBook 'AsExcel.workbook
Dim fso 'Asscrīpting.FileSystemObject
' This function will return anew Excel Object with a default new Workbook
Function CreateExcel() 'AsExcel.Application
Dim excelSheet 'AsExcel.worksheet
Set ExcelApp =CreateObject("Excel.Application") 'Create a new excel Object
ExcelApp.Workbooks.Add
ExcelApp.Visible = True
Set CreateExcel =ExcelApp
End Function
'This function will close thegiven Excel Object
'excelApp - an Excelapplication object to be closed
Sub CloseExcel(ExcelApp)
Set excelSheet =ExcelApp.ActiveSheet
Set excelBook =ExcelApp.ActiveWorkbook
Set fso =CreateObject("scrīpting.FileSystemObject")
On Error Resume Next
fso.CreateFolder"C:\Temp"
fso.DeleteFile"C:\Temp\ExcelExamples.xls"
excelBook.SaveAs"C:\Temp\ExcelExamples.xls"
ExcelApp.Quit
Set ExcelApp = Nothing
Set fso = Nothing
Err = 0
On Error GoTo 0
End Sub
'The SaveWorkbook method willsave a workbook according to the workbookIdentifier
'The method will overwrite thepreviously saved file under the given path
'excelApp - a reference to theExcel Application
'workbookIdentifier - The nameor number of the requested workbook
'path - the location to whichthe workbook should be saved
'Return "OK" on success and"Bad Workbook Identifier" on failure
Function SaveWorkbook(ExcelApp,workbookIdentifier, path) 'As String
Dim workbook 'AsExcel.workbook
On Error Resume Next
Set workbook =ExcelApp.Workbooks(workbookIdentifier)
On Error GoTo 0
If Not workbook Is NothingThen
If path = "" Or path =workbook.FullName Or path = workbook.Name Then
workbook.Save
Else
Set fso =CreateObject("scrīpting.FileSystemObject")
'if the path has no fileextension then add the 'xls' extension
If InStr(path, ".") = 0Then
path = path &".xls"
End If
On Error Resume Next
' Example 1
4、Excel操作函数集合:
Dim ExcellApp 'As Excel.Application
Dim ExcelApp 'As Excel.Application