Bonjour Amandine,
Voila, j'ai coder un petit exemple :
1 textbox avec le nom du fichier
1 textbox contenant le résultat d'une lecture
1 textbox contenant le résultat d'une écriture
4 boutons
-Ouverture
-Lecture
-Ecriture
-Ferneture
' Déclaration des variables globales des objets fichiers
Dim oFS As Object
Dim oLecteur As Object
' Déclaration des variables globales du classeur EXCEL
'
Dim xlApp, xlBook, xlChart, xlRange As Object 'Objets classeur
Dim xlWhs, iRows, iCols, iRotate As Object 'Objets feuille
'
' Déclaration des variables du fichier
Dim Fichier As String
Dim Disque As String
'
Private Sub cmd_Ecrire_Click()
xlRange.Cells(1, 1).Value = tbx_Ecriture.Text
End Sub
'
Private Sub cmd_FermerExcel_Click()
xlApp.Visible = True
xlRange.Cells(1, 1).Select
xlApp.DisplayAlerts = False
xlBook.SaveAs Fichier
xlApp.Quit
xlApp.DisplayAlerts = True
Set xlRange = Nothing
Set xlChart = Nothing
Set xlWKS = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
MsgBox "Fichier fermé"
End Sub
Private Sub cmd_Lire_Click()
tbx_Lecture.Text = xlRange.Cells(1, 1).Value
End Sub
'
Private Sub cmd_OuvrirExcel_Click()
'Lecture du nom du fichier
Fichier = tbx_NomCompletFichier.Text
' Capture de la lettre du lecteur à écrire
Disque = Mid(Fichier, 1, 2)
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oLecteur = oFS.GetDrive(Disque)
If (oLecteur.IsReady) Then
' Création de l'objet Excel (une classe)
Set oFS = CreateObject("Scripting.FileSystemObject")
Set xlApp = CreateObject("Excel.Application")
'Vérification de la présence du classeur
If (FichierExistant(Fichier) = True) Then
' Ouverture du classeur
Set xlBook = xlApp.Workbooks.Open(Fichier)
Else
' Création du classeur
xlApp.SheetsInNewWorkbook = 1
Set xlBook = xlApp.Workbooks.Add
End If
' Positionnement à l'intérieur du classeur
Set xlWKS = xlBook.Worksheets(1)
Set xlRange = xlWKS.Range("A1:A65535")
MsgBox "Fichier Ouvert"
End If
End Sub
'
Private Sub cmd_Quitter_Click()
Unload Me
End Sub
'
Function FichierExistant(NomFichier) As Boolean
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
FichierExistant = fso.FileExists(NomFichier)
Set fso = Nothing
End Function
Lupin
~L'essentiel est invisible pour les yeux~
~On ne voit bien qu'avec le coeur~