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.
CreateObject(ServerName.TypeName, RemoteServerName)
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.
oWord.Documents.Add
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
oWord.ActiveDocument.SaveAs "c:\test.doc"
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.
oWord.Documents.Open "C:\test.doc"
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.
How can I perform bold text on more number of words in a paragraph.
Eg. We are constantly looking for ways to improve our services and also for new quality content to publish. If you feel that you can contribute to the worldwide QTP community, or have any professional materials that you would like us to publish, then please contact us and we will gladly help.
In the above paragraph if I want to make the words (contribute, QTP community, please contact us etc) bold is there any ASCII Chr() like constant to make only that particular words in bold.
other than QTP. can we use any other method to automate word document..like any open source tool that is easily available?
its is very helpful.. easy to understand.. crystal clear.. good work..
pls let me know how to find (server name,type) for word application
excellent work!! it really helps me 4 my proj
but how to read a document line by line?
hi ,
I am new to QTP.I have write a script for selecting the file menu options(File–>Open or save or save as etc) in Ms-office 2003 with out using the shortcut keys.
Please can any one help me
Sir Im new in QTP and i use ver. 11.0
some of the codes dont work when i try it
Dim oWord produces an error
What is the purpose of the parameter 6 in the property oSelection.EndKey?
Thank You
What is the error code Nirmala?
Hi,
In my virtual PC 2007 QTP 9.2,
Set oWord = CreateObject(“Word.Application”)
It is giving error message.
I am not getting it that what method or property I should use.
Please guide me.
Thanks in advance.
HI,
I have been following this very blog for quite sometime, and as and when I feel my knowledge in QTP has started saturating, this blog has something to offer myriads of surprises. Once again a wonderful post here.
thanks buddy, do posting such innovative techniques!!!
Followup to my previous comment:
Based on some other stuff I did automating word I tried using the “Word.Document” object rather than the “Word.Application” object as shown below.
Set oWordDoc = CreateObject(“Word.Document”)
set oDocs = oWordDoc.Application.Documents
This code causes a new blank doc to open and the oDocs object has two members.
The members of the oDocs object are
1. The newly opened blank doc and
2. one of the Word Docs that was opened by my AUT but not the other one.
So it appears to me that there are two instances of Word here and I can only get at one of them. I would like to get at both of them.
So this shows me how to create a new word doc or open an existing one given you know its name and path. I have a different requirment.
My application under test (AUT) launches Word and creates a number of documents. And what I want to do is loop through the open documents and extract the file names and paths and then close the documents.
And if I do.
Set oWord = CreateObject(“Word.Application”)
set oDocs = oWord.Documents
then oDocs.count is zero even though I have two Word documents open that were created by my AUT.
So my question is – how do you use automation to access Word Documents not launched by the VBscript in QTP? How do you get to existing open Word instances?
wow.!!!! It’s really gud.!!
Hi Nandu,
for this you will need to refer object model of the application you want to use. like for MS Word if you read the post carefully you will find the link of Word object model and I have also provided the link for methods and properties that can be used. hope that will help you.
Hi..
This article is very usefull..
but if we want to practice.. how could we know the Object names and list of available objects for Ms Word or any other application for a newbie..
here in below example Dim oWord
Set oWord = CreateObject(“Word.Application”)
oWord.Documents.Open “c:\test.docx”
in the above example u r using createObject(“word.application”) — how to know the exact name of servername.typename of word or any other apps.. (“Word.appliction”..)
oWord.Documents.Open “c:\test.docx” — where can we get this info that available methods or supported obj’s by word/excel..
please provide detail info about CreateObject()..
This helps a lot for newbie’s..
Thanx
Thank you all 🙂
very nice
the content is really useful.
Good work Saket…..
It is very good example for handling MS work in QTP.
Thank you Saket to bring this information and it is useful our daily automation activities.
regards,
Veeraiah
Hey Ankur,
This is really useful. Our company deal with documents a lot, this is really helpful.
Thank You,
Vipul Borad.
gud work..!
quite intresting..!