Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Access > IHM
IHM Ce forum est dédié aux questions relatives à la création de formulaires et d'états, avec ou sans code VBA, et macros.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 17/08/2011, 07h05   #1
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
Par défaut test de l'existence d'un fichier

Bonjour,
j'ai un bouton commande qui permet d'ouvrir un document type puis le remplir avec des données et le sauvegarder sous un autre nom, mais voila parfois le fichier existe et le remplace directement,
voici le code:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Private Sub cmdPublipostage_Click()
 
    'En cas d'erreur, on n'en tient pas compte
    On Error Resume Next
 
    'Déclaration d'une instance Word
    Dim W_App As New Word.Application
 
    'Instance Word
    With W_App
 
        'Word est visible à l'écran ou non
        .Visible = True
 
         'Ouvre le document Word avec les signets
        .Documents.Open (CurrentProject.Path & "\dossier type.doc")
 
 
        'Tranfert du nom : strNom ==> Signet Nom
        .ActiveDocument.Bookmarks("Nom").Select
        .Selection.Text = Me.Nom
 
         'Tranfert du nom : strPrenom ==> Signet Prenom
        .ActiveDocument.Bookmarks("Prenom").Select
        .Selection.Text = Me.Prenom
 
         'Tranfert du nom : strDNaiss ==> Signet DNaiss
        .ActiveDocument.Bookmarks("DNaissance").Select
        .Selection.Text = Me.DNaiss
 
         'Tranfert du nom : strrue ==> Signet rue
         .ActiveDocument.Bookmarks("rue").Select
        .Selection.Text = Me.Rue
 
         'Tranfert du nom : strCP ==> Signet CP
        .ActiveDocument.Bookmarks("CP").Select
        .Selection.Text = Me.CP
 
         'Tranfert du nom : strVille ==> Signet Ville
        .ActiveDocument.Bookmarks("Ville").Select
        .Selection.Text = Me.Ville
 
         'Tranfert du nom : strTel ==> Signet Tel
        .ActiveDocument.Bookmarks("Tel").Select
        .Selection.Text = Me.Tel
 
         'Sauvegarde du nouveau document
        .ActiveDocument.SaveAs (CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc")
 
 
 
 
    End With
 
 
End Sub
quel code faut-il rajouter pour faire le teste de l'existance de fichier.
merci
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 07h10   #2
Rédacteur/Modérateur
 
Avatar de GAYOT
 
Homme Jean-Damien GAYOT
Inscription : novembre 2004
Messages : 2 074
Détails du profil
Informations personnelles :
Nom : Homme Jean-Damien GAYOT
Âge : 56
Localisation : France, Meuse (Lorraine)

Informations professionnelles :
Secteur : Distribution

Informations forums :
Inscription : novembre 2004
Messages : 2 074
Points : 4 381
Points : 4 381
Envoyer un message via Skype™ à GAYOT
Bonjour
Tu peux surement trouver des éléments de réponse dans la faq. Par exemple ici:
http://access.developpez.com/faq/?pa...eexistFunction
__________________
Plus j'avance et plus j'ai l'impression de ne rien savoir. Et comme j'essaie d'aller loin..!!.

Tutoriels sur:http://jdgayot.developpez.com

Pas de sujets techniques par Mp. Sinon
GAYOT est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 17/08/2011, 08h01   #3
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
Re
je l'ai déja consulté, comme je debute en programmation je ne sais pas laquel choisir entre les deux,
si je choisis la premiere je dois l'inserer avant comme ci dessous?:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Private Sub cmdPublipostage_Click()
 
Function existeFileFSO(ByVal fichier As String) As Boolean
Set fs = CreateObject("Scripting.FileSystemObject")
existeFileFSO = fs.FileExists(fichier)
Set fs = Nothing
End Function
 
 
 
    'En cas d'erreur, on n'en tient pas compte
    On Error Resume Next
 
    'Déclaration d'une instance Word
    Dim W_App As New Word.Application
 
    'Instance Word
    With W_App
 
        'Word est visible à l'écran ou non
        .Visible = True
 
         'Ouvre le document Word avec les signets
        .Documents.Open (CurrentProject.Path & "\dossier type.doc")
 
 
        'Tranfert du nom : strNom ==> Signet Nom
        .ActiveDocument.Bookmarks("Nom").Select
        .Selection.Text = Me.Nom
 
         'Tranfert du nom : strPrenom ==> Signet Prenom
        .ActiveDocument.Bookmarks("Prenom").Select
        .Selection.Text = Me.Prenom
 
         'Tranfert du nom : strDNaiss ==> Signet DNaiss
        .ActiveDocument.Bookmarks("DNaissance").Select
        .Selection.Text = Me.DNaiss
 
         'Tranfert du nom : strrue ==> Signet rue
         .ActiveDocument.Bookmarks("rue").Select
        .Selection.Text = Me.Rue
 
         'Tranfert du nom : strCP ==> Signet CP
        .ActiveDocument.Bookmarks("CP").Select
        .Selection.Text = Me.CP
 
         'Tranfert du nom : strVille ==> Signet Ville
        .ActiveDocument.Bookmarks("Ville").Select
        .Selection.Text = Me.Ville
 
         'Tranfert du nom : strTel ==> Signet Tel
        .ActiveDocument.Bookmarks("Tel").Select
        .Selection.Text = Me.Tel
 
         'Sauvegarde du nouveau document
        .ActiveDocument.SaveAs (CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc")
 
 
 
 
    End With
merci
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 11h31   #4
Membre confirmé
 
Avatar de lelensois16
 
Homme Romain M.
Étudiant
Inscription : avril 2009
Messages : 167
Détails du profil
Informations personnelles :
Nom : Homme Romain M.
Localisation : France, Nord (Nord Pas de Calais)

Informations professionnelles :
Activité : Étudiant
Secteur : Conseil

Informations forums :
Inscription : avril 2009
Messages : 167
Points : 204
Points : 204
Bonjour,

Voici quelques notions de base en algorithme :

- Il ne faut pas mélanger fonction (Function) et procédure (Sub). Une fonction renvoie un résultat , une procédure établit une "action" sans pour autant renvoyer de résultats

-En VBA , il faudrait placer les procédures dans un module

- la fonction existeFileFSO renvoie 2 résultats : Vrai / Faux en fonction du nom du fichier.
Par exemple existeFileFSO("NomFichier") renvoie Vrai si le fichier Existe déja ( en supposant que le chemin du fichier est correct )

- A partir de ces infos , je te laisse le soin de faire une petite structure conditionnelle utilisant cette fonction.

bon courage !
lelensois16 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 11h59   #5
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
ok Merci, j'essaye et je te repond

A+
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 13h33   #6
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
Re,
voila ce que j'ai deduit!! merci de me le corriger

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Private Sub cmdPublipostage_Click()
 
     'En cas d'erreur, on n'en tient pas compte
    On Error Resume Next
 
if existeFileFSO(CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc")
 
end sub
 
   'Déclaration d'une instance Word
    Dim W_App As New Word.Application
 
    'Instance Word
    With W_App
 
        'Word est visible à l'écran ou non
        .Visible = True
 
         'Ouvre le document Word avec les signets
        .Documents.Open (CurrentProject.Path & "\dossier type.doc")
 
 
        'Tranfert du nom : strNom ==> Signet Nom
        .ActiveDocument.Bookmarks("Nom").Select
        .Selection.Text = Me.Nom
 
         'Tranfert du nom : strPrenom ==> Signet Prenom
        .ActiveDocument.Bookmarks("Prenom").Select
        .Selection.Text = Me.Prenom
 
         'Tranfert du nom : strDNaiss ==> Signet DNaiss
        .ActiveDocument.Bookmarks("DNaissance").Select
        .Selection.Text = Me.DNaiss
 
         'Tranfert du nom : strrue ==> Signet rue
         .ActiveDocument.Bookmarks("rue").Select
        .Selection.Text = Me.Rue
 
         'Tranfert du nom : strCP ==> Signet CP
        .ActiveDocument.Bookmarks("CP").Select
        .Selection.Text = Me.CP
 
         'Tranfert du nom : strVille ==> Signet Ville
        .ActiveDocument.Bookmarks("Ville").Select
        .Selection.Text = Me.Ville
 
         'Tranfert du nom : strTel ==> Signet Tel
        .ActiveDocument.Bookmarks("Tel").Select
        .Selection.Text = Me.Tel
 
         'Sauvegarde du nouveau document
        .ActiveDocument.SaveAs (CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc")
 
 
 
 
    End With
end if
end sub
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 16h19   #7
Rédacteur/Modérateur
 
Avatar de User
 
Homme Denis
Développeur informatique
Inscription : août 2004
Messages : 3 206
Détails du profil
Informations personnelles :
Nom : Homme Denis
Âge : 42
Localisation : France

Informations professionnelles :
Activité : Développeur informatique

Informations forums :
Inscription : août 2004
Messages : 3 206
Points : 5 258
Points : 5 258
Salut,

Il faut enlever le
Code :
1
2
 
On Error resume next
Il t'empêche de debugger

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Private Sub cmdPublipostage_Click()
 
     'En cas d'erreur, on n'en tient pas compte
    ' On Error Resume Next
 
if Not existeFileFSO(CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc") then
 
 
   'Déclaration d'une instance Word
    Dim W_App As New Word.Application
 
    'Instance Word
    With W_App
 
        'Word est visible à l'écran ou non
        .Visible = True
 
         'Ouvre le document Word avec les signets
        .Documents.Open (CurrentProject.Path & "\dossier type.doc")
 
 
        'Tranfert du nom : strNom ==> Signet Nom
        .ActiveDocument.Bookmarks("Nom").Select
        .Selection.Text = Me.Nom
 
         'Tranfert du nom : strPrenom ==> Signet Prenom
        .ActiveDocument.Bookmarks("Prenom").Select
        .Selection.Text = Me.Prenom
 
         'Tranfert du nom : strDNaiss ==> Signet DNaiss
        .ActiveDocument.Bookmarks("DNaissance").Select
        .Selection.Text = Me.DNaiss
 
         'Tranfert du nom : strrue ==> Signet rue
         .ActiveDocument.Bookmarks("rue").Select
        .Selection.Text = Me.Rue
 
         'Tranfert du nom : strCP ==> Signet CP
        .ActiveDocument.Bookmarks("CP").Select
        .Selection.Text = Me.CP
 
         'Tranfert du nom : strVille ==> Signet Ville
        .ActiveDocument.Bookmarks("Ville").Select
        .Selection.Text = Me.Ville
 
         'Tranfert du nom : strTel ==> Signet Tel
        .ActiveDocument.Bookmarks("Tel").Select
        .Selection.Text = Me.Tel
 
         'Sauvegarde du nouveau document
        .ActiveDocument.SaveAs (CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc")
 
    End With
 
end if
 
end sub
A+
__________________
Merci de ne pas poster sur mon profil pour des problèmes techniques. Pour celà vous pouvez utiliser le forum ou m'envoyer un mp.

Bon développement !


Mes tutoriels et contributions sur ma page perso:
Ma page personnelle
User est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 18h24   #8
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
oh merci Denis tu es toujours la pour mon secours.

es ce que je dois inserer ce code qque part, car il ne reconnait pas la fonction il m'affiche "erreur de compilation Sub ou Function non definie"
Code :
1
2
3
4
5
Function existeFileFSO(ByVal fichier As String) As Boolean
Set fs = CreateObject("Scripting.FileSystemObject")
existeFileFSO = fs.FileExists(fichier)
Set fs = Nothing
End Function
A+
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 18h35   #9
Rédacteur/Modérateur
 
Avatar de User
 
Homme Denis
Développeur informatique
Inscription : août 2004
Messages : 3 206
Détails du profil
Informations personnelles :
Nom : Homme Denis
Âge : 42
Localisation : France

Informations professionnelles :
Activité : Développeur informatique

Informations forums :
Inscription : août 2004
Messages : 3 206
Points : 5 258
Points : 5 258
Salut,

Il faut mettre le code de la fonction dans 1 module indépendant par exemple :

Code :
1
2
3
4
5
Function existeFileFSO(ByVal fichier As String) As Boolean
Set fs = CreateObject("Scripting.FileSystemObject")
existeFileFSO = fs.FileExists(fichier)
Set fs = Nothing
End Function
A+
__________________
Merci de ne pas poster sur mon profil pour des problèmes techniques. Pour celà vous pouvez utiliser le forum ou m'envoyer un mp.

Bon développement !


Mes tutoriels et contributions sur ma page perso:
Ma page personnelle
User est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 19h05   #10
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
il m'affiche "erreur de compilation variable non definie" et il selectionne "fs =" en Bleu

merci.
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 19h19   #11
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
oups,
je n'ai bien respecter les consignes!!, j' ai changé , je l'ai copié dans un module vide et ça marche!!
j'ai rajouté un message dans le cas ou le dossier existe, voici le code:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Private Sub cmdPublipostage_Click()
 
     'En cas d'erreur, on n'en tient pas compte
    ' On Error Resume Next
 
if Not existeFileFSO(CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc") then
 
 
   'Déclaration d'une instance Word
    Dim W_App As New Word.Application
 
    'Instance Word
    With W_App
 
        'Word est visible à l'écran ou non
        .Visible = True
 
         'Ouvre le document Word avec les signets
        .Documents.Open (CurrentProject.Path & "\dossier type.doc")
 
 
        'Tranfert du nom : strNom ==> Signet Nom
        .ActiveDocument.Bookmarks("Nom").Select
        .Selection.Text = Me.Nom
 
         'Tranfert du nom : strPrenom ==> Signet Prenom
        .ActiveDocument.Bookmarks("Prenom").Select
        .Selection.Text = Me.Prenom
 
         'Tranfert du nom : strDNaiss ==> Signet DNaiss
        .ActiveDocument.Bookmarks("DNaissance").Select
        .Selection.Text = Me.DNaiss
 
         'Tranfert du nom : strrue ==> Signet rue
         .ActiveDocument.Bookmarks("rue").Select
        .Selection.Text = Me.Rue
 
         'Tranfert du nom : strCP ==> Signet CP
        .ActiveDocument.Bookmarks("CP").Select
        .Selection.Text = Me.CP
 
         'Tranfert du nom : strVille ==> Signet Ville
        .ActiveDocument.Bookmarks("Ville").Select
        .Selection.Text = Me.Ville
 
         'Tranfert du nom : strTel ==> Signet Tel
        .ActiveDocument.Bookmarks("Tel").Select
        .Selection.Text = Me.Tel
 
         'Sauvegarde du nouveau document
        .ActiveDocument.SaveAs (CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc")
 
    End With
 MsgBox ("Le dossier existe déja")
 
end if
 
end sub

tu penses qu'il est au bon endroit

merci Denis
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/08/2011, 20h00   #12
Rédacteur/Modérateur
 
Avatar de User
 
Homme Denis
Développeur informatique
Inscription : août 2004
Messages : 3 206
Détails du profil
Informations personnelles :
Nom : Homme Denis
Âge : 42
Localisation : France

Informations professionnelles :
Activité : Développeur informatique

Informations forums :
Inscription : août 2004
Messages : 3 206
Points : 5 258
Points : 5 258
La structure conditionnelle en VBA est du style :

Code :
1
2
3
4
5
if condition1 then
 
else ' sinon
 
end if
Ce qui te fait :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
Private Sub cmdPublipostage_Click()
 
     'En cas d'erreur, on n'en tient pas compte
    ' On Error Resume Next
 
if Not existeFileFSO(CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc") then
 
 
   'Déclaration d'une instance Word
    Dim W_App As New Word.Application
 
    'Instance Word
    With W_App
 
        'Word est visible à l'écran ou non
        .Visible = True
 
         'Ouvre le document Word avec les signets
        .Documents.Open (CurrentProject.Path & "\dossier type.doc")
 
 
        'Tranfert du nom : strNom ==> Signet Nom
        .ActiveDocument.Bookmarks("Nom").Select
        .Selection.Text = Me.Nom
 
         'Tranfert du nom : strPrenom ==> Signet Prenom
        .ActiveDocument.Bookmarks("Prenom").Select
        .Selection.Text = Me.Prenom
 
         'Tranfert du nom : strDNaiss ==> Signet DNaiss
        .ActiveDocument.Bookmarks("DNaissance").Select
        .Selection.Text = Me.DNaiss
 
         'Tranfert du nom : strrue ==> Signet rue
         .ActiveDocument.Bookmarks("rue").Select
        .Selection.Text = Me.Rue
 
         'Tranfert du nom : strCP ==> Signet CP
        .ActiveDocument.Bookmarks("CP").Select
        .Selection.Text = Me.CP
 
         'Tranfert du nom : strVille ==> Signet Ville
        .ActiveDocument.Bookmarks("Ville").Select
        .Selection.Text = Me.Ville
 
         'Tranfert du nom : strTel ==> Signet Tel
        .ActiveDocument.Bookmarks("Tel").Select
        .Selection.Text = Me.Tel
 
         'Sauvegarde du nouveau document
        .ActiveDocument.SaveAs (CurrentProject.Path & "\" & Me!Nom & " " & Me!Prenom & " " & Format(Me.DNaiss, "dd mm yy") & ".doc")
 
    End With
 
else
 
 MsgBox ("Le dossier existe déja")
 
end if
 
end sub
A+
__________________
Merci de ne pas poster sur mon profil pour des problèmes techniques. Pour celà vous pouvez utiliser le forum ou m'envoyer un mp.

Bon développement !


Mes tutoriels et contributions sur ma page perso:
Ma page personnelle
User est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/08/2011, 08h37   #13
Candidat au titre de Membre du Club
 
Inscription : décembre 2010
Messages : 79
Détails du profil
Informations forums :
Inscription : décembre 2010
Messages : 79
Points : 12
Points : 12
Bonjour,

Merci a tous et surtout a Denis, ça fonctionne.
A+
lounacou est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 06h48.


 
 
 
 
Partenaires

Hébergement Web