seadoggie01 Posted April 7, 2020 Share Posted April 7, 2020 (edited) I was attempting to register a function that would close an Excel workbook when my program exited as I'm in the middle of creating/testing a program. Since parts of my code will simply call Exit, I thought it would be a good idea to use OnAutoItExitRegister to call a function to close Excel whenever it exits. So I tried something like this... Main() Func Main() ; <-- More code --> ; Register the App and workbook to be closed later CloseExcel($oExcel, $oBook) ; Register the funtion to run before closing OnAutoItExitRegister("CloseExcel") ; <-- More code --> Exit EndFunc Func CloseExcel($oApp = Default, $oWorkbook = Default) Static $oExcel, $oBook If $oApp <> Default Then ; Save the variables for later $oExcel = $oApp $oBook = $oWorkbook Else ; Close the workbook and app without saving _Excel_BookClose($oBook, False) _Excel_Close($oExcel, False) EndIf EndFunc The first weird thing was that I got an AU3Check error saying that the function definition of CloseExcel was the first line calling it. Whatever, after flipping the two lines in Main(), it stopped giving me the error. The second thing that happened was that when I ran it and it got to Exit, I got an error inside of CloseExcel stating that $oApp hadn't been declared. That doesn't make sense to me since it was declared as a parameter. Am I abusing OnAutoItExitRegister in this way? Edit: For the time being, I solved the problem using @NumParams like this: Func CloseExcel($oApp = Default, $oWorkbook = Default) Static $oExcel, $oBook If @NumParams > 0 Then ; Save the variables for later $oExcel = $oApp $oBook = $oWorkbook Else ; Close the workbook and app without saving _Excel_BookClose($oBook, False) _Excel_Close($oExcel, False) EndIf EndFunc Edited April 7, 2020 by seadoggie01 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
Nine Posted April 7, 2020 Share Posted April 7, 2020 27 minutes ago, seadoggie01 said: That doesn't make sense to me since it was declared as a parameter Parameters on OnAutoItExitRegister function are totally ignored. So it is exactly what is happening, they simply don't exist. You could also use IsDeclared function in place of @NumParams. Not sure why you declare your variables Static inside that function. I do not believe it is useful. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
seadoggie01 Posted April 7, 2020 Author Share Posted April 7, 2020 Ah, I guess that makes sense. I just would've thought they would still be declared and have default values. I register them as static because I wouldn't have a reference to the Excel objects without them being static or global. That way I ensure that they actually get closed (I have Excel set to open hidden) before running my program again, which re-downloads the files. All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
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