ShawnW Posted March 16, 2010 Posted March 16, 2010 (edited) I have an application I wrote that has the option to export some results to an excel sheet. The problem is that even though I close the book, I cannot open the excel file until the script exits. The order of excel function calls (in case it helps) are as follows. $oExcel = _ExcelBookNew(0) _ExcelWriteSheetFromArray($oExcel,$user,1,1,0,1) _ExcelRowInsert($oExcel,1,1) ;several _ExcelWriteCell() calls adding column names $oExcel.ActiveSheet.Columns("A:M").AutoFit _ExcelBookSaveAs($oExcel, $filePath,"xls",0,1) _ExcelBookClose($oExcel) Now it all looks good once I close the program and open the saved file. But I'd like to be able to open the file as soon as it is saved without closing the application. Thanks Shawn Edited March 16, 2010 by ShawnW
enaiman Posted March 16, 2010 Posted March 16, 2010 I think you have an error somewhere; check your code again. This works very well: #include <excel.au3> $oExcel = _ExcelBookNew(0) _ExcelWriteCell($oExcel, "test 123", 1, 1) _ExcelBookSaveAs($oExcel, "c:\temp\test.xls") _ExcelBookClose($oExcel) $oExcel = _ExcelBookOpen("c:\temp\test.xls", 0) MsgBox(0, "Cell 1:1 content", _ExcelReadCell($oExcel, 1, 1)) _ExcelBookClose($oExcel) SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
ShawnW Posted March 17, 2010 Author Posted March 17, 2010 Thanks for the reply, I realize now I wasn't completely clear. Yes opening the excel file again from within the program and reading it works, but I was talking about the physical file. For example try changing that code to this. #include <excel.au3> $oExcel = _ExcelBookNew(0) _ExcelWriteCell($oExcel, "test 123", 1, 1) _ExcelBookSaveAs($oExcel, "c:\temp\test.xls") _ExcelBookClose($oExcel) MsgBox(0, "blah", "blah") Now the file is closed before the MsgBox call, but try opening it from your computer before you close the MsgBox so the script is still running. The odd thing is it does not look like a normal file lock error. Doesn't even open in read only mode, it just bugs up.
picaxe Posted March 17, 2010 Posted March 17, 2010 _ExcelBookClose does not release object $oExcel, AutoIt automatically releases objects on script exit but if you need the xls file to be accessible before the script exits, you must specifically delete the object.#include <excel.au3> $oExcel = _ExcelBookNew(0) _ExcelWriteCell($oExcel, "test 123", 1, 1) _ExcelBookSaveAs($oExcel, "c:\temp\test.xls") _ExcelBookClose($oExcel) $oExcel = 0 ; delete object MsgBox(0, "blah", "blah")
ShawnW Posted March 17, 2010 Author Posted March 17, 2010 _ExcelBookClose does not release object $oExcel, AutoIt automatically releases objects on script exit but if you need the xls file to be accessible before the script exits, you must specifically delete the object.#include <excel.au3> $oExcel = _ExcelBookNew(0) _ExcelWriteCell($oExcel, "test 123", 1, 1) _ExcelBookSaveAs($oExcel, "c:\temp\test.xls") _ExcelBookClose($oExcel) $oExcel = 0 ; delete object MsgBox(0, "blah", "blah") Nice! Just what I needed, thanks!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now