tester l'existence d'un fichier

  • Initiateur de la discussion Initiateur de la discussion chewi
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

chewi

XLDnaute Junior
Bonjour à tout le forum,

Je crée un fichier, via un bouton de commande d'un UserForm, portant le nom de ma ComboBox.
Problème: dans le cas où le classeur existe déjà, je souhaiterais qu' Excel ne m'affiche pas de message " Ce fichier existe déjà, désirez-vous le remplacer" mais affiche une MsgBox que je crée moi-même "Ce fichier existe déjà, voulez-vous l'ouvrir?" oui - non
oui: l'ouvrir
non: revenir à ma ComboBox de départ

Quelque chose du genre:

Code:
If "C:\Documents and Settings\moi\Desktop\" & ComboBox1 exist Then
Open "C:\Documents and Settings\moi\Desktop\" & ComboBox1
Else Set wk = Workbooks.Add
      wk.SaveAs ...
      ...
End If

Il faut en gros tester si le fichier existe ou pas.
S'il existe, il se trouve obligatoirement sur mon Desktop.

Mais je ne sais pas quel code dois-je écrire pour tester l'existence de mon fichier?
Il faut apparement utiliser la fonction dir mais je ne sais comment l'utiliser

Merci à vous

Chewi
 
Re : tester l'existence d'un fichier

Bonjour,

Voici un exemple qui pourrait t'aider...

Code:
'Recherche du fichier
Nom = Dir(C:\Documents and Settings\moi\Desktop\" & ComboBox1 )

If Nom<>"" Then
    ' Ton code si le fichier existe
else
    ' Si le fichier n'existe pas
End if

En espérant répondre à ta question
 
Re : tester l'existence d'un fichier

Bonjour Chewi, le forum,

Voici un bout de code :

Code:
[COLOR=blue][FONT=Courier]Sub[/FONT][/COLOR][FONT=Courier] Test_Fichier()[/FONT]
[FONT=Courier]    [COLOR=blue]Dim[/COLOR] Fichier [COLOR=blue]As String[/COLOR][/FONT]
[FONT=Courier][COLOR=blue]Dim reponse as boolean[/COLOR][/FONT]
[FONT=Courier]    Fichier = Dir(C:\Documents and Settings\moi\Desktop\" & ComboBox1)[/FONT]
[FONT=Courier]    [COLOR=blue]If[/COLOR] Fichier <> "" [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier][COLOR=blue]Reponse = msgbox("[FONT=Verdana][COLOR=#000000]Ce fichier existe déjà, voulez-vous l'ouvrir?" [/COLOR][/FONT], vbYesNo)
If Reponse = vbNo Then
Exit Sub
End If
Reponse = vbYes[/COLOR][/FONT]
[FONT=Courier][COLOR=blue]Fichier.open[/COLOR][/FONT]
[FONT=Courier]        [COLOR=#006633]'le fichier existe[/COLOR][/FONT]
[FONT=Courier]    [COLOR=blue]Else[/COLOR][/FONT]
[FONT=Courier][COLOR=blue]exit sub[/COLOR][/FONT]
[FONT=Courier]        [COLOR=#006633]'le fichier n'existe pas[/COLOR][/FONT]
[FONT=Courier]    [COLOR=blue]End If[/COLOR][/FONT]
[COLOR=blue][FONT=Courier]End Sub[/FONT][/COLOR]
[COLOR=blue][FONT=Courier][/FONT][/COLOR]

C'est à tester...
 
Re : tester l'existence d'un fichier

Re Simons et Sly le globe trotter,

Petit problème à la ligne Fichier.Open : "Qualificateur incorrect"

Code:
Private Sub Valider_Click()
Dim wk As Workbook
    If ComboBox1 <> "" Then
        Dim Fichier As String
        Dim reponse As Boolean
        Fichier = Dir("C:\Documents and Settings\moi\Desktop\" & ComboBox1)
        If Fichier <> "" Then
        reponse = MsgBox("Ce fichier existe déjà, voulez-vous l'ouvrir?", vbYesNo)
        If reponse = vbNo Then
        Exit Sub
        End If
        reponse = vbYes
        Fichier.Open
        'le fichier existe
        Else
        'le fichier n'existe pas
            Set wk = Workbooks.Add
            wk.SaveAs "C:\Documents and Settings\moi\Desktop\" & ComboBox1
            Unload Uf1
            Load Uf2
            Uf2.Show
        End If
    Else
            MsgBox "Vous n'avez pas entré de Lot"
    End If
End Sub

Merci, Chewi
 
Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
3
Affichages
1 K
Compte Supprimé 979
C
Retour