Jump to content

OnAutoItExitRegister Functions


Recommended Posts

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 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 functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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 functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
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
 Share

  • Recently Browsing   0 members

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