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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| Option Explicit
Private Sub Appel()
Dim Chemin$, Tablo As Variant, LeMot$
Chemin = "C:\CGI\DocProdRapport\Commercial\"
If Not Dir(Chemin) <> "" Then
MsgBox "Répertoire inexistant"
Exit Sub
End If
Dim appWd As Word.Application
Set appWd = CreateObject("Word.Application")
appWd.Visible = True
LeMot = "\" 'InputBox("Saisir le mot à chercher", "RECHERCHE", "Options")
'CreateObject("Wscript.shell").Popup "Minute papillon, je bosse !", 1, "PATIENCE, ÇA VIENT !"
If Trim(LeMot) <> "" Then
Call Lister(appWd, Chemin, LeMot)
End If
appWd.Quit
Set appWd = Nothing
MsgBox "èf' I... FI, èn' i... NI c'est FINI !"
End Sub
'Liste les fichiers (*.doc) du répertoire
Private Sub Lister(prmAppWd As Word.Application, Chemin As String, LeMot As String)
Dim nomFicResult As String: nomFicResult = Chemin & ThisDocument.Name
nomFicResult = Replace(nomFicResult, ".doc", ".txt")
Dim numFicResult As Long: numFicResult = FreeFile()
Open nomFicResult For Output As numFicResult
Dim NomFich As String
Dim LeDoc As Document
NomFich = Dir(Chemin & "*.doc")
'Vérification de l'existence de fichiers dans le répertoire
If NomFich = "" Then
MsgBox "Aucun fichier dans le répertoire " & Chemin
Exit Sub
End If
'Ouverture des fichiers du répertoire
Do While NomFich <> ""
ThisDocument.Save
Debug.Print NomFich
If Chemin & NomFich <> ThisDocument.FullName Then
Set LeDoc = prmAppWd.Documents.Open(Chemin & NomFich)
DoEvents
'Lance la recherche
Call Chercher(prmAppWd, numFicResult, LeMot, Chemin & NomFich)
'Ferme le document objet de la recherche
LeDoc.Close False
Set LeDoc = Nothing
End If
DoEvents
'Passe au fichier suivant
NomFich = Dir
Loop
Close #numFicResult
End Sub
'Recherche du mot dans le fichier ouvert
Private Sub Chercher(prmAppWd As Word.Application, prmNumFicResult As Long, LeMot As String, NomComplet As String)
Dim clipboard As DataObject
Dim Lien As Boolean
'Utile pour n'insérer le lien qu'une seule fois, ici avant copie des paragraphes
Lien = True
'Place en début de doc avant de lancer la recherche
prmAppWd.Selection.HomeKey Unit:=wdStory
With prmAppWd.Selection.Find
.ClearFormatting
'Début la boucle de recherche : Tant que la donnée est trouvée, on continue
Do While .Execute(FindText:=LeMot, Forward:=True, _
Wrap:=wdFindStop)
'Crée un lien hypertexte vers le document contenant le mot
If Lien Then
'Insère un saut de ligne avant de coller le lien
Print #prmNumFicResult, String(80, "=")
Print #prmNumFicResult, "Fichier source"
Print #prmNumFicResult, NomComplet$
Print #prmNumFicResult, ""
Lien = False
End If
'renvoie en début de ligne
prmAppWd.Selection.HomeKey Unit:=wdLine
'Sélectionne le paragraphe
prmAppWd.Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
'Copie le paragraphe
prmAppWd.Selection.Copy
'Colle le paragraphe dans le document principal
Set clipboard = New DataObject
clipboard.GetFromClipboard
Print #prmNumFicResult, String(80, "-")
Print #prmNumFicResult, Trim(clipboard.GetText)
Print #prmNumFicResult, String(80, ".")
Print #prmNumFicResult, ""
prmAppWd.Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop
End With
End Sub |
Partager