Bonjour,
J'ai un fichier texte très long à écrire via VBA. J'ai donc crée un appel de macro pour limiter le nombre de ligne par sub. Je me demandais si en bonne pratique il convenait de vider la mémoire à chaque sub ou juste à la fin. Pour faire simple préconisez vous le code 1 :
ou le code 2
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Option Explicit Dim Repertoire As String, FS As FileSystemObject, Output As TextStream Sub Texte_1() Repertoire = ThisWorkbook.Path & "\Fichier.txt" Set FS = CreateObject("Scripting.FileSystemObject") Set Output = FS.CreateTextFile(Repertoire, True, False) Output.Writeline ("Ligne.1_1") Output.Writeline ("Ligne.1_2") '... Output.Writeline ("Ligne.1_n") Output.Close Set FS = Nothing '? Set Output = Nothing '? Call Texte_2 Call Texte_x End Sub Sub Texte_2() Set FS = CreateObject("Scripting.FileSystemObject") Set Output = FS.OpenTextFile(Repertoire, ForAppending) Output.Writeline ("Ligne.2_1") Output.Writeline ("Ligne.2_2") '... Output.Writeline ("Ligne.2_n") Output.Close Set FS = Nothing '? Set Output = Nothing '? End Sub Sub Texte_x() Set FS = CreateObject("Scripting.FileSystemObject") Set Output = FS.OpenTextFile(Repertoire, ForAppending) Output.Writeline ("Ligne.x_1") Output.Writeline ("Ligne.x_2") '... Output.Writeline ("Ligne.x_n") Output.Close Set FS = Nothing '? Set Output = Nothing '? End Sub
Merci de votre retour
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Option Explicit Dim Repertoire As String, FS As FileSystemObject, Output As TextStream Sub Texte_1() Repertoire = ThisWorkbook.Path & "\Fichier.txt" Set FS = CreateObject("Scripting.FileSystemObject") Set Output = FS.CreateTextFile(Repertoire, True, False) Output.Writeline ("Ligne.1_1") Output.Writeline ("Ligne.1_2") '... Output.Writeline ("Ligne.1_n") Output.Close Call Texte_2 Call Texte_x Set FS = Nothing '? Set Output = Nothing '? End Sub Sub Texte_2() Set FS = CreateObject("Scripting.FileSystemObject") Set Output = FS.OpenTextFile(Repertoire, ForAppending) Output.Writeline ("Ligne.2_1") Output.Writeline ("Ligne.2_2") '... Output.Writeline ("Ligne.2_n") Output.Close End Sub Sub Texte_x() Set FS = CreateObject("Scripting.FileSystemObject") Set Output = FS.OpenTextFile(Repertoire, ForAppending) Output.Writeline ("Ligne.x_1") Output.Writeline ("Ligne.x_2") '... Output.Writeline ("Ligne.x_n") Output.Close End Sub
Partager