Qtp realtime excel scripts

6,232 views

Published on

Published in: Technology, Education
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
6,232
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
522
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Qtp realtime excel scripts

  1. 1. For more QTP Realtime Scripts, visit www.ramupalanki.com MS Excel ScriptsScript to create a new excel file , write datasave the file with read and write protectedpwd1 is for read protected pwd2 is for write protectedSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Addxl.DisplayAlerts=FalseSet ws=wb.Worksheets("sheet1")ws.cells(1,1)=100ws.cells(1,2)=200wb.Saveas "e:data2.xls",,"pwd1","pwd2"wb.CloseSet xl=nothingScript to open excel file ,which is read and write protected write datapwd1 is for read protected pwd2 is for write protectedSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:data2.xls",0,False,5,"pwd1","pwd2")xl.DisplayAlerts=FalseSet ws=wb.Worksheets("sheet1")ws.cells(1,2)="hello"ws.cells(2,2)="new data"wb.Savewb.CloseSet xl=nothingScript to get the list of links in Google and do spell check=================================================dim dset mw=CreateObject("Word.Application")set d=Description.Created("micclass").value="Link"set a=Browser("Google").page("Google").childobjects(d)for i=0 to a.count-1mw.WordBasic.filenews=a(i).getROProperty("innertext")mw.WordBasic.insert sif mw.ActiveDocument.Spellingerrors.count>0 thenwww.ramupalanki.com
  2. 2. For more QTP Realtime Scripts, visit www.ramupalanki.comReporter.ReportEvent 1,"Spelling","spelling error :"&send ifmw.ActiveDocument.Close(False)nextmw.quitset mw=nothing=========================================Script to check ON the checkboxes in yahoo mail inbox=========================================Dim dSet d=Description.Created("micclass").value="WebCheckBox"Set c=Browser("Inbox (17) - Yahoo! Mail").Page("Inbox (17) - Yahoo!Mail").ChildObjects(d)For i=1 to 10c(i).set "ON"Next========================================script to select a mail having subject hi or HI========================================n=Browser("yahoo").Page("yahoo").WebTable("Inbox").RowCountFor i=2 to ns=Browser("yahoo").Page("yahoo").WebTable("Inbox").GetCellData(i,7)If lcase(trim(s))="hi" ThenBrowser("yahoo").Page("yahoo").WebCheckBox("index:="&i-1).set "ON"End IfNext========================================Function to send a mail========================================Function SendMail(SendTo, Subject, Body, Attachment)Set otl=CreateObject("Outlook.Application")Set m=otl.CreateItem(0)m.to=SendTom.Subject=Subjectm.Body=BodyIf (Attachment <> "") ThenMail.Attachments.Add(Attachment)End Ifm.Sendotl.QuitSet m = NothingSet otl = NothingEnd Functionwww.ramupalanki.com
  3. 3. For more QTP Realtime Scripts, visit www.ramupalanki.comCall SendMail("a@thdf.com","hi","This is test mail for testing","")create a new text file=================================================Dim fs,fSet fs=CreateObject("Scripting.FileSystemObject")Set f=fs.CreateTextFile("e:file1.txt")f.WriteLine "hello"f.WriteLine "this is sample data"f.CloseSet fs=nothing=================================================read data from a text file=================================================Dim fs,fSet fs=CreateObject("Scripting.FileSystemObject")Set f=fs.OpenTextFile("e:file1.txt",1)While f.AtEndOfLine<>Truemsgbox f.ReadLineWendf.CloseSet fs=nothing=================================================create a new excel file and write data=================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.AddSet ws=wb.Worksheets("sheet1")ws.cells(1,1)=10ws.cells(2,1)=20ws.cells(3,1)=50wb.SaveAs "e:file1.xls"wb.CloseSet xl=nothing=================================================open existing file and write data in second column in Sheet1=================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:file1.xls")Set ws=wb.Worksheets("sheet1")ws.cells(1,2)="mindq"ws.cells(2,2)="hyd"ws.cells(3,2)="ap"www.ramupalanki.com
  4. 4. For more QTP Realtime Scripts, visit www.ramupalanki.comwb.Savewb.CloseSet xl=nothing=================================================read data from excel from rows and columns=================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:file1.xls")Set ws=wb.Worksheets("sheet1")r=ws.usedrange.rows.countc=ws.usedrange.columns.countFor i=1 to rv=""For j=1 to cv=v&" "& ws.cells(i,j)Nextprint vprint "-----------------------"Nextwb.CloseSet xl=nothing=================================================get the bgcolor in a cell in excel=================================================Dim xl,wb,wsSet xl=CreateObject("Excel.Application")Set wb=xl.Workbooks.Open("e:file3.xls")Set ws=wb.Worksheets("sheet1")r=ws.usedrange.rows.countc=ws.usedrange.columns.countFor i=1 to rFor j=1 to cx=ws.cells(i,j).interior.colorindexmsgbox xNextNextwb.CloseSet xl=nothing=================================================create word and write data=================================================dim mwset mw=CreateObject("Word.Application")www.ramupalanki.com
  5. 5. For more QTP Realtime Scripts, visit www.ramupalanki.commw.Documents.Addmw.selection.typetext "hello"mw.ActiveDocument.SaveAs "e:file1.doc"mw.quitset mw=nothing=================================================script will display all the doc files in all the drives in the system=================================================Dim mwSet mw=CreateObject("Word.Application")Set fs=createobject("Scripting.FileSystemObject")Set d=fs.Drivesmw.FileSearch.FileName="*.doc"For each dr in dmsgbox drmw.FileSearch.LookIn=drmw.FileSearch.SearchSubFolders=Truemw.FileSearch.ExecuteFor each i in mw.FileSearch.FoundFilesprint iSet f=fs.GetFile(i)print f.Name&" "&f.Size&" "&f.DateCreatedprint "-------------------------------------------------------------------"NextNextmw.Quit=================================================Open Internet Explorer and navigate to yahoomail=================================================Dim ieSet ie=CreateObject("InternetExplorer.Application")ie.Visible=Trueie.Navigate "www.yahoomail.com"x=Browser("CreationTime:=0").GetROProperty("title")msgbox x=================================================Create word, Create table and write all the services names=================================================Set mw = CreateObject("Word.Application")mw.Visible = TrueSet dc = mw.Documents.Add()Set objRange = dc.Range()dc.Tables.AddobjRange,1,3www.ramupalanki.com
  6. 6. For more QTP Realtime Scripts, visit www.ramupalanki.comSet objTable = dc.Tables(1)x=1strComputer = "."Set wms=GetObject("winmgmts:" & strComputer & "rootcimv2")Set colItems = wms.ExecQuery("Select * from Win32_Service")For Each s in colItemsIf x > 1 ThenobjTable.Rows.Add()End IfobjTable.Cell(x, 1).Range.Font.Bold = TrueobjTable.Cell(x, 1).Range.Text = s.NameobjTable.Cell(x, 2).Range.text = s.DisplayNameobjTable.Cell(x, 3).Range.text = s.Statex=x+1Nextwww.ramupalanki.com

×