Forum Discussion

ARAKOTONDRABESA's avatar
ARAKOTONDRABESA
Contributor
15 years ago

Microsoft Excel ActiveWorkBook -> (Null Object)

Hi everybody!

I have to save a Excel file generate by my firm application but I have a problem: When I inspect my object oExcel -> I have  ActiveWorkBook (Null Object) and ActiveWindow is null object. So I can't save my open excel file. Have you a solution? This code run with other excel file. (I verify my excel file : macro is disabled ; my excel file is not read only)



function SaveExcel(sPath)

{

 var oExcel = Sys.OleObject("Excel.Application");

 var oWorkBook = oExcel.ActiveWorkBook;

 oWorkBook.SaveAs(sPath);

 oExcel.ActiveWindow.Close();

}

thanks

5 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    The code you posted does not give any indication that it is opening an Excel file.  I'm assuming, then, that you're expecting the Excel file to be already open in windows when you call that code.  Please verify that this is the case.
  • Hi!

    When I launch this function, my Excel File is already opened by my application , but it's not saved. So maybe my problem with ActiveWorkBook is here? so have you a solution?( I work with Excel 2003)

    Thanks a lot

    Andry 
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    I have no specific solution here as, when I tried it (opened an excel sheet and then called the function), it worked fine for me.



    While the file is not read only, could it be that your application that is generating the file still has some sort of lock on it?
  • Hi! Can you try to open an Excel File an edit something into it without saving? after save it with this function...normally you must have a ActiveWorkBook (null object) .So if you put a breakpoint in the line 2 and if you inspect the oExcel object you have the propertie ActiveWorkBook (null object)
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    I created a new Excel sheet, entered data in a couple of fields, ran the function up to the point where it assigned oWorkbook... oWorkbook was not-null.



    So... Again, I'm not sure what the problem is as I cannot reproduce it in a simple environment which tells me that it's a problem either in your environment or in the sheet itself (where it is not available to the Excel.Application).