Jump to content

Recommended Posts

Posted

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

Posted

@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.

Posted

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?

Posted

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
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
Posted

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.

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
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
Posted

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

  • 1 year later...
Posted

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.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...