Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can QTP compare two MS word documents
#1
Solved: 11 Years, 4 Months ago Question 
Hi,

Can QTP compare two word documents and report back the changes

Thanks,
Yogini
Reply
#2
Solved: 11 Years, 4 Months ago
Hi,

I think its possible, we can compare documents line by line.

Regards
KrishDeep
Reply
#3
Solved: 11 Years, 4 Months ago
Find the below code to Comapare Two word documents.

Code:
F_Path_1="D:\Compare\Document1.rtf"
F_Path_2="D:\Compare\Document2.rtf"

Public Function CompareFiles (F_Path_1, F_Path_2)

Dim FS, first_file, second_file
Set FS = CreateObject("Scripting.FileSystemObject")

  'Function will simply exit if the size of both files being
' compared is not equal

If FS.GetFile(F_Path_1).Size <> FS.GetFile(F_Path_2).Size Then
CompareFiles = True
Exit Function
End If

  'OpenAsTextStream Method: Opens a specified file and returns
'a TextStream object that can be used to read from, write to,
'or append to the file.

Set first_file = FS.GetFile(F_Path_1).OpenAsTextStream(1, 0)
Set second_file = FS.GetFile(F_Path_2).OpenAsTextStream(1, 0)

CompareFiles = False

  'AtEndOfStream Property: Read-only property that returns True
'if the file pointer is at the end of a TextStream file; False
'if it is not.

  Do While first_file.AtEndOfStream = False
      
  'The Read method reads a specified number of characters from
'a TextStream file

Str1 = first_file.Read(1000)
Str2 = second_file.Read(1000)

  'The StrComp function compares two strings and returns 0 if
'the strings are equal

CompareFiles = StrComp(Str1, Str2, 0)
  If CompareFiles <> 0 Then
   CompareFiles = True
   Exit Do
  End If
Loop

first_file.Close()
second_file.Close()
End Function

File1 =inputbox ("Enter first file nam")
File2 = inputbox ("Enter second file name")

If CompareFiles(File1, File2) = False Then
MsgBox File1 & " and " & File2 & " " &  "are identical."
Else
MsgBox File1 & " and " & File2 & " " &  "are NOT identical."
End If

Regards,

Reply
#4
Solved: 11 Years, 4 Months ago
I don't like the straight file compare method published here. I developed a method that uses Word built in compare revisions functionality. And this has the nice feature that you can get the system to save a word document that shows the differences between the two documents with markup that shows the differences.

