Closing Excel properly?

Discussion in 'JavaScript' started by Yoduh, Aug 8, 2007.

  1. #1
    Hello, I'm fairly new to javascript. I'm making a website for my personal use that will read from Excel and display some data on an HTML page and I've been learning javascript as I go.

    A problem I have is when I leave the website running overnight (it refreshes itself every minute), an error eventually pops up that says
    "Not enough memory to run Microsoft Office Excel. Please close other applications and try again."
    Internet Explorer also gives the error "Automation Server can't create object" on the line
    var excel = new ActiveXObject("Excel.Application");
    Code (markup):
    so anyways heres my code for opening excel
    var excel = new ActiveXObject("Excel.Application");
    excel.visible=false;
    var excelfile = excel.Workbooks.Open("displayinfo.xls");
    var excelsheet = excel.Worksheets("Sheet1");
    Code (markup):
    I use actually vbscript to go through task manager and kill the "EXCEL.EXE" process, but that didnt help get excel out of memory and I tried this code to no avail-
    excelfile.Close(false);
    excel.Quit();
    excel = null;
    setTimeout("CollectGarbage()",1);
    Code (markup):
    I've been searching the internet for a solution for days now, and I've tried my best to solve this on my own. Any help on how to correctly release excel from memory would be greatly appreciated.
     
    Yoduh, Aug 8, 2007 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I wonder if instead of creating a new ActiveX object every minute, you created it once and then reused it by calling just these two lines:
    
    var excelfile = excel.Workbooks.Open("displayinfo.xls");
    var excelsheet = excel.Worksheets("Sheet1");
    
    Code (markup):
    That would seem to me to be equivalent to using the Media Player ActiveX object and then changing the currently playing media file, and not creating a new Media Player for each media file.

    I also think that if the above doesn't display updated information, you might have to prevent the page from being cached. If you are using .aspx files, with C#, you can use this in the script section.
    
    <script runat="server">
        
        protected void Page_Load(object sender, EventArgs e)    
        {
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
        }
    
    </script>
    Code (markup):
    Alternatively, instead of using your setInterval to call your display function, you might just need to force the page to reload, from the server, not the cache, every minute:

    
    <script type="text/javascript">
    
             function init(){
    
                   setTimeout("self.location.reload(true)",60000);
            }
      
              onload=init;
    
    </script>
    Code (markup):
    Forcing the page to reload should destroy the ActiveX object.

    I also think that you should expect problems, because you are not doing this in the traditional way.

    You are attempting to use an Excel spreadsheet as a substitute for using AJAX and an SQL database, probably because you would like to avoid all the coding that would require.

    So, I'd be happy with whatever works, even if that means the page is reloaded every minute.
     
    Mike H., Aug 8, 2007 IP
  3. Yoduh

    Yoduh Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    heyy, sorry for bumping such an old thread, but I finally got my website to stop running out of memory overnight.

    
    excelfile.Close(false);
    excel.Quit();
    excel.Application.Quit();
    excel = null;
    excelfile = null;
    excelsheet = null;
    CollectGarbage();
    setTimeout("CollectGarbage()",1);
    
    Code (markup):
    I'm not sure if the excelfile.Close(false) is necessary, I'm also not sure which one was needed, Quit() or Application.Quit(), but this website of mine has to be literally finished tomorrow, so I'm leaving it in there for now, I really cut it close to the wire trying to solve this problem lol. I learned from a google search that Excel has a bug and doesn't shut down after calling the Quit() method when automating from JScript. To work around this problem, the CollectGarbage() method is used to force JScript's garbage collection to occur almost immediately.

    I just hate when I read threads started by people with similar problems as me, then they post a final "solved the problem kbye" but forget to mention how they solved it :mad: so I just wanted to give a reply for anyone who happens to stumble across this thread with the same problem I had. Mike, I also tried your first suggestion, no luck. But doesn't matter now, problem solved :)
     
    Yoduh, Aug 14, 2007 IP