Jump to content

Excel UDF


Recommended Posts

Hello,

 

When I use the Excel UDF , with the snippet below, I want to leave Excel Open and let the user decide what to do with the file.

However after the user closes the excel file, their is still an instance of Excel running in task Manager , not visible to users

How do I code the script so that once the user closes excel it also closes the reference to Excel in the still running script and not have to pause the script so

that it can continue to run

 

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, "D:\review.xlsx")

 

Edited by HighlanderSword
Link to comment
Share on other sites

Here is a modified example I'm using from the help file.

Note: the _Excel_Close($oExcel)

$oExcel = _Excel_Open(True)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "write_spreedsheat", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    Local $oWorkbook = _Excel_BookNew($oExcel)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "write_spreedsheat", "Error creating the new workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
        _Excel_Close($oExcel)
        Exit
    EndIf

Is this what you need?

Edited by Xandy
Link to comment
Share on other sites

#include <Excel.au3>
#include <MsgBoxConstants.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookNew($oExcel)

If (@error) Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: Events Example", "Error opening workbook '_Excel2.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oExcel)
    Exit
EndIf

ObjEvent($oWorkbook, "Workbook_")

While (True)
    Sleep(100)
WEnd

Func Close()
    _Excel_Close($oExcel, False, True)
    exit 0
EndFunc

Volatile Func Workbook_BeforeClose(ByRef $bCancel)
    #forceref $bCancel
    Close()
EndFunc   ;==>Workbook_BeforeClose

How about that?

Sauce

Didn't even know there was a way to capture object events in an autoit script. I've been using Excel and VBA for a few months now lol

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