Sign in to follow this  
Followers 0

_ExcelBookClose() problems

5 posts in this topic

Posted (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 by ShawnW

Share this post


Link to post
Share on other sites



Posted

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)

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

_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")

Share this post


Link to post
Share on other sites

Posted

_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!

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0