Automating Word Documents: Part 1

Updated On Jul 7, 2014 by

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.


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.Selection.TypeText "Hello World!!! This document is generated by QTP."
oWord.ActiveDocument.SaveAs "c:\test.doc"
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"
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. "
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
.Font.Size ="14"
.Font.Name = "Arial"
.Font.Bold = True
.TypeText " Feeling better now! "
.Font.Bold = False
.ParagraphFormat.Alignment =2
.Font.Italic = True
.TypeText "- Saket Kumar"
End With
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"
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.


Welcome new readers: Download the Free eBook!

Ankur JainI am Ankur Jain, the owner of LearnQTP and founder of a software organization. This site is run, maintained and updated by me and my team. Feel free to take a look around, you may want to start with What is QTP? 

You can connect with me on my Google+ page. 

Please enter your first name and email address below to instantly download the eBook on Optimizing QTP Scripts. In future, we will make sure you get new tips & tricks on QTP delivered direct to your email box.

Share This:


  1. chandra

    its is very helpful.. easy to understand.. crystal clear.. good work..

  2. amol z

    pls let me know how to find (server name,type) for word application

  3. amol z

    excellent work!! it really helps me 4 my proj

  4. Prudhvi

    but how to read a document line by line?

  5. chakri

    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

  6. Jade

    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

  7. Hagay

    What is the purpose of the parameter 6 in the property oSelection.EndKey?

    Thank You

  8. What is the error code Nirmala?

  9. Nirmala


    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.

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

  11. ttguy

    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.

  12. ttguy

    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?

  13. satish bonpally

    wow.!!!! It’s really gud.!!

  14. Saket Kumar

    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.

  15. 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..


  16. Saket Kumar

    Thank you all :)

  17. Radhika

    very nice

  18. the content is really useful.
    Good work Saket…..

  19. Veeraiah

    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.


  20. Vipul Borad

    Hey Ankur,

    This is really useful. Our company deal with documents a lot, this is really helpful.

    Thank You,
    Vipul Borad.

  21. santosh

    gud work..!
    quite intresting..!


  1. Automating Word Documents Part-3 | Learn QTP - [...] have already learned the basics of automating word documents with QTP in the previous parts(Part 1, Part 2) of this …
  2. Automating MS Word Documents – Part 2 | Learn QTP - [...] ← Automating Word Documents: Part 1 [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>