cancel
Showing results for 
Search instead for 
Did you mean: 

MS Word automation and getting to the objects

SOLVED
dasabaja
Occasional Contributor

Re: MS Word automation and getting to the objects

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

Re: MS Word automation and getting to the objects

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

Re: MS Word automation and getting to the objects

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
Moderator

Re: MS Word automation and getting to the objects


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. ⬇️⬇️⬇️

View solution in original post

dasabaja
Occasional Contributor

Re: MS Word automation and getting to the objects

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
Moderator

Re: MS Word automation and getting to the objects


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

Re: MS Word automation and getting to the objects

Worked like a charm!

I owe you a drink!Smiley Happy

AlexKaras
Community Hero

Re: MS Word automation and getting to the objects

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

Re: MS Word automation and getting to the objects

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

AlexKaras
Community Hero

Re: MS Word automation and getting to the objects

> 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