javascriptms-wordrtfenterprise-architect

Enterprise Architect javascript - working with word RTF and linked elements


I have an Enterprise Architect project with some Diagram. I want to add them to an external Word document (which cannot be generated with EA). Moreover, other people may need to edit this word document without having the EA project. So here is what I did to do so :

I created an EA JavaScript file which generate rtf files (1 .rtf file for each diagram). In my main word document, whenever I wanted to insert the diagram, I added a link to the specific rtf file previously generated. Everything is working fine, except, after some times, I cannot generate the rtf file anymore (when running docGenerator.SaveDocument), it tells me "Failed to save the output file".

GenerateRTF :

function OnProjectBrowserScript()
{
    // Get the type of element selected in the Project Browser
    var treeSelectedType = Repository.GetTreeSelectedItemType();
    switch ( treeSelectedType )
    {
        case otPackage :
        {
            // Code for when a package is selected
            [...]
            packageDiagrams = thePackage.Diagrams; // Collection of all the Diagrams
            
            for ( var i = 0 ; i < packageDiagrams.Count ; i++ ) {
                var currentElement as EA.Diagram;
                currentDiagram = packageDiagrams.GetAt( i );
            
                // Document Generation using docGenerator
                var docGenerator as EA.DocumentGenerator;
                docGenerator = Repository.CreateDocumentGenerator();
                docGenerator.NewDocument("")
                docGenerator.DocumentDiagram(currentDiagram.DiagramID,1,"Diagram Report");
                var diagramName = currentDiagram.Name + ".rtf";

                //Save diagram into rtf document
                var saveSuccess = docGenerator.SaveDocument( diagramName, EA.DocumentType.dtRTF );
                if ( saveSuccess ) {
                    Session.Prompt( "Documentation complete!", promptOK );
                } else {

                    // -- > Error message saying file cannot be saved <--
                    Session.Prompt( "Error saving file " + diagramName + " : " + docGenerator.GetLastError(), promptOK );

                }
            
            }
            break;
        }
        [...]

This "problem" is that my RTF files are used/linked in another document (if I don't use them in another file as a link, I can generate as many times as I want the rtf files).

Moreover, I never open the generated rtf files with Word (as I don't want Word to lock them ....). Also, when generating the rtf files, my main word document is also closed. I think the problem comes from the EA rtf file generation as I already used this method with another tool generating rtf and I did not get any problem.

How could I pass by the protection when writing the rtf file ? Is there another method to export the file from EA which could be used instead of using docGenerator ?


Solution

  • Thanks for all your comments that lead me to the solution. Ideed, problem was not due to a file generation problem but was relater to Word wth its links between files.

    What I previously did, in the main document to import my RTF file (sorry if the name are not identicial to what you can see in your version of Word, I don't have the english version) :

    Insert > Object > From a file > I selected the file and checked the Link to the file box

    However the solution below worked like a charm :

    Insert > text from a file > I selected the file and the trick part : click on to arrow next to the Insert button in order to Instert as a Link

    Be aware that, with this solution, you can now directly edit the inserted text in the main word document which was not possible with the pevious method. So methodology depends on what you need to do but I don't understand in which case you would use the first method with the links and locked files ...