Following this question, I'm trying to convert the VBScript code on this page to JScript. If I runt the below .hta
code:
<HTML>
<BODY>
<INPUT id=button1 name=button1 type=button value=Button>
<SCRIPT LANGUAGE="VBScript">
sub button1_onclick()
dim app
set app = createobject("Excel.Application")
app.Visible = true
dim wb
set wb = app.workbooks.add
end sub
</SCRIPT>
</BODY>
</HTML>
It opens an empty Excel sheet upon clicking on the button. as expected. However, if I convert the VBScript part to JScript
<HTML>
<BODY>
<INPUT id=button1 name=button1 type=button value=Button>
<script LANGUAGE="JScript">
function button1_onclick() {
// var app = WScript.CreateObject("Excel.Application");
var app = new ActiveXObject("Excel.Application");
app.Visible = true;
var wb = app.Workbooks.Add();
}
</script>
</BODY>
</HTML>
it doesn't open Excel at all when clicking the button. I would appreciate it if you could help me know what is the problem and how I can resolve it. Thanks for your support in advance.
Object_Event
syntax work?It's actually very simple Object_Event
syntax is VBScript, to allow events to automatically bind in JScript you just need to use the correct syntax which is Object::Event
, here is an example using automatically bound events.
<html>
<body>
<input id="button1" name="button1" type="button" value="Button">
<script language="JScript">
function button1::onclick() {
var app = new ActiveXObject("Excel.Application");
app.Visible = true;
var wb = app.Workbooks.Add();
}
</script>
</body>
</html>
Just tested this and it seems to be working, so it may just be you are missing the onclick
event handler as @Teemu suggests in the comments.
<html>
<body>
<input id="button1" name="button1" type="button" value="Button" onclick="return button1_onclick();">
<script language="JScript">
function button1_onclick() {
var app = new ActiveXObject("Excel.Application");
app.Visible = true;
var wb = app.Workbooks.Add();
}
</script>
</body>
</html>
The only real difference is the inclusion of the onclick
event handler on the <input>
element.
onclick="return button1_onclick();"
It displays a button which when clicked opens Microsoft Excel with a new Workbook.