This is in continuation with Part 1 of the series on Automating MS word with QTP.

So, by now- you must have got some basic idea on automating MS word. Let us now go deeper into it and see how we can create a table in a word document. A table helps you in aligning text in a word document. Inserting a table also helps in a case when you want to display some kind of information in tabular format. Use “table” method of your doucment to add a table into your doucment and then using cell, you can enter data into the table in specific rows and columns. Following example demonstrates a simple example on how to insert a table in a word document.

  1. Set oWord = CreateObject("Word.Application")
  2. oWord.Visible = True
  3.  
  4. Set oDoc = oWord.Documents.Add()
  5. Set oRange = oDoc.Range()
  6. oDoc.Tables.Add oRange,4,2
  7.  
  8. Set objTable = oDoc.Tables(1)
  9. objTable.Cell(1, 1).Range.Text = "Name"
  10. objTable.Cell(1, 2).Range.text = "Level"
  11. objTable.Cell(2, 1).Range.text = "Mr X"
  12. objTable.Cell(2, 2).Range.text = "Administrator"
  13. objTable.Cell(3, 1).Range.text = "Mr Y"
  14. objTable.Cell(3, 2).Range.text = "Manager"
  15. objTable.Cell(4,1).Range.text = "Mr Z"
  16. objTable.Cell(4, 2).Range.text = "Accountant"

This gives you a simple tabular display of your data.

You will notice that, in the above example we have written statements to write data into table every time we need. at the same time we can minimise this if we have our data in a collection or in a datasource coming from the database. In that case we can use a loop and counter for the rows and columns. In the next example you will see how we can do this in a formatted table. by formatted table it means that applying word formatting to your table.

  1. Set oWord = CreateObject("Word.Application")
  2. oWord.Visible = True
  3.  
  4. Set oDoc = oWord.Documents.Add()
  5. Set oRange = oDoc.Range()
  6. oDoc.Tables.Add oRange,1,3
  7. Set objTable = oDoc.Tables(1)
  8. objTable.style = "Table Grid"
  9.  
  10. strComputer = "."
  11.  
  12. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  13. Set colItems = objWMIService.ExecQuery("Select * from Win32_Process")
  14.  
  15. nRow = 1
  16.  
  17. objTable.Cell(nRow, 1).Range.Font.Bold = True
  18. objTable.Cell(nRow, 1).Range.Text = "Process"
  19. objTable.Cell(nRow, 2).Range.Font.Bold = True
  20. objTable.Cell(nRow, 2).Range.Text = "Description"
  21. objTable.Cell(nRow, 3).Range.Font.Bold = True
  22. objTable.Cell(nRow, 3).Range.Text = "Path"
  23.  
  24. For Each objItem in colItems
  25. nRow = nRow + 1
  26. objTable.Rows.Add()
  27. objTable.Cell(nRow, 1).Range.Font.Bold = True
  28. objTable.Cell(nRow, 1).Range.Text = objItem.Name
  29. objTable.Cell(nRow, 2).Range.text = objItem.Description
  30. if objItem.Executablepath <> "" then objTable.Cell(nRow, 3).Range.text = objItem.Executablepath
  31. Next

a formatted table looks like below

The above example uses WMI to retrieve the system process information and display the details into the document in a table. For more information on WMI scripting please refer earlier post by Ankur – Windows Management Instrumentaion. Same way the data can be displayed from a data source. Refer earlier post by me … to get the data in a data source from a database.

Till now, we discussed how to create/open a document and write into it. what else can we do in a document? There could be a heavy requirement of searching a particular text/data in the document. Word has this cool feature inbuilt into it, let us see how easily we can use this. You will need to specify a range within which you want word to search for the specified string. Following example demonstrates how to search a string (“QTP”) in a specified document.

  1. Set oWord = CreateObject("Word.Application")
  2. Set oDoc = oWord.Documents.Open("C:\Test1.doc")
  3.  
  4. searchString = "QTP"
  5.  
  6. For n = 1 To oDoc.Paragraphs.Count
  7. startRange = oDoc.Paragraphs(p).Range.Start
  8. endRange = oDoc.Paragraphs(p).Range.End
  9. Set tRange = oDoc.Range(startRange, endRange)
  10. tRange.Find.Text = searchString
  11. tRange.Find.Execute
  12. If tRange.Find.Found Then
  13. msgbox "Word found in the document"
  14. End If
  15. Next
  16. oDoc.Close
  17. oWord.Quit
  18. Set oDoc = Nothing
  19. Set oWord = Nothing

In the next part we will see how to compare two documents and other important examples which can be used.