Sign in to follow this  
Followers 0
cag8f

Problems with _ExcelCloseBook

12 posts in this topic

_ExcelCloseBook() has been working just fine for me for a few weeks now; it would close the Excel file I had previously opened. But all of a sudden it stopped working as expected. Here is my test code (taken from the help file):

#include <Excel.au3>
$oExcel = _ExcelBookNew() ; Example 1 - Create a Microsoft Excel window

MsgBox ( 0, "", _ExcelBookClose($oExcel))

Exit

Excel is opened and a new file is created. However upon script completion Excel stays open. Furthermore I cannot click anywhere in the window, even to close it. I can't even move the window (I can resize it though). I can only close it by killing it in task manager. Also, the message box returns 0, indicating that the file close failed. Probably something stupid, but any thoughts?

Share this post


Link to post
Share on other sites



@error would give you a little more info. I have the same problem sometimes, has to do with an existing excel.exe process created by another script or something else that opened a workbook but didn't close it properly.

Share this post


Link to post
Share on other sites

Task manager does not show any other Excel processes running. I'm a noob; what is the syntax for using @error? Can I simply put it in a message box?

Share this post


Link to post
Share on other sites

Task manager does not show any other Excel processes running. I'm a noob; what is the syntax for using @error? Can I simply put it in a message box?

Something like this should work:

#include <Excel.au3>
$oExcel = _ExcelBookNew() ; Example 1 - Create a Microsoft Excel window
$excelBookCloseResult = _ExcelBookClose($oExcel)
$excelBookCloseErr = @error
MsgBox ( 0, "", "Result: " & $excelBookCloseResult & @CRLF & "Error: " & $excelBookCloseErr)
Exit

Share this post


Link to post
Share on other sites

I ran that and got Result = 0 Error = 1. The same problem with the Excel workbook was exhibited.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Can you close Excel with this as the last line before exit?...

$oExcel.Application.Quit

Edit: Just trying anything until someone that has a better idea sees your thread :x

Edited by MrMitchell

Share this post


Link to post
Share on other sites

Thanks for your help. Yes that did close Excel, but I'd obviously like for _ExcelCloseBook to work. Might there be some open workbooks that are hidden somehow? I've rebooted the machine a few times.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Yah put this in to see how many workbooks are present before your _ExcelBookClose()

MsgBox(0, "", $oExcel.Application.Workbooks.Count)

Add: Also, maybe change your ExcelBookClose statement to enable alerts to see what might be holding it up:

$excelBookCloseResult = _ExcelBookClose($oExcel, 1, 1)

Edited by MrMitchell

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

-- Inserting the check for open workbooks before the _ExcelCloseBook returns 1.

-- _ExcelBookCloseResult returns 0.

When I enable message alerts in _ExcelBookClose, Excel still stay open, but I can now click around in the window and even close it from the window. While if I disable message alerts, I am not able to do so. Curious.

Edited by cag8f

Share this post


Link to post
Share on other sites

-- Inserting the check for open workbooks before the _ExcelCloseBook returns 1.

-- _ExcelBookCloseResult returns 0.

When I enable message alerts in _ExcelBookClose, Excel still stay open, but I can now click around in the window and even close it from the window. While if I disable message alerts, I am not able to do so. Curious.

Yea if you check the code for that function it first checks how many workbooks are open and if only 1 it will quit the app. If more than 1 it just closes that workbook that you're on. Something like that

Share this post


Link to post
Share on other sites

Still not resolved, but a workaround is in place. Thanks for all the help.

Share this post


Link to post
Share on other sites

I know this is old and a workaround exists, but I've found a simpler workaround that may help others. Whatever variable you use with _ExcelBookNew() or _ExcelBookOpen() must be set to zero after using _ExcelBookClose(). For example:

$oExobject=_ExcelBookNew()
;...do stuff
_ExcelBookClose()
$oExobject=0

This will immediately end the EXCEL.EXE process and allow you to maniputlate the saved Excel file with other functions.

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