[Catégorie modifiée VB6 -> VBA] VBA 6.0 Probléme dans le code

Jaimelespates Messages postés 1 Date d'inscription mardi 8 février 2011 Statut Membre Dernière intervention 8 février 2011 - 8 févr. 2011 à 17:55
cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 - 8 févr. 2011 à 20:37
Bonjour,

Avant tout choses: j'aime les pattes.

Ceci étant dit, j'ai un soucis avec mon script sur VBA 6.0.
J'essaye de réaliser une interface avec menu, sous menu et zone de texte.

Mais, j'ai fini mon code et lorsque je le test, "erreur d'exécution 53 fichier introuvable".

Du coup, je sollicite votre aide afin que je puisse utiliser mon code.

Voici les feuilles de scripts, en vous remerciant d'avance:

Feuille 1

Private Declare Sub CoderLettres Lib "menuCodage" ()
Private Declare Function CoderTexte$ Lib "menuCodage" (Mot$)
Private Declare Function DecoderTexte$ Lib "menuCodage" (Mot$)
Private Declare Function CoderLettre$ Lib "menuCodage" (Lettre$)
Private Declare Function DecoderLettre$ Lib "menuCodage" (Lettre$)
Private Declare Sub nouveauDocument Lib "menuFichier" ()
Private Declare Sub enregistrerDocument Lib "menuFichier" ()
Private Declare Sub enregistrerDocumentSous Lib "menuFichier" ()
Private Declare Sub enregistrerDansFichier Lib "menuFichier" (nomFichier$)
Private Declare Sub ouvrirDocument Lib "menuFichier" ()
Private Declare Sub lireFichier Lib "menuFichier" (nomFichier$)
Private Declare Function fichierExiste Lib "menuFichier" (nomFichier$)

Private Sub Form_Load()
documentModifie = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
If documentModifie Then
reponse = MsgBox("Voulez-vous enregistrer les modifications ?", vbYesNo)
If reponse = vbYes Then Call enregistrerDocument
End If
End Sub

Private Sub Form_change()
documentModifie = True
End Sub


Private Sub menuCoderTexte_Click()
letexte = Form1.zoneTexte.Text
Call CoderTexte(letexte)
End Sub

Private Sub menuDecoderTexte_Click()
Call DecoderTexte(Form1.zoneTexte.Text)
End Sub

Private Sub menuEnregistrer_Click()
If documentOuvert = True Then
If documentModifie = True Then
Call enregistrerDocument
End If
End If
End Sub

Private Sub menuEnregistrerSous_Click()
If documentOuvert = True Then
Call enregistrerDocumentSous
End If
End Sub

Private Sub menuFermerDocument_Click()
If documentOuvert = True Then
Call fermerDocument
End If
End Sub

Private Sub menuNouveau_Click()
Call nouveauDocument
End Sub


Private Sub menuOuvrir_Click()
Call ouvrirDocument
End Sub


Feuille 2 (module annexe)

Private CODES(1 To 52) As String * 1
Sub CoderLettres()
'---Générer un nombre aléatoire entier N entre 1 et 10 :
'--- TIMER : fonction qui renvoie le nombre de secondes écoulées depuis 0 heure
' RANDOMIZE : fonction qui initialise le générateur de nombres aléatoires
' RND : génère un nombre aléatoire réel compris entre 0 et1
Randomize Timer
N = (Int(Rnd * 10) Mod 10) + 1
'--- Coder les lettres (décalage d'un nombre aléatoire de positions)
For i = 65 To 90
LETTRES(i - 64) = Chr$(i)
CODES(i - 64) = Chr$(((i + N) Mod 26) + 65)
Next i
End Sub

Function CoderTexte$(Mot$)
N = Len(Mot$)
MotCode$ = ""
For i = 1 To N
Caractere$ = Mid$(Mot$, i, 1)
MotCode$ = MotCode$ + CoderUneLettre$(Caractere$)
Next i
CoderTexte$ = MotCode$
End Function

Function CoderUneLettre$(Lettre$)
For i = 1 To 26
If LETTRES(i) = Lettre$ Then
CoderUneLettre$ = CODES(i)
Exit Function
End If
Next i
End Function

Function DecoderTexte$(Mot$)
N = Len(Mot$)
MotDecode$ = ""
For i = 1 To N
Caractere$ = Mid$(Mot$, i, 1)
MotDecode$ = MotDecode$ + DecoderUneLettre$(Caractere$)
Next i
DecoderTexte$ = MotDecode$
End Function

