Book a Demo

Author Topic: Excel closes after execution  (Read 4705 times)

royvanmarrewijk

  • EA User
  • **
  • Posts: 61
  • Karma: +1/-0
    • View Profile
Excel closes after execution
« on: July 28, 2016, 05:57:32 pm »
Hi,

I am busy writing a script that export content to an Excel file. This is what I got:

Code: [Select]
!INC Local Scripts.EAConstants-JScript
!INC IXAS Doc Lib.Log


/*Excel Application functions*****************************************************************/

function Excel()
{
//Attributes
this.app = new ActiveXObject("Excel.Application");

//Operations
this.destroy = _destroy;
}

function _destroy()
{
this.app.Quit();
}

/*Excel Sheet functions***********************************************************************/

function Sheet()
{
//Attributes
this.sheet = new ActiveXObject("Excel.Sheet");
this.sheet.Application.Visible = true;

//Operations
this.addText = _addText;
this.destroy = _destroy;
}

function _addText(column, row, text)
{
this.sheet.ActiveSheet.Cells(column, row).Value = text;
}

/*Main***********************************************************************/

function openExcel()
{
var excel = new Excel();
var sheet = new Sheet();

return sheet;
}

function main()
{
//Clear log windows
Repository.ClearOutput("Script");
Repository.EnsureOutputVisible("Script");

var sheet = openExcel();

sheet.addText(1,1, "Cheers");

log.info("Document generation completed. ");
}

main()


When I run this script it opens Excel and writes the text in the given cell but when the script finishes the script wil destroy the object en so the Excel application closes. Can some of you tell me how to keep Excel open after finishing the script? I would like to realize this without saving the document.

Kind regards,
Roy
« Last Edit: July 28, 2016, 05:59:07 pm by royvanmarrewijk »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Excel closes after execution
« Reply #1 on: July 28, 2016, 06:05:57 pm »
I faintly remember from a distant past that I saved my excel output as well.
Not sure if there is another way, but it might be that I didn't bother searching for another solution.

Geert

royvanmarrewijk

  • EA User
  • **
  • Posts: 61
  • Karma: +1/-0
    • View Profile
Re: Excel closes after execution
« Reply #2 on: July 28, 2016, 10:17:42 pm »
I played arround with objects and variabled and solved the problem:

Code: [Select]
!INC Local Scripts.EAConstants-JScript
!INC IXAS Doc Lib.Log

function _addText()
{
this.app.ActiveSheet.Cells(1,1).Value = "Cheers";
}

function Document(document)
{
this.document = document;
this.app = this.document.application;

this.addText = _addText;

}

function _createXlsx()
{
var template = "Z:\\600 - Ontwerp\\610 en 710 - TTI\\DO TTI\\EA_template\\Excel.xltx"
var doc = this.app.Workbooks.Add(template);

var document = new Document(doc);

return document;

}

function _destroy()
{

}

function XlsxGenerator()
{
this.app = new ActiveXObject("Excel.Application");
this.app.visible = true;

this.createXlsx = _createXlsx;
this.destroy = _destroy;

}

function openXlsx()
{
var xlsxGenerator = new XlsxGenerator();
var xlsx = xlsxGenerator.createXlsx();

return xlsx;
}

function main()
{
//Clear log windows
Repository.ClearOutput("Script");
Repository.EnsureOutputVisible("Script");

var document = openXlsx();
document.addText();

log.info("Document generation completed. ");
}

main()