Sparx Systems Forum

Discussion => Automation Interface, Add-Ins and Tools => Topic started by: BruceTOGAF2 on December 08, 2017, 01:57:00 am

Title: Sparx Jscript and MS-Word GetCrossReferenceItems
Post by: BruceTOGAF2 on December 08, 2017, 01:57:00 am
Does anyone know how to use Sparx Jscript to find all headings in a Word document?

// The following lines open the Word document successfully
var wdObj = new ActiveXObject("Word.Application");
wdObj.Visible = true;
var wdDoc = wdObj.Documents.Open(inputFileName);

      
// The following line prints Bookmarks.Count correctly
Session.Output("wdDoc.Bookmarks.Count:" + wdDoc.Bookmarks.Count);

// The following line prints Sections.Count correctly
Session.Output("wdDoc.Sections.Count:" + wdDoc.Sections.Count);
      
// Following line seems to be working because it takes a while in a large document
var myHeadings = wdDoc.GetCrossReferenceItems(wdRefTypeHeading);
      
// Following line shows blank
Session.Output("myHeadings" + myHeadings);
      
// Following line errors with "Object expected"
var lheadingCount = UBound(myHeadings) - LBound(myHeadings) + 1;
      
// Following line errors with "'Count' is null or not an object"
Session.Output("myHeadings.Count:" + myHeadings.Count);
      
// Following line errors with "'length' is null or not an object"
Session.Output("myHeadings.length" + myHeadings.length);
Title: Re: Sparx Jscript and MS-Word GetCrossReferenceItems
Post by: Geert Bellekens on December 08, 2017, 02:11:36 am
I used to do something like that a while ago.
In C# I used code like this:
Code: [Select]
app.Selection.Collapse();
Find findH2 = app.Selection.Find;
findH2.set_Style("Heading 2");
findH2.Text = "";
if (findH2.Execute())
To find the next heading of style "Heading 2"
This moves the selection in Word (which you can can follow visually)

Geert