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
19 REPLIES 19
Highlighted
Occasional Contributor

Note with the enable visibility option (second one) doesn't work for me. I'v tried it earlier. I get the same error.

 

As for the first one, I've tried implementing

Sys.Process("Winword").Window("Shell_TrayWnd").Activate();

I'm not sure what this Shell_TrayWnd is. This is the error I get after I insert the above line into my code.

 

Cannot obtain the window with the window class 'Shell_TrayWnd', window caption '*' and index -1. See Additional Information for details. 9:31:30 Normal

Highlighted
Regular Contributor

You've got a word Automation Problem.

A variable can not be accessed by Name.

You have to iterate the variables and check for the given Name to get Access to Your variable.

Highlighted
Occasional Contributor

I have a piece of VBA code that iterates through all the variables in a document and prints them.

I use it to check if I've placed all the variables into my Word document. 

 

Private Sub ListAllVariables()
    Dim myVariable As Variable
    
    For Each myVariable In ThisDocument.Variables
        Debug.Print myVariable.Name & " : " & myVariable.Value
    Next
End Sub

How can I implement this in my TC function? 

 

I know how to iterate through arrays and stuff in Javascript, but how can I iterate something I don't have access to?

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

Highlighted
Occasional Contributor

Thank you HKosova! Adding the ".Item()" made my function work. 
Here is the final version:

function myFunc (){

var objWordApplication = Sys.OleObject("Word.Application");
objWordApplication.Documents.Open('C:\InvoiceLatTemplate.doc');

//objWordApplication.Visible = true;
var WordDoc = objWordApplication.ActiveDocument;
var myVariable = WordDoc.Variables.Item("Group").Name;
var myVariable1 = WordDoc.Variables.Item("Group").Value;

Log.Message(myVariable)
Log.Message(myVariable1)

}

Both name and value are logged properly.

 

One final question: is there a way to iterate through all the variables in a Word file? I have that function that itterates in VBA, but obviously TC script and VBA are not the same...

Highlighted
Moderator


dasabaja wrote:

One final question: is there a way to iterate through all the variables in a Word file? I have that function that itterates in VBA, but obviously TC script and VBA are not the same...


Use a for loop from 1 to Variables.Count and access the variables using Variables.Item(index):

for (var i = 1; i <= WordDoc.Variables.Count; i++)
{
  variable = WordDoc.Variables.Item(i);
  Log.Message(variable.Name + ": " + variable.Value);
}

Keep in mind that MS Office indexing is 1-based, not 0-based.


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
Highlighted
Occasional Contributor

Worked like a charm!

I owe you a drink!Smiley Happy

Highlighted
Community Hero

> I owe you a drink!Smiley Happy

You may start with giving a Kudo to Helen and marking her reply as a solution 😉

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
================================
Highlighted
Occasional Contributor

Oh, I kudosed it immediately! How do I mark it as the answer?

Highlighted
Community Hero

> I kudosed it immediately

Ah, looked at another reply by Helen, my bad...

 

> How do I mark it as the answer?

There is an 'Accept as Solution' link in the Options menu that is on the right of the corresponding post.

 

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