The following code can be placed inside a Word document and then you can call the function from another place.
Thus, this is writen in VBA rather than VB script
Code:
Public Function CompareDocument(FileNameDoc1 As String, FileNameDoc2 As String, Optional blnIgnoreDateDiffs As Boolean = False, Optional ByRef pstrErrText As String) As Integer
Dim origDoc As Document, intDiffs As Integer, strFileNameDiffs As String, lngDiff As Long, intRevisionType As Integer, strRevisionType As String, strRevisionText As String
Dim intPrevRevisionType As Integer, strPrevRevisonText As String, lngRevisionStartPos As Long, lngPrevRevisionStartPos As Long
Dim lngRevisionEndPos As Long, lngPrevRevisionEndPos As Long, strPotentialDate As String
Dim intPosNextPara As Integer, intTextBoxDifs As Integer, strErrText As String
Dim intRealDiffCount As Integer, intDateOnlyDifference As Integer, intDeletionAndInsertOfSameTextDiffCount As Integer


    Documents.Open FileName:=FileNameDoc2, _
        ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False, _
         Revert:=False, Format:=wdOpenFormatAuto
    Set origDoc = ActiveDocument ' save a pointer to our original doc
        ActiveDocument.Compare Name:=FileNameDoc1, DetectFormatChanges:=False, CompareTarget:=wdCompareTargetNew, _
                                IgnoreAllComparisonWarnings:=True, AddToRecentFiles:=False
    ' ActiveDocument becomes the result of merging/comparing the two documents
    
    'MsgBox ActiveDocument.Name & " - revision count : " & CStr(ActiveDocument.Revisions.Count)
    intDiffs = ActiveDocument.Revisions.Count ' number of differences word reckons there are

    If intDiffs = 0 Then
        intRealDiffCount = 0
        ActiveDocument.Close (False)  ' Close and dont save to document that resulted from the Compare

    Else ' word thinks there are some differences. Find cases where we have deleted text and inserted text straight after
         ' For such cases check if the difference is a change in date and keep score of how many differences are due to this change
         ' Also compare the apparently deleted and insertion texts are the same and count such cases
        For lngDiff = 1 To intDiffs
               intPrevRevisionType = intRevisionType
               strPrevRevisonText = strRevisionText
               lngPrevRevisionStartPos = lngRevisionStartPos
               lngPrevRevisionEndPos = lngRevisionEndPos
               strRevisionText = ActiveDocument.Revisions(lngDiff).Range.Text
               lngRevisionStartPos = ActiveDocument.Revisions(lngDiff).Range.Start
               lngRevisionEndPos = ActiveDocument.Revisions(lngDiff).Range.End
               intRevisionType = ActiveDocument.Revisions(lngDiff).Type

              
               'MsgBox lngDiff & " " & strRevisionType & " revision text: " & strRevisionText & " start pos " & lngRevisionStartPos & " prev type " & intPrevRevisionType & " prev text " & strPrevRevisonText & " prev end pos " & lngPrevRevisionEndPos
               If intPrevRevisionType = wdRevisionDelete And intRevisionType = wdRevisionInsert And intPrevRevisionType <> 0 And lngPrevRevisionEndPos = lngRevisionStartPos Then
                    ' This if statement determines if there was a deletion of text
                    ' and then an insertion of text right afterwards
                    
                    
                    If strPrevRevisonText = strRevisionText Then
                    ' same text inserted as deleted - count this
                        intDeletionAndInsertOfSameTextDiffCount = intDeletionAndInsertOfSameTextDiffCount + 2 ' to of the observed differences are due to deletion and insertion of the same text
                                             ' we are going to subtract intDeletionAndInsertOfSameTextDiffCount off the total difference count at the end of the process
                                            
                       ' MsgBox lngDiff & "Deleted |" & strPrevRevisonText & "| and apparently inserted the same|"
                    Else ' not the same text inserted as deleted. It might be just a date change - check
                        
                        'MsgBox lngDiff & "Deleted |" & strPrevRevisonText & "| and inserted |" & strRevisionText & "|"
                        
                        ActiveDocument.Revisions(lngDiff).Range.Select
                        Selection.EndOf wdParagraph, wdExtend '' in some situations this selects an entire block of text. I think it does this when the text is a field
                                                               ' However, we use this to find the position of the end of the paragraph.
                        intPosNextPara = Selection.End ' note end of selection
                        'ActiveDocument.Range(lngRevisionStartPos, intPosNextPara - 1).Select ' select from the begining of the revision up to one less than the end of the selection
                        ' Works for "17 May 2006<newParagraph>"
                        ' but this still causes a selection of a whole block of a field
                        ' Field ... 17 May2006<sectionend> -- does not work
                        
                        ' The act of selecting the end of the field causes the beginging of the field to be selected as well
                        ' And we get a lot of text before the revision in question.
                        
                        ' To get around this don't to a .Select but just extract the text from the Range of the lngRevisionStartPos
                        ' and just before the end of the paragraph
                        '
                        strPotentialDate = ActiveDocument.Range(lngRevisionStartPos, intPosNextPara - 1).Text
    
                        
                         If isDate(strPotentialDate) Then
                            'blnDeletionAndThenInsertionOnDateField = True
                            intDateOnlyDifference = intDateOnlyDifference + 2 ' two differences are due to dates only

                          Else
                           ' Not a date only difference - the insertion and deletion text is different. This is thus a
                           ' genuine difference. Thus don't increment the intDateOnlyDifference or the intDeletionAndInsertOfSameTextDiffCount

                          End If
                    End If
                    
               Else ' revisions are not a deletion then an insertion straight after
                 ' This is thus a
                ' genuine difference. Thus don't increment the intDateOnlyDifference or the intDeletionAndInsertOfSameTextDiffCount
               End If
              
              
        Next 'Next  revision
        intRealDiffCount = intDiffs - intDeletionAndInsertOfSameTextDiffCount ' substract away the pseudo differences where the same text was inserted as deleted
        If blnIgnoreDateDiffs Then
  
            'MsgBox "ignore date diffs"

            intRealDiffCount = intRealDiffCount - intDateOnlyDifference  ' if we are running with  blnIgnoreDateDiffs = TRUE
                                             ' then we are only interested non- date differences
            
            
        End If 'blnIgnoreDateDiffs
                    ' MsgBox origDoc.Path & "\" & Left(origDoc.Name, Len(origDoc.Name) - 4) & "_diff.doc"
        strFileNameDiffs = origDoc.Path & "\" & Left(origDoc.Name, Len(origDoc.Name) - 4) & "_diff.doc"
            
        ActiveDocument.SaveAs FileName:=strFileNameDiffs, AddToRecentFiles:=False
        ' Save the differences document if Word has it flagged that there are differences
        ' save a differences document even if we think the differences are only Date differences
        ' or if there are apparenly insertion of same text difference. so we can double check
        ActiveDocument.Close (False)
    End If ' word thinks there are some differences.
    intTextBoxDifs = CompareTextBoxes(FileNameDoc1, FileNameDoc2, pstrErrText)
    origDoc.Close (False)  ' Close and dont save FileNameDoc1
    intRealDiffCount = intRealDiffCount + intTextBoxDifs
     CompareDocument = intRealDiffCount
End Function
Reply
#5
Solved: 11 Years, 4 Months ago
Yes, I also liked the Word built in feature to compare documents. You will find this in third part of the ongoing series
Automating Word Documents
here is the simple peice of code to compare two documents -
Code:
Doc1= "c:\Test1.docx"
Doc2= "c:\Test2.docx"
    set oWord = createobject("Word.Application")
     oWord.Visible =true
     set oCompared = oWord.Documents.Open(Doc2)
    oCompared.Compare(Doc1)
    oCompared.Close

Reply
#6
Solved: 11 Years, 4 Months ago
Where is part 3 of Automating word docs? Your link takes me to part 1. And that brings me to this thread.
https://www.learnqtp.com/automating-word...nts-part-3 does not exist. So I can't find part 3
Reply
#7
Solved: 11 Years, 4 Months ago
this is an on-going series, third part will be available soon.

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UFT Word automation wannel 1 2,334 10-31-2016, 06:59 PM
Last Post: wannel
  Compare WebTable Elements saraiado 1 2,559 06-11-2015, 06:54 PM
Last Post: venkatesh9032
  How to compare two binary values Naresh 0 2,349 09-09-2014, 05:06 PM
Last Post: Naresh
  Can QTP AOM identify word documents that are already opened anil2u 2 4,382 09-05-2013, 12:45 PM
Last Post: anil2u
  how to compare excel with datatatble diya 4 5,458 12-04-2012, 05:39 AM
Last Post: diya

Forum Jump:


Users browsing this thread: 1 Guest(s)