Fichier introuvable par Visual Basic

roadmender -  
ghuysmans99 Messages postés 2496 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,
J'ai ce code en VB :

Private Sub Command1_Click()

Dim FSO As Variant
Dim LeFichier As Variant
Dim CheminNomFichier
Dim MeTbl As Variant
Dim PourTbl As Variant
Dim T As Variant

Set FSO = CreateObject("Scripting.FileSystemObject")
CheminNomFichier = "C:\toto.txt"
MsgBox CheminNomFichier
Set LeFichier = FSO.OpenTextFile(CheminNomFichier, 1)
PourTbl = LeFichier.ReadAll
LeFichier.Close

'PourTbl contient: "un;deux;trois;quatres;cinqiéme et dernier;"
MeTbl = Split(PourTbl, ";")
For T = 0 To UBound(MeTbl) - 1
MsgBox "la variable MeTbl indice(" & T & ") contient: " & MeTbl(T)
Next

End Sub

J ai sans cesse l'erreur fichier introuvable ... Le fichier existe bien je ne comprends pas avez vous une idée SVP.

1 réponse

  1. ghuysmans99 Messages postés 2496 Date d'inscription   Statut Contributeur Dernière intervention   342
     
    Il n'est pas interdit de coder correctement :
    Private Sub Command1_Click()
        Dim FSO As Object, LeFichier As Object
        Dim CheminNomFichier As String
        Dim MeTbl() As String, PourTbl As String
        Dim T As Long
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        CheminNomFichier = "C:\toto.txt"
        
        On Error Resume Next
        Set LeFichier = FSO.OpenTextFile(CheminNomFichier, 1)
        If Err Then
            MsgBox "Impossible d'ouvrir le fichier :" & vbCrLf & Err.Number & " -- " & Err.Description, vbExclamation, "Erreur"
            Exit Sub
        End If
        On Error GoTo 0
        PourTbl = LeFichier.ReadAll
        LeFichier.Close
        
        MeTbl = Split(PourTbl, ";")
        For T = 0 To UBound(MeTbl) - 1
            MsgBox "MeTbl(" & T & ")=" & MeTbl(T)
        Next T
    End Sub

    Le message d'erreur provient probablement du fait que tu n'as pas les droits sur le fichier.
    0