Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Sunday 5 May 2019

Word Doc in UFT/QTP

Why we need to use  Word documents in the test scripts...?
  1. The Application under test produces some kind of output in word document and testing need to verify it.
  2. The test needs to take some input data from the Word document.
  3. The test script requires outputting a test log in word format.
Automating a Word doc in UFT/QTP,  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 first statement will be to get the object reference using CreateObject.


Set oWord = CreateObject("Word.Application")



To create a new document, we 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 we want  to write some text into our document. For this we can use Selection property which basically represents a selected area in the document or an insertion point.

oWord.Selection.TypeText "This document is generated by UFT."

We done with creating our document, now we 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:\testdata.docx"

Create Simple Word Document... ?

Dim oWord
Set oWord = CreateObject("Word.Application")
oWord.Documents.Add
oWord.Selection.TypeText "This document is generated by UFT."
oWord.ActiveDocument.SaveAs "c:\testdata.docx"
oWord.Quit
Set oWord = Nothing

Same way we can use Open method to open an existing document.

oWord.Documents.Open "C:\test.doc"

Opening a Document and Append some Text into it... ?

Dim oWord
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open "c:\testdata.docx"
oWord.Selection.TypeText "This text entered to the existing document."
oWord.ActiveDocument.Save
oWord.Quit
Set oWord = Nothing

Append New Text at the End of Document... ?

Dim oWord
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open "c:\testdata.docx"
oWord.Selection.EndKey 6,0
oWord.Selection.TypeText " This text entered to the existing document. "
oWord.ActiveDocument.Save
oWord.Quit
Set oWord = Nothing


Note: Here 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 we 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.

Suppose if we need to see the Word document during execution of our statements use its property "Visible"

oWord.Visible = True

We can use ActiveDocument Property for different tasks.

Opens a Document and Prints the Document..?

Dim oWord
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open "c:\testdata.docx"
oWord.Activedocument.PrintOut
oWord.Quit
Set oWord = Nothing

Search a given String in a specified document...?

Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open("C:\Test1.doc")
searchString = "UFT"
For n = 1 To oDoc.Paragraphs.Count
    startRange = oDoc.Paragraphs(n).Range.Start
    endRange = oDoc.Paragraphs(n).Range.End
    Set tRange = oDoc.Range(startRange, endRange)
    tRange.Find.Text = searchString
    tRange.Find.Execute
    If tRange.Find.Found Then
       msgbox "Word found in the document"
    End If
Next
oDoc.Close
oWord.Quit
Set oDoc = Nothing
Set oWord = Nothing

Comparing two Word documents ... ?

Dim Doc1, Doc2, oWord, oCompared
Doc1= "C:\Test1.doc"
Doc2= "C:\Test2.doc"
Set oWord = CreateObject("Word.Application")
oWord.Visible = true
Set oCompared = oWord.Documents.Open(Doc2)
oCompared.Compare(Doc1)
oCompared.Close

Extracting all strings(words) from Word Documents.... ?

Dim oWord, Doc, currentDocument
Doc = "C:\Test1.docx"
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = 0
oWord.Documents.Open Doc
Set currentDocument = oWord.Documents(1)
Dim nWords, n
nWords = currentDocument.Words.count
Print  "Number of Words: " & nWords
For n = 1 to nWords
    Print currentDocument.Words(n)
Next
currentDocument.Close
Set currentDocument = Nothing
oWord.Quit
Set oWord = Nothing

Writing the Text in a New Line... ?

Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"
objWord.Selection.EndKey 6,0
objWord.Selection.TypeText vbCrLf & "testing time 2"
objWord.ActiveDocument.Save
objWord.Quit
Set objWord=Nothing

Format the Text in the Word Document... ?

Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"
objWord.Selection.Font.Size ="18"
objWord.Selection.Font.Name ="18"
objWord.Selection.Font.bold = true
objWord.Selection.EndKey 6,0
objWord.Selection.TypeText vbCrLf & "testing time 3"
objWord.ActiveDocument.Save
objWord.Quit
Set objWord=Nothing

Reading content of a Word Document and Storing in a String ...?

Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"
set objdoc = objWord.Activedocument
strWordtext = ""
iWordCnt= objdoc.Words.count
For i = 1 to iWordCnt
 strWordtext = strWordtext + objdoc.Words(i)
Next
msgbox strWordtext
objWord.Quit
Set objWord=Nothing

Finding content In Word Document..?

Public Function fnFindInWordDocument(strFilePath, strStringToFind)
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open(strFilePath)
isFound =False
For n = 1 To oDoc.Paragraphs.Count
    startRange = oDoc.Paragraphs(n).Range.Start
    endRange = oDoc.Paragraphs(n).Range.End
    Set tRange = oDoc.Range(startRange, endRange)
    tRange.Find.Text = strStringToFind
    tRange.Find.Execute
    If tRange.Find.Found Then
       isFound =True
     Exit For
    End If
Next
If isFound =True Then
    fnFindInWordDocument =True
Else
    fnFindInWordDocument =False
End If
oDoc.Close
oWord.Quit
Set oDoc = Nothing
Set oWord = Nothing
End Function


Replace a String in a word document with another String ... ?

Dim obj_word,obj_doc,obj_words
Set obj_word = CreateObject("Word.Application")
Set obj_doc = obj_word.documents.Open("D:\UFT_Practice\UFT.docx")
Set obj_words = obj_doc.Words
For i = 1 To obj_words.Count
   obj_words.Item(i).Text = Replace(obj_words.Item(i).Text, "UFT""Mercury")
Next
obj_doc.Save
obj_doc.Close
obj_word.Quit
Set obj_words = Nothing
Set obj_doc = Nothing
Set obj_word = Nothing



Creating a Table and Insert a some text into Table ..?

Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add()
Set oRange = oDoc.Range()
oDoc.Tables.Add oRange,4,2
Set objTable = oDoc.Tables(1)
objTable.Cell(11).Range.Text = "Name"
objTable.Cell(12).Range.text = "Designations"
objTable.Cell(21).Range.text = "Mr X"
objTable.Cell(22).Range.text = "Tester"
objTable.Cell(31).Range.text = "Mr Y"
objTable.Cell(32).Range.text = "Lead"
objTable.Cell(4,1).Range.text = "Mr Z"
objTable.Cell(42).Range.text = "Manager"
oDoc.Close
oWord.Quit
Set oDoc=Nothing
Set oWord=Nothing

OutPut:

Name
Designations
Mr X
Tester
Mr Y
Lead
Mr Z
Manager

No comments:

Post a Comment

Note: only a member of this blog may post a comment.