A new three part series on Automating Word Documents with QTP by Saket Kumar.
Many of us are familiar with the concept of using Excel Sheets in our tests. It could be either using QTP methods like Import sheet, Import, Export etc. or using the instance of COM component to deal with an external excel sheet. If you are an experienced QTP user, you can probably mange it do whenever you need. You can refer earlier posts by Ankur for Excel and QTP Part 1, Part2, Part3 for using Excel sheet. This article focuses mainly on automating Word in QTP. If you are already using Excel in your tests it will be quite easier for you, if not, nothing to worry, and both are similar except of some methods and properties. Read the whole article and you will be familiar of using Word and it is quite similar and will be easier for you to work with excel as well.
Let us first understand why one would need to deal with Word documents in the test scripts. Here are some possible reasons-
The Application under test produces some kind of output in word document and testing need to verify it
The test needs to take some input data from the Word document.
The test script requires outputting a test log in word format.
These are just a few;there could be many reasons for this depending on your testing requirement like you can see at our QTP forums, there are lots of questions asked on automating word for different reasons – Replace a string in a document with another string ,Can QTP compare two MS Word Documents ,How to compare two Word Documents. You can find many similar threads discussed there.
Automating a Word in QTP is not very hard, you must be aware that we can use CreateObject Method to create an instance of a COM component. It accepts two arguments one mandatory and another optional.
ServerName is the name of application that provides the object or the application which provides Com interface. We can call them as automation servers. TypeName is the type or class of object to be created. RemoteServerName is used to create the object on a remote server.
For word documents the application or the automation server is “Word” and the possible class for which we would like to create the objects for a Word Document would be Application.
We use set statement to assign the object reference to a variable. So our very first statement will be to get the object reference using CreateObject.
Set oWord = CreateObject("Word.Application")
Now we can manipulate a Word document using different methods and properties of Word object. Refer MSDN Library – Microsoft Word Object Model for a complete list of methods and properties of Word Object. The very first thing which would be required is to create a new document or open an existing document.
To create a new document, you will have to add a new document to the word application. Use Document property which returns the documents collection and use Add method to create a new blank document.
This will create a new blank document. Now its time to write some text into your document. For this you can use Selection property which basically represents a selected area in the document or an insertion point.
oWord.Selection.TypeText "Hello World!!! This document is generated by QTP."
Once you are done with creating your document, you need to save it. To save it on a new location use “SaveAs” else use “Save” method to save it on default location
Do not forget to quit from the application and destroy the object variables used at the end.
Below the simple example to create a new document
Dim oWord Set oWord = CreateObject("Word.Application") oWord.Documents.Add oWord.Selection.TypeText "Hello World!!! This document is generated by QTP." oWord.ActiveDocument.SaveAs "c:\test.doc" oWord.Quit Set oWord = Nothing
Same way you can use Open method to open an existing document.
below example demonstrates opening a document and append some text into it.
Dim oWord Set oWord = CreateObject("Word.Application") oWord.Documents.Open "c:\test.docx" oWord.Selection.TypeText "This text has been entered by opening the existing document" oWord.ActiveDocument.Save oWord.Quit Set oWord = Nothing
You will see the text mentioned has been written to your document. But wait, it was supposed to append at the last but it appended at the beginning only. It is the selection property which is taking care of all these. By default it always represents the beginning of the document. So you will need to put the statements for selection at the end before entering the text. Below example will append new text at the end of document.
Dim oWord Set oWord = CreateObject("Word.Application") oWord.Documents.Open "c:\test.docx" oWord.Selection.EndKey 6,0 oWord.Selection.TypeText " This text has been entered by opening the exisitng document. " oWord.ActiveDocument.Save oWord.Quit Set oWord = Nothing
We have used EndKey method to do this, which moves the selection to the specified unit. The two parameters are unit and Extend. We have used 6 (wdStory) for unit which is to move at the end of story and 0(wdmove) as extend which is to move the selection. If you don’t specify these parameters, selection moves to the end of line.
The Selection Property can be used for different purposes like setting Font, Size, alignment, paragraph etc. Below example demonstrates using some of these properties. Refer End Key Method for more details on these parameters.
Dim oWord Set oWord = CreateObject("Word.Application") oWord.Documents.Open "c:\test.docx" Dim oSelection Set oSelection = oWord.Selection With oSelection .EndKey 6,0 .TypeParagraph() .Font.Size ="14" .Font.Name = "Arial" .Font.Bold = True .TypeText " Feeling better now! " .Font.Bold = False .TypeParagraph() .ParagraphFormat.Alignment =2 .Font.Italic = True .TypeText "- Saket Kumar" End With oWord.ActiveDocument.Save oWord.Quit Set oWord = Nothing
Basically, what we are doing in the above example is we are setting the style of text before typing it in the document, TypeParagraph is used to specify a paragraph in your document. The other properties like Bold Italic alignment is used to set the style for the text to be written. If you want a particular text as bold or italic it should be true otherwise set it as false if not needed. Similarly for alignment you will have to set the alignment as right left or center. By default it is left. But you will need to specify a value which can be one of the ‘pbParagraphalignmenttype’ constants. For left it is 0, 1 –center, 2- right. Refer PbParagraphAlignment Type for more enumeration type. Note that when you set a particular style, it is set for the rest of the document. So always make sure to reset the things if you don’t need. Like you can see the bold has been reset in the example above.
We can use different properties of Application object as well for various purposes. Like suppose if you need to see the Word document during execution of your statements use its property ‘visible’
oWord.Visible = True
you can use ActiveDocument property for different tasks. For example below statements opens a document and prints the document.
Dim oWord Set oWord = CreateObject("Word.Application") oWord.Documents.Open "c:\test.docx" oWord.Activedocument.PrintOut oWord.Quit Set oWord = Nothing
I hope it will help you to start with, In the next part we will discuss more on automating word with some more examples.
If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.