Function DecoderUneLettre$(Lettre$)
For i = 1 To 26
If CODES(i) = Lettre$ Then
DecoderUneLettre$ = LETTRES(i)
Exit Function
End If
Next i
End Function


Feuille 3 (module annexe 2)

Public documentModifie As Boolean

Sub nouveauDocument()
If documentOuvert = True Then
If documentModifie = True Then
reponse = MsgBox("Voulez-vous enregistrer les modifications ?", vbYesNoCancel)
If reponse = vbYes Then Call enregistrerDocumentSous
ElseIf reponse <> Cancel Then
Form1.zoneTexte.Visible = True
Form1.zoneTexte.Text = ""
Form1.Caption = "Sans titre"
documentModifie = False
End If
End If
End Sub

Sub enregistrerDocument()
If Form1.Caption = "Sans titre" Then
Call enregistrerDocumentSous
Else
Call enregistrerDansFichier
End If
End Sub

Sub enregistrerDocumentSous()
If fichierExiste Then
reponse = MsgBox("Voulez-vous écraser le fichier du même nom", vbYesNoCancel)
If reponse = vbYes Then
Call enregistrerDansFichier
ElseIf reponse = vbNo Then
ComDialog1.ShowSave
End If
Else
Call enregistrerDansFichier
End If
End Sub

Sub enregistrerDansFichier(nomFichier$)
Open nomFichier$ For Output As #1
Write #1, Form1.zoneTexte.Text
Close #1
Form1.Caption = nomFichier$
End Sub

Sub ouvrirDocument()
If documentOuvert = True Then
If documentModifie = True Then
reponse = MsgBox("Souhaitez-vous enregistrer?", vbYesNoCancel)
If reponse = vbYes Then
Call enregistrerDocumentSous
ComDialog1.ShowOpen
Call lireFichier
Form1.zoneTexte.Visible = True
Form1.Caption = nomFichier$
documentModifie = False
ElseIf reponse = vbNo Then
ComDialog1.ShowOpen
Call lireFichier
Form1.zoneTexte.Visible = True
Form1.Caption = nomFichier$
documentModifie = False
End If
End If
End If
End Sub

Sub lireFichier(nomFichier$)
Open nomFichier$ For Input As #1
Write #1, Form1.zoneTexte.Text
Close #1
documentModifie = False
End Sub

Function fichierExiste(nomFichier$) As Boolean
If Dir(nomFichier$) <> "" Then
fichierExiste = True
Else
fichierExiste = False
End If
End Function


En vous remerciant d'avance. La je n'ai plus d'idée pour rendre mon script lisible...

2 réponses

cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
8 févr. 2011 à 20:35
Salut et bienvenu(e)

Oui, c'est bon, les pâtes (et pas les pattes) ... remarque, on peut aimer aussi les pattes. J'aime bien celles de mon chat.

Pour coller du code, mieux vaut passer par la coloration syntaxique de ... VB : Il conserve ainsi les indentations dans le code = plus facile à lire.

Tu ne fais pas du VB6, mais du VBA (qui a la mauvaise idée d'avoir la version 6)
--> Catégorie

Le seul endroit où tu peux trouver cette erreur, dans ton code, c'est lors de l'ouverture d'un fichier en lecture. En écriture, si le fichier n'existe pas, il le crée.
Donc, dans ta Sub lireFichier
Donc à toi de vérifier ce que vaut ta variable nomFichier.
Contient-elle bien le chemin complet ?
Le fichier existe t-il ?

En fait, en scrutant ton code, je m'aperçois de pas mal d'anomalies :
- A chaque fois que tu lance la Sub lireFichier, tu ne fournis pas de nom de fichier, juste un Call lireFichier.
- Les déclarations en tête de programme se réfèrent aux fonctions d'une DLL inconnue. Parmi ces fonctions, on retrouve aussi lireFichier : Il y a (ou il y aura) des conflits.

Vala
Jack, MVP VB
NB : Je ne répondrai pas aux messages privés

Le savoir est la seule matière qui s'accroit quand on la partage (Socrate)
0
cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
8 févr. 2011 à 20:37
Pense à lancer le menu "Debogage" + "Compiler VBAProject" dans l'interface VB de ton Excel ou Word ou autre.
Il te dira où sont les problèmes.
0
Rejoignez-nous