Author Topic: Sparx Jscript and MS-Word GetCrossReferenceItems  (Read 101 times)

BruceTOGAF2

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Sparx Jscript and MS-Word GetCrossReferenceItems
« 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);

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7745
  • Karma: +165/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Sparx Jscript and MS-Word GetCrossReferenceItems
« Reply #1 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