Bonjour à tous,
Je cherche à écrire une macro qui me permettra de rechercher et remplacer plusieurs termes dans de nombreux fichiers (plusieurs centaines).
Voici ce que j'ai pour l'instant :
Parfois la macro fonctionne sur un ou deux fichiers, parfois elle ne se lance pas du tout. Lorsque j'ai un message d'erreur, VBA me signale qu'il ne trouve pas tel ou tel fichier, pas forcément le même à chaque fois. La macro fonctionne en général sur au moins un fichier avant de planter.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 Sub rechercher_remplacer_batch() Dim Directory As String Dim FType As String Dim FName As String 'mettre ici le chemin d'accès au dossier où se trouvent les fichiers Directory = "C:\fic" FType = "*.rtf" ChDir Directory FName = Dir(FType) Do While FName <> "" Documents.Open FileName:=FName Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find ' copier autant de fois qu'il y a d'éléments à remplacer .Text = "(-:" .MatchCase = True .Replacement.Text = "{0>" .MatchWildcards = False End With Selection.Find.Execute Replace:=wdReplaceAll ' copier autant de fois qu'il y a d'éléments à remplacer With Selection.Find .Text = ")=" .MatchCase = True .Replacement.Text = "<}" .MatchWildcards = False End With Selection.Find.Execute Replace:=wdReplaceAll ' copier autant de fois qu'il y a d'éléments à remplacer With Selection.Find .Text = "101" .MatchCase = True .Replacement.Text = "100" .MatchWildcards = False End With Selection.Find.Execute Replace:=wdReplaceAll ' copier autant de fois qu'il y a d'éléments à remplacer With Selection.Find .Text = "%(" .MatchCase = True .Replacement.Text = "{>" .MatchWildcards = False End With Selection.Find.Execute Replace:=wdReplaceAll ' copier autant de fois qu'il y a d'éléments à remplacer With Selection.Find .Text = ":-)" .MatchCase = True .Replacement.Text = "<0}" .MatchWildcards = False End With Selection.Find.Execute Replace:=wdReplaceAll ActiveDocument.Close wdSaveChanges FName = Dir Loop End Sub
De toute évidence je loupe quelque chose, mais j'ai beau triturer ce bout de code, je tourne en rond !
Merci d'avance à ceux qui pourront m'aider !
Partager