This blog outlines how to do scripting for test cases that involve editing of word documents or verification of the contents of a word document.

The script would need to have:

To open an MS word document:

The Range(x,y) specifies the place at which text will be inserted. “.Text” allows to insert text in the word document at the location specified by parameters x and y. Here we insert text “Hello World!!!” at the beginning of the document.

Once the document is edited, it should be saved before closing. So, just use “.save()” or “.saveas()” to save the document.

This was all about editing a word document. Now, we will validate the changes in the document. So, for validating open the document again. Capture all the text of the word document in a variable (filetextall). Check whether the selection includes the text you are looking for (“Hello World!!!” in this case):

We can easily manipulate this code to create a new instead of opening an existing document.

All we need to do is:

Replace:

With:

Just remember one thing, there should be no instance of word already open on your system (Check task manager to ensure that no Winword.exe is running). I noticed that in case an instance is already open, the command documents.open() throws error.

Happy Scripting!!!

Enhanced by Zemanta