Code : Tout sélectionner
; #FUNCTION# ====================================================================================================================
; Name...........: _ExcelBookOpenUpdate
; Description ...: Opens an existing workbook and returns its object identifier.
; Syntax.........: $oExcel = _ExcelBookOpen($sFilePath, $fVisible = 1, $fReadOnly = False, $sPassword = "", $sWritePassword = "")
; Parameters ....: $sFilePath - Path and filename of the file to be opened
; $fVisible - Flag, whether to show or hide the workbook (0=not visible, 1=visible) (default=1)
; $fReadOnly - Flag, whether to open the workbook as read-only (True or False) (default=False)
; $sPassword - The password that was used to read-protect the workbook, if any (default is none)
; $sWritePassword - The password that was used to write-protect the workbook, if any (default is none)
; $fUpdatelinks - Flag, whether to update or not the links (0=not update, 1=update) (default= Ask the User)
; Return values .: Success - Returns new object identifier
; Failure - Returns 0 and sets @error on errors:
; @error=1 - Unable to create the object
; @error=2 - File does not exist
; Author ........: SEO <locodarwin at yahoo dot com>
; Modified.......:
; Remarks .......: None
; Related .......:
; Link ..........;
; Example .......; Yes
; ===============================================================================================================================
Func _ExcelBookOpenUpdate($sFilePath, $fVisible = 1, $fUpdatelinks = 0, $fReadOnly = False, $sPassword = "", $sWritePassword = "")
Local $oExcel = ObjCreate("Excel.Application")
If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
If Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
If $fVisible > 1 Then $fVisible = 1
If $fVisible < 0 Then $fVisible = 0
If $fReadOnly > 1 Then $fReadOnly = 1
If $fReadOnly < 0 Then $fReadOnly = 0
With $oExcel
.Visible = $fVisible
If $sPassword <> "" And $sWritePassword <> "" Then .WorkBooks.Open($sFilePath, $fUpdatelinks, $fReadOnly, Default, $sPassword, $sWritePassword)
If $sPassword = "" And $sWritePassword <> "" Then .WorkBooks.Open($sFilePath, $fUpdatelinks, $fReadOnly, Default, Default, $sWritePassword)
If $sPassword <> "" And $sWritePassword = "" Then .WorkBooks.Open($sFilePath, $fUpdatelinks, $fReadOnly, Default, $sPassword, Default)
If $sPassword = "" And $sWritePassword = "" Then .WorkBooks.Open($sFilePath, $fUpdatelinks, $fReadOnly)
.ActiveWorkbook.Sheets(1).Select ()
EndWith
Return $oExcel
EndFunc ;==>_ExcelBookOpenUpdate