Forum Discussion

tristaanogre's avatar
tristaanogre
Esteemed Contributor
14 years ago

Comparing FormFields in a Word Document

This is not a request for information this time, but simply a sharing of information for anyone who may need this.



Currently, there's a published Script Extension that allows you to compare the text of two different word documents.  You can find this extension at http://smartbear.com/support/viewarticle/17635/.  This works VERY well for finding differences in two word documents...except if the two word documents include form fields that may change.



In order to do that, you need to utilize the actual Word.Application OleObject directly in your automation code.  It's actually very easy to do.  Here's the code where I'm specifically filtering out CheckBoxes in the form fields in the word document and looking for the data within them.






Sub CompareWordFormFields(ExpectedFile, ActualFile)






    Dim WordAppObject, ActualWordDoc, ExpectedWordDoc, Index


    


    Set WordAppObject = Sys.OleObject("Word.Application")


    WordAppObject.Documents.Open(ActualFile)


    Set ActualWordDoc = WordAppObject.ActiveDocument


    WordAppObject.Documents.Open(ExpectedFile)


    Set ExpectedWordDoc = WordAppObject.ActiveDocument


    For Index = 1 to ExpectedWordDoc.FormFields.Count step 1


        If ExpectedWordDoc.FormFields.Item(Index).Type = 71 Then


            If (ExpectedWordDoc.FormFields.Item(Index).Result <> ActualWordDoc.FormFields.Item(Index).Result) Then


                Call Log.Warning("The check boxes " + ExpectedWordDoc.FormFields.Item(Index).Name + " and " + ActualWordDoc.FormFields.Item(Index).Name + " do not match")


            End If


        End If


    Next


    Call WordAppObject.Documents.Close

    Call WordAppObject.Quit

End Sub




I'm sure there's a LOT of enhancements that can be done for this including filtering for different field types, actually creating constants that enumerate the different field times (for code readability), etc.  I spent a bit of time today coming up with this and I thought there were others who might be able to use it.



Enjoy!