View Single Post
 
Old 12-04-2020, 12:20 PM
jpl jpl is offline Windows 7 64bit Office 2010 32bit
Advanced Beginner
 
Join Date: Jan 2016
Location: France
Posts: 33
jpl is on a distinguished road
Default

Dans le code de Greg Maxey, la boucle While est bien plus belle et efficace que la mienne.
Voici donc une nouvelle version qui reprend la boucle de Greg.
Code:
Sub Essai()
  Dim Trouvé As String, ListeTrouvés As String
  Dim Prefixes As Variant, Prefixe As Variant
  Dim Total As Long

  'pour sortie dans un fichier Word
  Dim Fichier As Document

  ListeTrouvés = " "
  Prefixes = Array("PE-", "JEB-", "HEL-")

  For Each Prefixe In Prefixes

    With ActiveDocument.Content.Find
    
      .ClearFormatting
      .MatchWildcards = True
      .Text = Prefixe & "[0-9]{1,}"
      .Forward = True


      Do While .Execute
        Trouvé = .Parent.Text
        If InStr(1, ListeTrouvés, Trouvé & " ") = 0 Then
          ListeTrouvés = ListeTrouvés & Trouvé & " "
          Total = Total + 1
        End If
      Loop
      
    End With
  Next Prefixe

  ListeTrouvés = Trim(ListeTrouvés)
  ListeTrouvés = Replace(ListeTrouvés, " ", vbCrLf)
  ListeTrouvés = ListeTrouvés & vbCrLf & "Total = " & CStr(Total)

  'Création du fichier Word de sauvegarde de la liste
  Set Fichier = Documents.Add()
  Fichier.Range.Text = "Liste à sauvegarder" & vbCrLf & ListeTrouvés

End Sub

Last edited by jpl; 12-04-2020 at 12:22 PM. Reason: correction de la malédiction
Reply With Quote