We have already learned the basics of automating word documents with QTP in the previous parts(Part 1, Part 2) of this series, using which you can achieve your required tasks easily. In this part, I would like to give some common examples that can be used as per the requirement.
Compare 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
Above example uses word’s in built feature of comparing documents.
Extract all words from Word document
Below example demonstrates to extract all the words from a word document also retrieves the number of words in the document.
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
Convert Word document to html/pdf
Following function can be used to convert a word document to pdf or excel. Required file will be created in the same folder of the word document file
ConvertDOCtoHTMLPDF("C:\Test1.docx","Pdf") ConvertDOCtoHTMLPDF("C:\Test1.docx","html") Function ConvertDOCtoHTMLPDF(SrcFile, Format) Dim fso, oFile, FilePath, myFile Set fso = CreateObject( "Scripting.FileSystemObject") Set oFile = fso.GetFile(SrcFile) FilePath = oFile.Path Dim oWord Set oWord = CreateObject("Word.Application") oWord.Documents.Open FilePath If ucase(Format) = "PDF" Then myFile = fso.BuildPath (ofile.ParentFolder ,fso.GetBaseName(ofile) & ".pdf") oWord.Activedocument.Saveas myfile, 17 elseIf ucase(Format) = "HTML" Then myFile = fso.BuildPath (ofile.ParentFolder ,fso.GetBaseName(ofile) & ".html") oWord.Activedocument.Saveas myfile, 8 End If oWord.Quit Set oWord = Nothing Set fso = Nothing Set oFile = Nothing End Function
This Test case is Passed.
How to print “Passed” in above sentence in Bold and Green color.
Hi Ankur,
Could you please share how can we validate font, style and color of the text present in word document using UFT.
Its very urgent. Thanks in Advance.
Regards,
@Sreelatha: Just refer the Word Automation Object Model. For ex: Here is the one for Font.
How can i compare a word document and a web page?? Can you please help me with this???
Great Article Ankur.
I am working on a VSTO Application with WPF objects in Word Doc. QTP does not identify the WPF object. On spying it shows the entire WPF window as WinObject.
Can you please help on how to approach this problem?
Thanks,
Maya
I am sorry to have missed these additional points:
– .Net and WPF addins are loaded to UFT 11.5
– I do open UFT first and then the VSTO application
– In the Record and Run setting, I do select the Windows tab
hi Ankur,
In the above script for converting word doc to HTMl and PDF docs, I am not able to pass arguments to functions as is given.
However by using the call statement as
Call ConvertDOCtoHTMLPDF ,I can pass arguments.
Thanks
Hi Ankur,
Could you please let us know how to get No of Pages of a Word document?
Thankyou
Hi Ankur
Can you please help me to provide some points on below.
QTP versus OTA approach for loading and extracting requirements and tracing: SVK to provide a write-up detailing the pros and cons of the two approaches
Thanks
Vijay palem
Hi Ankur,
I want to have some details about HP certification for QTP. I will be very grateful if you can give me the answers.
# What all HP certifications are available for QTP.
# I have seen skill levels of both Intermediate and Foundational. If its correct, is it possible for me to get a certification of Intermediate without appearing for a Foundational level?
# Can you please share some sites that will be useful for preparing the exam.
Thanks in advance.
In Extract all words from Word document script Print is not working, it is displaying an error message Type Mismatch, I have tried it with Msgbox function and then it is displaying the results. Please tell me how to use Print in the above script
can you tell about panes(1).pages
and how to get word count from particular page
Eg:
if 5 pages are there how to get word count of the 4th page
i got it right. Thanx
In “Compare two Word documents” script mentioned above how can we check the result? coz after the run is complete it does not show anything in the results generated.
Can some one help me converting PDF to word using QTP code? There are quite a few free wares that do this but need to do this using QTP code.
Thanks in advance.
Dear Ankur,
it is very good and valuable information,
if possible please send QTP 11 video Tutorial Please,
Thank you
hey Ankur,
Can you please tell me how can we handle inserted images position in QTP. For e.g. i have made a script that will open a word file and will insert an image, now i want to set position,size. how will this be handled?
it is a nice topic