Forum Discussion

KB1's avatar
KB1
Champion Level 2
2 years ago
Solved

Comparing Word field exchange to testcomplate value

I'm busy with creating a automated test to compare the field exchange values of a word document to text on a website.

There I'm stuck and don't know how to make testcomplete open the word document or print it out.

  • I've just provided example code of opening a word document (document not provided); display number of paragraphs and output the first paragraph. It should be enough to get you started.

     

    A better version of the example code,

    function Test4()
    {
        var word = getActiveXObject("Word.Application");
        word.Documents.Open('C:\\Temp\\Word.docx');
        word.Visible = true;
        
        var paragraphs = word.ActiveDocument.Paragraphs;
        for (var i = 1; i <= paragraphs.Count; i++) {
            Log.Message(paragraphs.Item(i));
        }
        
        word.Quit(false);
    }

     which outputs all paragraphs. 

  • If the document structure is always the same, but the content is different. Then instead of having paragraphs output to log, you can store each paragraph into an array, which you can then use afterward e.g. to compare date, select fields etc.

     

    You need to define your data structure, and decide which values you intent to use, operation to perform etc. if applicable.

     

8 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    See Adding Tested Applications - add the appropriate command line parameter to open the word document.

     

    Or, to access and interact with the ribbon menu, add the WndClass name to your project settings to access Microsoft Active Accessibility

     

    Or, via OleObject,

     

    function Test3()
    {
        var word = Sys.OleObject("Word.Application");
        word.Documents.Open('C:\\Temp\\Word.docx');
        word.Visible = false;
        
        var paragraphs = word.ActiveDocument.Paragraphs;
        Log.Message(paragraphs.Count);
        Log.Message(paragraphs.First.Range.Text);
    
        word.Quit(false)
    }

     

     

     

     

    • KB1's avatar
      KB1
      Champion Level 2

      The botton code only opens up Words and does not open any documents. and  how do I let testcomplete after opening the right document compare certain pieces of text?

       

    • KB1's avatar
      KB1
      Champion Level 2

      And after that.?  the code samples didn't help me further

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I've just provided example code of opening a word document (document not provided); display number of paragraphs and output the first paragraph. It should be enough to get you started.

     

    A better version of the example code,

    function Test4()
    {
        var word = getActiveXObject("Word.Application");
        word.Documents.Open('C:\\Temp\\Word.docx');
        word.Visible = true;
        
        var paragraphs = word.ActiveDocument.Paragraphs;
        for (var i = 1; i <= paragraphs.Count; i++) {
            Log.Message(paragraphs.Item(i));
        }
        
        word.Quit(false);
    }

     which outputs all paragraphs. 

    • KB1's avatar
      KB1
      Champion Level 2

      Can I ask you 1 more thing.

      After I got the word document in my log. how do I compare it to something automatically? 

      I want to extract certain lines, or extract everything does not matter.

       

    • KB1's avatar
      KB1
      Champion Level 2

      I love you rraghvani,

       

      I forgot to add the extra "\". after adding that it worked.

      thank you

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If the document structure is always the same, but the content is different. Then instead of having paragraphs output to log, you can store each paragraph into an array, which you can then use afterward e.g. to compare date, select fields etc.

     

    You need to define your data structure, and decide which values you intent to use, operation to perform etc. if applicable.