Knowledge Share

Knowledge is NOT Power IMPLEMENTATION of knowledge is Power!!!
 
HomePortalGalleryCalendarFAQRegisterUsergroupsLog in

Share | 
 

 QTP

View previous topic View next topic Go down 
AuthorMessage
Admin
Admin


Posts : 142
Points : 410
Reputation : 0
Join date : 2007-12-29
Location : Chennai

PostSubject: QTP   Mon Nov 22, 2010 10:21 am

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
Back to top Go down
http://knowledgeshare.forumotion.com
 
QTP
View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Knowledge Share :: Testing :: Automation-
Jump to: