Ask a Question

Comparing Word field exchange to testcomplate value

SOLVED
kevinbenson
Occasional Contributor

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.

8 REPLIES 8
rraghvani
Super Contributor

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

rraghvani_0-1664531698465.png

 

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)
}

 

 

 

 

AlexKaras
Community Hero

Hi,

 

As you don't need to interact with Word but just need to get document data, it is recommended to work with Word using COM.

https://support.smartbear.com/testcomplete/docs/tutorials/samples/common/msoffice.html might provide you with initial ideas. Also search for Word COM Model to get understanding of its COM model and possible code samples.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
kevinbenson
Occasional Contributor

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?

 

kevinbenson
Occasional Contributor

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

rraghvani
Super Contributor

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. 

kevinbenson
Occasional Contributor

I love you rraghvani,

 

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

thank you

kevinbenson
Occasional Contributor

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.

kevinbenson_0-1664779885217.png

 

rraghvani
Super Contributor

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.

 

cancel
Showing results for 
Search instead for 
Did you mean: