Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi all,
Actually i made the code in which onloading event of body i want to open Excel which is in one function and onClick event of checkbox i want to work with that excel in another function.
Can anyone please tell me how to pass excel object in other function.
Thanks in advance.
<html>
<head>
<script type="text/javascript">
function initialize()
{Excel = new ActiveXObject("Excel.Application"); 
Excel.Visible = false; 
a=Excel.Workbooks.Open("C:/desktop/P1."+x).sheets(1);
return a;
}
function search(b)
{n=b.usedrange.rows.count;
alert(n);
}
</script>
</head>
<body  önload="x=initialize()">
<input type="checkbox" id="b1"  önClick="search(x)">
</body>
</html>
Posted
Updated 13-Apr-11 7:00am
v2
Comments
TweakBird 13-Apr-11 13:01pm    
Please use <pre> tag for code blocks.
Manfred Rudolf Bihy 15-Apr-11 8:32am    
Sorry Farah, for posting untested code yesterday. I thought ironing out the most blatant mistakes would suffice, but alas it wasn't enough. This time I got my sample code fixed and please try it out heeding the message right below "Modification".
Thanks,

1 solution

Restructure your code a bit analogous to this:

Modification:

Sorry Farah for posting untested code. This time I tried out the example I posted below and it works. So please try to resolve your issue by using the code below as an example. I removed some funny characters that were there. Instead of an o in onclick and onload there was an ö both times. I adjusted the path in my example because I had to generate an Excel workbook for the test. Please substitute your path instead, but look at what you did in your excel path. I'm not sure why you wanted to append x to the end of the filename because x would have been the excel worksheet (see your önload in the body tag).

XML
<html>
<head>
<script type="text/javascript">
    // This global variable may not really be needed, but you might want to use it later on so I made it global
    var myExcelSheet = undefined;

    function initialize(){
        Excel = new ActiveXObject("Excel.Application");
        Excel.Visible = false;
        return Excel.Workbooks.Open("C:/temp/test.xlsx").sheets(1);
    }

    function search(sheet){
        var n = sheet.usedrange.rows.count;
        alert(n);
    }

    function onBodyLoaded()
    {
        // Sets the global variable myExcelSheet here
        myExcelSheet = initialize();
    }
</script>
</head>
<body  onload="onBodyLoaded();">
<form>
    <input type="checkbox" id="b1"  onClick="search(myExcelSheet)">
</form>
</body>
</html>



You should really debug this using IE's "Developer Tools" where you can set breakpoints in your javascript and then go through your code step by step.

Happy coding!

-MRB
 
Share this answer
 
v4
Comments
Farah Siraj 14-Apr-11 3:45am    
Thanks all
I have a strange problem now that MS script debugger is installed in my system but it doesn't showing any error even the dialogue box doesn't ask "do you want to debugg"
How can i see error?
Manfred Rudolf Bihy 14-Apr-11 5:32am    
What version of IE are you using?
"Developer Tools" is found under the tools menu in IE8 and IE9.
Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900