Skip to main content
Inspiring
February 21, 2017
Answered

ExtendScript - postworddocupdate

  • February 21, 2017
  • 2 replies
  • 664 views

1) How can I instantly alter the topics, after generating/updating a Word-document?

   I tried this with no luck, see the results in my experiment.

2) How can I make sure that my ExtendScript is running only on the Word-Documents that 'Generate or Update'?

   We will make projects with 20 or more linked Word-documents. So if one changes we won't run the script on all.

Experiment:

I have a script as following.

Searching all topics within a project => cleanup all inline styling/topic.

That works fine.

I registered the script as an event.   (Event: postworddocupdate)

   I assumed that this event would trigger from the moment a generate or an update, of a Word-document, has finished. Meaning the topics are made.

Why using this option:

We put multiple Word-documents in one project. We do this with a link, without making a copy of the Word-document.

I linked 2 Word-documents;

    - Doc_A.docx

    - Doc_B.docx

I 'Generate' the Word-document Doc_A.docx

Result in the 'Output View'

Number of Word-documents present: 2                             => project.WordFileManager.count

name Word-document: Doc_A    

name Word-document: Doc_B

Start removing inline styling !                                              => Start searching all topics within the project

Topic: Hello

Location: P:\Mdenbaes\RH_17\STK_N\Hello.htm                => default topic

End removing inline styling !                                                => all topics within the project are handeld

Next:

I 'Generate' the Word-document Doc_B.docx

Result in the 'Output View'

Number of Word-documents present: 2                        

name Word-document: Doc_A

name Word-document: Doc_B

Start removing inline styling !

Topic: Sect steekkaart PC_124

Location: P:\Mdenbaes\RH_17\STK_N\File_A\Doc_A\Doc_A.htm           => now the topic of the First Word-document is found.

Topic: Hello

Location: P:\Mdenbaes\RH_17\STK_N\Hello.htm

End removing inline styling !

As you can guess: If I 'Update' or 'Force update' one of the existing Word-documents: the topics of both Word-documents are shown in the 'Output View'.

This topic has been closed for replies.
Correct answer Willam van Weelden

Hi Marino,

The event delivers the full path of the word document that was updated as a parameter. From there, you can parse the linked word document XML to find the topics that are linked to the document. As you have probably found, ExtendScript offers various low level features, so more advanced stuf like this requires more work.

In my ExtendScript library I use the following code to get the linked files:

var Xpath = "//genfile/filename";

  var loadFilesThatAreLinked = function(filepath) {

  var LinkedFiles = new Array();

        var ContainerFile = new File(filepath);

        if(ContainerFile.exists)

        {

  var ContainerXML = new XML(readFile(ContainerFile));

  var LinkedFilesinContainer = ContainerXML.xpath(Xpath).children();

  for(var i = 0; i< LinkedFilesinContainer.length();i++)

  {

  LinkedFiles.push(LinkedFilesinContainer.toString());

  }

  return LinkedFiles;

        } else {

  return null;

  }

    }

LinkedWordDocs = loadFilesThatAreLinked(projectpath()+WordContainer);

In here, I simply use an XPath to get all topics from a linked Word document, not from a specific Word document. This might be a good starting point. But you'll want to do it a little differently:

  • Load the projects RHWordDoc.apj. Either as an XML object as above or as a token. This might be easier because of the next step.
  • Iterate through the Word documents until you find the document that was updated.

<word_doc>

  <file_name>..\..\..\..\..\Desktop\Verduidelijking eis 6 Noordoostpolder.docx</file_name>

IF you want to get just the file name (if that's sufficient), use the filename() function from my library on the file path.

  • Once you found the correct word document, check the '/genfile/filename' element to find which topics belong to the word document.
  • Iterate through the topics and update them.

2 replies

Willam van Weelden
Willam van WeeldenCorrect answer
Inspiring
February 22, 2017

Hi Marino,

The event delivers the full path of the word document that was updated as a parameter. From there, you can parse the linked word document XML to find the topics that are linked to the document. As you have probably found, ExtendScript offers various low level features, so more advanced stuf like this requires more work.

In my ExtendScript library I use the following code to get the linked files:

var Xpath = "//genfile/filename";

  var loadFilesThatAreLinked = function(filepath) {

  var LinkedFiles = new Array();

        var ContainerFile = new File(filepath);

        if(ContainerFile.exists)

        {

  var ContainerXML = new XML(readFile(ContainerFile));

  var LinkedFilesinContainer = ContainerXML.xpath(Xpath).children();

  for(var i = 0; i< LinkedFilesinContainer.length();i++)

  {

  LinkedFiles.push(LinkedFilesinContainer.toString());

  }

  return LinkedFiles;

        } else {

  return null;

  }

    }

LinkedWordDocs = loadFilesThatAreLinked(projectpath()+WordContainer);

In here, I simply use an XPath to get all topics from a linked Word document, not from a specific Word document. This might be a good starting point. But you'll want to do it a little differently:

  • Load the projects RHWordDoc.apj. Either as an XML object as above or as a token. This might be easier because of the next step.
  • Iterate through the Word documents until you find the document that was updated.

<word_doc>

  <file_name>..\..\..\..\..\Desktop\Verduidelijking eis 6 Noordoostpolder.docx</file_name>

IF you want to get just the file name (if that's sufficient), use the filename() function from my library on the file path.

  • Once you found the correct word document, check the '/genfile/filename' element to find which topics belong to the word document.
  • Iterate through the topics and update them.
Inspiring
February 21, 2017

I don't have a solution for you - sorry - but I do a lot of linking to Word documents and the script you mention is interesting to me. Any chance you can post it in this thread?

Peter Grainge
Community Expert
Community Expert
February 21, 2017

It will not be possible to attach a script to a forum post. I suggest you use a Private Message to give your email address.


See www.grainge.org for RoboHelp and Authoring information

@petergrainge

Use the menu (bottom right) to mark the Best Answer or Highlight particularly useful replies. Found the answer elsewhere? Share it here.
Inspiring
February 21, 2017

Thanks Peter - I sent a private message.