45937553-QTP-Excel-Scripts

Embed Size (px)

Citation preview

  • 8/7/2019 45937553-QTP-Excel-Scripts

    1/6

    For more QTP Realtime Scripts, visit www.ramupalanki.com

    MS Excel Scripts

    '''Script to create a new excel file , write data

    '''save the file with read and write protected'''''pwd1 is for read protected pwd2 is for write protected

    Set 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=nothing

    '''Script to open excel file ,which is read and write protectedwrite data'''''pwd1 is for read protected pwd2 is for write protected

    Set 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=nothing

    ''Script 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 s

    www.ramupalanki.com

  • 8/7/2019 45937553-QTP-Excel-Scripts

    2/6

    For more QTP Realtime Scripts, visit www.ramupalanki.com

    if mw.ActiveDocument.Spellingerrors.count>0 thenReporter.ReportEvent 1,"Spelling","spelling error :"&send if mw.ActiveDocument.Close(False)next

    mw.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 10

    c(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 If Next========================================'''''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 If m.Sendotl.QuitSet m = NothingSet otl = Nothing

    www.ramupalanki.com

  • 8/7/2019 45937553-QTP-Excel-Scripts

    3/6

    For more QTP Realtime Scripts, visit www.ramupalanki.com

    End FunctionCall SendMail(" [email protected]","hi","This is test mail for testing","")'''''''''''''''create a new text file=================================================

    Dim fs,f Set 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,f Set fs=CreateObject("Scripting.FileSystemObject")Set f=fs.OpenTextFile("e:\file1.txt",1)While f.AtEndOfLineTruemsgbox 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 inSheet1==========================================

    www.ramupalanki.com

    mailto:[email protected]%5Cmailto:[email protected]%5C
  • 8/7/2019 45937553-QTP-Excel-Scripts

    4/6

    For more QTP Realtime Scripts, visit www.ramupalanki.com

    =======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"wb.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)Next

    print 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 c

    www.ramupalanki.com

  • 8/7/2019 45937553-QTP-Excel-Scripts

    5/6

    For more QTP Realtime Scripts, visit www.ramupalanki.com

    x=ws.cells(i,j).interior.colorindexmsgbox xNextNextwb.Close

    Set xl=nothing================================================='''''''''''''''''''''create word and write data=================================================dim mwset mw=CreateObject("Word.Application")mw.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 thesystem=================================================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

    www.ramupalanki.com

  • 8/7/2019 45937553-QTP-Excel-Scripts

    6/6

    For more QTP Realtime Scripts, visit www.ramupalanki.com

    =================================================Dim ieSet ie=CreateObject("InternetExplorer.Application")ie.Visible=True

    ie.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,3Set objTable = dc.Tables(1)x=1strComputer = "."Set wms=GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set colItems = wms.ExecQuery("Select * from Win32_Service")For Each s in colItemsIf x > 1 ThenobjTable.Rows.Add()

    End If objTable.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 + 1Next

    www.ramupalanki.com

    http://www.yahoomail.com/http://www.yahoomail.com/