Piloter Excel en Lotuscript

par Daniel CORDIER Email

Voici mon premier article concernant un code LotuScript que j'utilise depuis quelques temps.
c'est un ensemble de fonctions qui vous permettra de manipuler un fichier EXCEL (excel doit être installé sur le poste utilisateur).

Suite:

variables globales (généralement des 'Déclarations') :

Public XL As Variant
Public XLfic As Variant
Public XLfeuille As Variant

Fonctions disponibles :

Ouvrir un fichier excel :

Function OuvreExcel3(FichierExcel As String,Feuille As String,IsVisible As Integer ) As Integer

On Error Goto ErrOuvreExcel3

OuvreExcel3 = True
Set XL = Nothing
Set XL = CreateObject("Excel.Application")
XL.visible = True
Set XLfic = XL.Workbooks.Open(FichierExcel,,True)
If Feuille = "" Then
Set XLfeuille = XLfic.Worksheets(1)
Else
Set XLfeuille = XLfic.Worksheets(Feuille)
End If
If XLfeuille Is Nothing Then
Msgbox "Erreur ouverture de la feuille " & feuille
OuvreExcel3 = False
End If
XLfeuille.Select
XL.visible = IsVisible
Exit Function

ErrOuvreExcel3:
OuvreExcel3 = False
Resume Next
End Function

Fermer le fichier Excel courant

Public Sub FermeExcel2(NePasSauver As Integer )
On Error Goto ErrFermeExcel2

If XL Is Nothing Then
Exit Sub
End If
If NePasSauver = True Then
XLfic.Close False
'XL.ActiveWorkBook.saved = True
'XL.DisplayAlerts = False
End If
XL.Quit
Set XLfeuille = Nothing
Set XLfic = Nothing
Set XL = Nothing
Exit Sub

ErrFermeExcel2:
Msgbox Cstr(Err) & " - " & Error$
Resume Next
End Sub

Changer la feuille courante du fichier Excel ouvert

Public Sub ChangeFeuilleExcel(Feuille As String)
On Error Goto ErrChangeFeuilleExcel

' si la feuille n'est pas renseigné on prend la première
If Trim(Feuille) = "" Then
Set XLfeuille = XLfic.Worksheets(1)
Else
Set XLfeuille = XLfic.Worksheets(Feuille)
End If
If Not (XLfeuille Is Nothing) Then
XLfeuille.activate
End If
Exit Sub

ErrChangeFeuilleExcel:
Msgbox "ERREUR : " & Error$,,"Erreur"
Resume Next
End Sub

Sauve le fichier Excel

Public Sub SaveExcel(FichierExcel As String)
On Error Goto ErrSaveExcel

XLfeuille.saveas FichierExcel
Exit Sub

ErrSaveExcel:
Msgbox Cstr(Err) & " - " & Error$
Resume Next
End Sub

Se déplacer vers une cellule nommée

Function GotoAdresse(Nom As String) As Integer
Dim ObjxZone As Variant
On Error Goto errGotoAdresse

Set ObjxZone = Nothing
Set ObjxZone = XL.Range(Nom)

If Not (ObjxZone Is Nothing) Then
ObjxZone.Select
GotoAdresse = True
Else
GotoAdresse = False
End If

Exit Function

errGotoAdresse:
GotoAdresse = False
Resume Next
End Function

Lit une cellule et la convertie en texte

Function LitCelluleTXT(Ligne As Integer, Colonne As Integer) As String
On Error Goto ErrLitCelluleTXT

LitCelluleTXT = Cstr(XLfeuille.Cells(Ligne, Colonne).text)
Exit Function

ErrLitCelluleTXT:
LitCelluleTXT = ""
Resume Next
End Function

Place une valeur texte dans une cellule

Function EcritCelluleTXT(Ligne As Integer, Colonne As Integer, Valeur As String ) As Integer
Dim ObjxZone As Variant
On Error Goto ErrEcritCelluleTXT

If Ligne < 1 Or Colonne < 1 Then
EcritCelluleTXT = False
Exit Function
End If
Set ObjxZone = xl.Range(Chr(Asc("A") + Colonne - 1) & Cstr(Ligne))

ObjxZone.Value = Valeur
EcritCelluleTXT = True

ErrEcritCelluleTXT:
EcritCelluleTXT = False
Msgbox Cstr(Err) & " - " & Error$
Resume Next

End Function

Voila pour ce premier article développeur, en espérant que cela vous aidera dans vos programmes.