Ask a Question

MS Word automation and getting to the objects

SOLVED
vthomeschoolmom
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
dasabaja
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

Manfred_F
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.

dasabaja
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?

HKosova
SmartBear Alumni (Retired)


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
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
dasabaja
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...

HKosova
SmartBear Alumni (Retired)


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
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
dasabaja
Occasional Contributor

Worked like a charm!

I owe you a drink!Smiley Happy

> 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 Champion]
____
[Community Champions] 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 Champions]
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 Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
dasabaja
Occasional Contributor

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

> 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 Champion]
____
[Community Champions] 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 Champions]
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 Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
cancel
Showing results for 
Search instead for 
Did you mean: