wchan2122 Posted July 28, 2010 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
doudou Posted July 28, 2010 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: Spoiler DDEML.au3 - DDE Client + ServerLocalization.au3 - localize your scriptsTLI.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 yesterday Coder's last words before final release: WE APOLOGIZE FOR INCONVENIENCEĀ
wchan2122 Posted July 29, 2010 Author 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.
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