Thursday, August 15, 2013

COM or 'Component Object Model'

COM or 'Component Object Model' is a method by which an application exposes its functionalities to other applications & programming languages using a set of methods and functions.

As an example consider Microsoft Outlook Application. You can open Outlook and then send mails, manage appointments & do many other things. Now what if you want to do all these tasks through code ( i.e. without manually opening Outlook). This is the situation where you would need COM. 

Microsoft Outlook exposes all its functionalities in form of certain methods and properties. You can access these methods using any programming language like C#, VBA, VBScript etc and directly interact with MS Outlook. 

Check out the below code to send mail using MS Outlook.
'Create an object of type Outlook
Set objOutlook = CreateObject("Outlook.Application")
Set myMail = objOutlook.CreateItem(0)
 
'Set the email properties
myMail.To = "some_m...@gmail.com"
myMail.Subject = "Sending mail from MS Outlook using QTP"
myMail.Body= "Test Mail Contents"

'Send the mail
myMail.Send

Wednesday, August 14, 2013

Working With MS Excel

'Create a new excel, insert data into Cell, rename the sheet, Save and Close
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
Set NewSheet = objExcel.Sheets.Item(1)
NewSheet.Name = "New"
NewSheet.Cells(2,2) = "USER"
objExcel.ActiveWorkbook.SaveAs "D:\ENCRYPTED\LearningQTP\CreateExcel.xlsx"
objExcel.Quit
Set objExcel = Nothing
Set objWorkBook = Nothing

Set objWorkSheet = Nothing

'Excel Search String Color Text and Bold and FontSize

Dim vrSearchString

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("D:\ENCRYPTED\Learning_QTP\Data_Files\Test.xls")
Set objWorkSheet = objExcel.Worksheets("Sheet3")
objExcel.Application.Visible = True

With objWorkSheet.UsedRange 'select the used range in particular sheet
Set vrSearchString = .Find (hardeep) ' data to find
For each vrSearchString in objWorkSheet.UsedRange' Loop through the used range
If vrSearchString="hardeep" then '’ compare with the expected data
   vrSearchString.Interior.ColorIndex = 40' make the yellow color if it finds the data
   vrSearchString.Font.FontStyle = "Bold"
   vrSearchString.Font.ColorIndex = 5
   vrSearchString.Font.Size = 14
CellAddress =vrSearchString.address
msgbox CellAddress
End If


Set vrSearchString = .FindNext(vrSearchString) ' next search

Next
End With

objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
Set objWorkBook = Nothing
Set objWorkSheet = Nothing

'Open an existing excel, Active sheet2, insert data into Cells, Save and Close

Set objExcel = CreateObject("Excel.Application")
Set objWorkBook = objExcel.Workbooks.Open("D:\ENCRYPTED\LearningQTP\Test.xlsx")
objExcel.Application.Visible = True
Set objWorkSheet = objWorkBook.Worksheets("Sheet2")
objExcel.Worksheets("Sheet2").Select

objWorkSheet.Cells(1,1) = "This"
objWorkSheet.Cells(2,1) = "is"
objWorkSheet.Cells(3,1) = "for"
objWorkSheet.Cells(4,1) = "QTP"
objWorkSheet.Cells(5,1) = "TEST"

objExcel.ActiveWorkbook.Save

objExcel.Quit
Set objExcel = Nothing
Set objWorkBook = Nothing
Set objWorkSheet = Nothing



'Open an existing excel, Insert a new sheet, insert data into Cells, Save and Close

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("D:\ENCRYPTED\LearningQTP\Test.xlsx")
Set objWorkSheet = objExcel.Worksheets("Sheet3")
objExcel.Application.Visible = True

objExcel.Worksheets.Add
Set NewSheet = objExcel.Sheets.Item(1)
NewSheet.Name = "New Worksheet"
NewSheet.Cells(2,2) = "USER"

objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
Set objWorkBook = Nothing
Set objWorkSheet = Nothing




'Open an existing excel, Rename a worksheet, Save and Close

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("D:\LearningQTP\Test.xlsx")
Set objWorkSheet = objExcel.Worksheets("HS")
objExcel.Application.Visible = True

objExcel.Sheets("Sheet5").Select
objExcel.Sheets("Sheet5").Name = "ABCDE"

objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
Set objWorkBook = Nothing
Set objWorkSheet = Nothing



'Open an existing excel, Search for a word, Highlight it, Save and Close

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("D:\ENCRYPTED\LearningQTP\Test.xlsx")
Set objWorkSheet = objExcel.Worksheets("Sheet3")
objExcel.Application.Visible = True

With objWorkSheet.UsedRange 'select the used range in particular sheet
Set c = .Find ("hardeep") ' data to find
For each c in objWorkSheet.UsedRange' Loop through the used range
If c="hardeep" then '’ compare with the expected data
c.Interior.ColorIndex = 40' make the yellow color if it finds the data

CellAddress =c.address
msgbox CellAddress

End If
Set c = .FindNext(c) ' next search

next
End With

objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
Set objWorkBook = Nothing
Set objWorkSheet = Nothing