Antonio Alvarez
Antonio Alvarez

Reputation: 61

How to change the content of a text box in excel from javascript

I´m working on a javascript code that creates a Excel using the ActiveXObject command (IE exclusive). At some point I create a text box in the following way:

excel = new ActiveXObject('Excel.Application');
excel.Workbooks.Add();
excel.Application.Visible = true;
var vFirma1 = excel.ActiveSheet.Shapes.AddTextbox(1, 200, 200, 200, 200);
vFirma1.TextFrame.Characters.Text = "Test Box"; //Error here!!!

The text box is created correctly but when I try to add a value it throws the next error:

the object doesn't support this property or method

Any help will be appreciated.

Upvotes: 0

Views: 442

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

TextFrame.Characters is a method with optional parameters. VBScript/VBA can hide this from the programmer, but JScript can't:

excel = new ActiveXObject('Excel.Application');
excel.Workbooks.Add();
excel.Application.Visible = true;
var vFirma1 = excel.ActiveSheet.Shapes.AddTextbox(1, 200, 200, 200, 200);
WScript.Echo("vFirma1\t", typeof vFirma1);
WScript.Echo("TextFrame\t", typeof vFirma1.TextFrame);
WScript.Echo("Characters\t", typeof vFirma1.TextFrame.Characters);
WScript.Echo("Characters()\t", typeof vFirma1.TextFrame.Characters());
WScript.Echo("Characters().Text\t", typeof vFirma1.TextFrame.Characters().Text);
vFirma1.TextFrame.Characters().Text = "Test Box"; //No Error here!!!

output:

cscript 18390537.js
vFirma1  object
TextFrame        object
Characters       unknown
Characters()     object
Characters().Text        string

Upvotes: 1

Related Questions