cancel
Showing results for 
Search instead for 
Did you mean: 

MS Word automation and getting to the objects

SOLVED
Highlighted
Super Contributor

MS Word automation and getting to the objects

Does anyone have examples of the very BASICS of interacting with MS Word objects with TC? I would like to know how to get a handle on the Selection object of the current selection in the currently open document, for example. I am not that savvy with Word automation to begin with. Getting there though! Thanks



S
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator


dasabaja wrote:

After implementing the line that opens the document I got a different error.

TypeError
WordDoc.Variables is not a function
Error location:
Unit: "HR\HR SmartTemplates\Script\Unit1"
Line: 8 Column: 26.

Any hints on what this might be?


Change Variables("Group") to Variables.Item("Group"). The former is VBA/VBScript syntax, but in other languages you need to use the .Item() method to access collection items in MS Office COM objects.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Learn SmartBear products in a fun and easy way and prove your knowledge!
>>Participate in the TechCorner Challenge today

View solution in original post

19 REPLIES 19
Highlighted
Super Contributor

Hi Lane,


I recommend that you perform your validations via COM. All MS Office applications provide very convenient and deep access to their documents this way, so you should be able to check anything you want. To obtain a COM object, use the Sys.OleObject property (for example, to obtain MS Word, use word = Sys.OleObject("Word.Application")).


To find out which objects, properties and methods you should use to work with the Microsoft Word application, see the http://msdn.microsoft.com/en-us/library/aa221371(v=office.11).aspx MSDN article.

I’m not a member of the SmartBear Support Team, I’m just helping users in this community. Any views or opinions expressed do not necessarily represent those of SmartBear Software.
Highlighted
Contributor

I agree, COM Interface and OLE standards are the way to go.



I have just been working on a OfficeWordCheckpoint that into parameter a word file stored in TestComplete Stores item.

I planned to make it available within a Script extension but in the meantime, if you want to work with Word document contents, I strongly recommend you to take a look at the Interop API  :



http://msdn.microsoft.com/en-us/library/ms254954.aspx



If you are trying to acces text content, you will certainly need to use interfaces such as Document, ActiveDocument, Range, Paragraph, Words.

Here is a very simple js example. Hope this help :



var objWordApplication = Sys.OleObject("Word.Application");

var WordDoc = objWordApplication.ActiveDocument;

var ParagraphsContent = WordDoc.Paragraphs;

var objCurrentParagraph = ParagraphsContent.First;

var idxParagraph = 0;

while(objCurrentParagraph != null) {

    Log.Message(objCurrentParagraph.Range.Text; )

    objCurrentParagraph = objCurrentParagraph.Next(); 

}
Highlighted
Super Contributor

That is EXACTLY the info I was looking for. Thanks.
Highlighted
Super Contributor

How does one handle the vagaries of non-standard VBA synatx? I have something that in Word VBA would look like



objActiveWindow.Selection.GoTo What:=wdGoToBookmark, Name:="\para"



(where I would not have an objActiveWindow object of course. I could go directly to the selection.)



But TC does not like the := syntax.



Can someone advise?  Thanks
Highlighted
Super Contributor

Hi Lane,


Try using the following code instead:



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

Set objActiveWindow = objWordApplication.ActiveWindow

objActiveWindow.Selection.GoTo What, , , "\para"


The fact is that VBA doesn't require certain order of the parameters. You can specify parameters in any order using the Param_Name := "Param_Value" syntax. However, VBScript requires that all the parameters be specified in the order declared in the method's description. If you need to omit some parameters, you can just specify spaces for them and delimit the parameters using commas.


I hope this information helps 🙂

I’m not a member of the SmartBear Support Team, I’m just helping users in this community. Any views or opinions expressed do not necessarily represent those of SmartBear Software.
Highlighted
Occasional Contributor

Hi all.

 

Sorry for bringing this topic from the dead, but I'm having kinda the same problems like the original poster.

There's an awesome answer on how to  manipulate document content, but what I'm interested in is reading variables from a .doc file added using VBA.

 

I have a function that adds variables to a Word file

 

Private Sub AddVariable()
        
     ThisDocument.Variables.Add "Group", "Group"

End Sub

So, I have a variable named Group that has a value Group. It's stupid, I know, but just as an example.

 

I would like to use TC and read the value of the variable Group. The document is open at the same time as TC, it's in windows' processes.

 

From what I read here (and oh similar topics), this is the function I've come up with in TC.

 

function myFunc (){

var objWordApplication = Sys.OleObject("Word.Application");

objWordApplication.Visible = true;

var WordDoc= objWordApplication.ActiveDocument;
var myVariable = WordDoc.Variables("Group").Value;

Log.Message(myVariable)

}

This doesn't work. When I type „WordDoc.“ , there is no intellisense after the dot. As if there is no Variables property.

 

Also, the error i get is:

 

JavaScript runtime error

 

Cannot read property 'Variables' of undefined.

 

So, I'm kinda stuck.

I could really use some help!

Highlighted
Community Hero

The error you're getting means that the WordDoc variable is not getting anything.  Not sure why the ActiveDocument call is not working.

 

Try the following instead.  Make sure that Word is closed and not running and do the following:

 

function myFunc (){

var objWordApplication = Sys.OleObject("Word.Application");

objWordApplication.Documents.Open(myDocumentPath);

var WordDoc= objWordApplication.ActiveDocument;
var myVariable = WordDoc.Variables("Group").Value;

Log.Message(myVariable)

}

Replace the bold with the actual file path and name of the document.  See if that works.

 


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Highlighted
Occasional Contributor

Hi, tristaanogre.

 

Sorry for the late reply, I wasn't in my office during the weekend.

 

After implementing the line that opens the document I got a different error.

 

TypeError
WordDoc.Variables is not a function
Error location:
Unit: "HR\HR SmartTemplates\Script\Unit1"
Line: 8 Column: 26.

Any hints on what this might be?

Highlighted
Community Hero

Hi,

 

At the first glance your code seems to be correct... The only idea that I have at the moment is related to the specifics of Office applications described in the "Note for Microsoft Office users" section of the https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/sys/oleobject-propert... topic. Does it help?

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
================================
New Here?
Join us and watch the welcome video:
Announcements
Join TechCorner Challenge!
Want a fun and easy way to learn TestComplete? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get properties of a web page element

See replies!

Compare images using the Region Checkpoint

See replies!

Compare HTML table with Excel file and correct data in Excel file

Participate!

How to execute remote test and obtain results via Test Runner REST API

Participate!
Top Kudoed Authors