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
|
Sub test2()
Dim DocEnCours As Document, NouveauDoc As Document
Dim MatriceRanges() As Variant
Dim I As Long, IndexMatrice As Long, CaractereFin As Long
Dim StrFilename As Variant
Set DocEnCours = ActiveDocument
With DocEnCours
IndexMatrice = 0
CaractereFin = .Characters.Count
For I = .Paragraphs.Count To 1 Step -1
If InStr(1, .Paragraphs(I).Range, "Reference: ", vbTextCompare) > 0 Then
.Paragraphs(I).Range.Select
StrFilename = Trim(Split(.Paragraphs(I).Range.Text, ":")(1))
StrFilename = Mid(StrFilename, 1, Len(StrFilename) - 1)
Selection.HomeKey unit:=wdLine
Selection.HomeKey unit:=wdStory, Extend:=wdExtend
ReDim Preserve MatriceRanges(2, IndexMatrice)
MatriceRanges(0, IndexMatrice) = Selection.Characters.Count
MatriceRanges(1, IndexMatrice) = CaractereFin
MatriceRanges(2, IndexMatrice) = StrFilename
CaractereFin = MatriceRanges(0, IndexMatrice) - 1
IndexMatrice = IndexMatrice + 1
End If
Next I
For IndexMatrice = LBound(MatriceRanges, 2) To UBound(MatriceRanges, 2)
If MatriceRanges(2, IndexMatrice) <> "" Then
Selection.HomeKey unit:=wdStory, Extend:=wdExtend
.Range.Select
Selection.SetRange MatriceRanges(0, IndexMatrice), MatriceRanges(1, IndexMatrice)
Selection.Range.Copy
Set NouveauDoc = Documents.Add
With NouveauDoc
.Range.Paste
.SaveAs DocEnCours.Path & "\" & MatriceRanges(2, IndexMatrice)
.Close True
End With
Set NouveauDoc = Nothing
End If
Next IndexMatrice
End With
Set DocEnCours = Nothing
End Sub |
Partager