#include <Excel.au3>
#include <MsgBoxConstants.au3>

; COM Errorhandler
Global $oErrorHandler = ObjEvent("AutoIt.Error", "_COMErrFunc")

; Erstellt ein Excel Anwendungs-Objekt oder verbindet zu einer bereits Instanz
Global $oExcel = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_Open ", _
		"Fehler beim Erstellen des Excel Anwendungs-Objekt." & @CRLF & _
		"@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_Open ", "Excel Anwendung wurde erfolgreich geöffnet.")
; Öffnet ein existierendes workbook und gibt den Objectidentifier zurück :
Local $sWorkbook = "C:\DHL-Versand\Versand.xlsm"
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook)
If @error Then
	MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookOpen ", _
			"Fehler beim Öffnen des Workbooks : " & @CRLF & _
			"Dateiname : " & $sWorkbook & @CRLF & _
			"@error = " & @error & ", @extended = " & @extended)
	_Excel_Close($oExcel)
	Exit
EndIf
$oWorkbook.Parent.Run("Versand")     ; Name des Makros
#cs
Sleep(2000)
; Schließt das Excel Anwendungs-Objekt
_Excel_Close($oExcel)
If @error Then
	MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_Close ", _
			"Fehler beim Schließen des Excel Anwendungs-Objekt." & @CRLF & _
			"@error = " & @error & ", @extended = " & @extended)
Else
	MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_Open ", _
			"Excel Anwendung wurde erfolgreich geschlossen." & @CRLF)
EndIf
#ce

Func _COMErrFunc($oError)
    MsgBox(16, "COM Error", @ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & $oError.description & @CRLF & _
            @TAB & "err.source is: " & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & "0x" & Hex($oError.retcode))
EndFunc   ;==>_ErrFunc