wchan2122 0 Posted July 28, 2010 I want to close an in use excel file with below code but fail with error. Please help >"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\w5\Auto\t4.au3" C:\w5\Auto\t4.au3 (6) : ==> The requested action with this object has failed.: For $element In $oExcel.Application.Workbooks For $element In $oExcel.Application^ ERROR >Exit code: 1 Time: 0.395 _CloseExcel() Func _CloseExcel() $oExcel = ObjGet("", "Excel.Application") For $element In $oExcel.Application.Workbooks If $element.FullName <> "" Then ;MsgBox(0,"",$element.FullName) $element.Save $element.Close Else $element.Activate $oExcel.Application.Dialogs(5).Show EndIf Next $oExcel.Application.Quit EndFunc Share this post Link to post Share on other sites
doudou 5 Posted July 28, 2010 This func will do just what you need: Func _CloseExcel() Local $oExcel = ObjGet("", "Excel.Application") If IsObj($oExcel) Then ConsoleWrite("EXCEL: " & ObjName($oExcel) & @lf) For $wbk In $oExcel.Workbooks ConsoleWrite("Closing workbook: " & $wbk.Name & @lf) $wbk.Close(True) Next $oExcel.Quit() EndIf EndFunc You don't need to check if a workbook is new (FullName property is anyway the wrong place to look - it is always set) or to call Save(), $wbk.Close(True) will save automatically or ask user for path where appropriate. But you always should check IsObj($var) before calling any methods on it. UDFS & Apps:DDEML.au3 - DDE Client + Server[*]Localization.au3- localize your scripts[*]TLI.au3 - type information on COM objects (TLBINF emulation)[*]TLBAutoEnum.au3 - auto-import of COM constants (enums)[*]AU3Automation - export AU3 scripts via COM interfacesTypeLibInspector- OleView was yesterdayCoder's last words before final release: WE APOLOGIZE FOR INCONVENIENCEĀ Share this post Link to post Share on other sites
wchan2122 0 Posted July 29, 2010 This func will do just what you need: Func _CloseExcel() Local $oExcel = ObjGet("", "Excel.Application") If IsObj($oExcel) Then ConsoleWrite("EXCEL: " & ObjName($oExcel) & @lf) For $wbk In $oExcel.Workbooks ConsoleWrite("Closing workbook: " & $wbk.Name & @lf) $wbk.Close(True) Next $oExcel.Quit() EndIf EndFunc You don't need to check if a workbook is new (FullName property is anyway the wrong place to look - it is always set) or to call Save(), $wbk.Close(True) will save automatically or ask user for path where appropriate. But you always should check IsObj($var) before calling any methods on it. Share this post Link to post Share on other sites
wchan2122 0 Posted July 29, 2010 Dear Doudou,It can workMillion thanks!!!! Share this post Link to post Share on other sites