MS Word automation and getting to the objects
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
MS Word automation and getting to the objects
S
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. ⬇️⬇️⬇️
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Worked like a charm!
I owe you a drink!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
> I owe you a drink!
You may start with giving a Kudo to Helen and marking her reply as a solution 😉
/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
================================
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oh, I kudosed it immediately! How do I mark it as the answer?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
> 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.
/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
================================

- « Previous
-
- 1
- 2
- Next »
- « Previous
-
- 1
- 2
- Next »