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
| Sub Bouton2_Clic()
Dim oWdApp As Object
Dim oWdDoc As Object
Dim oWdDocExp As Object
Dim oRng1 As Object
Dim oRng2 As Object
Dim sDebut As String
Dim sFin As String
Dim oRng3 As Object
Dim oRng4 As Object
Set oWdApp = CreateObject("Word.Application")
oWdApp.Visible = True
Set oWdDocExp = oWdApp.Documents.Add
If bWordIsOpen(Cells(Chemin_L, Chemin_C + 1).Value) = False Then
Set oWdApp = CreateObject("Word.Application")
oWdApp.Visible = True
Set oWdDoc = oWdApp.Documents.Open(Cells(Chemin_L, Chemin_C + 1).Value)
ElseIf bWordIsOpen(Cells(Chemin_L, Chemin_C + 1).Value) = True Then
Set oWdDoc = GetObject(Cells(Chemin_L, Chemin_C + 1).Value)
End If
For i = Cocher_L + 2 To Cells(Cocher_L + 2, Cocher_C + 2).End(xlDown).Row
If Cells(i, Cocher_C).Interior.ColorIndex = 3 Then
sDebut = Cells(i, Cocher_C + 2).Value
sFin = Cells(i + 1, Cocher_C + 2).Value
For j = i + 1 To Cells(Cocher_L + 2, Cocher_C + 2).End(xlDown).Row
If Cells(j, Cocher_C).Interior.ColorIndex = 3 Then
sFinexp = Cells(j, Cocher_C + 1).Value
Exit For
End If
Next
If sFin <> "" Then
Set oRng1 = oWdDoc.Range
If oRng1.Find.Execute(FindText:=sDebut) Then
Set oRng2 = oWdDoc.Range(oRng1.Start, oWdDoc.Range.End)
If oRng2.Find.Execute(FindText:=sFin) Then
oWdDoc.Range(oRng1.Start, oRng2.Start).Copy
oWdDocExp.Parent.Selection.Paste
End If
End If
Set oRng3 = oWdDocExp.Range
If oRng3.Find.Execute(FindText:=sDebut) Then
Set oRng4 = oWdDocExp.Range(oRng3.Start, oWdDocExp.Range.End)
If oRng4.Find.Execute(FindText:=sFinexp) Then
oWdDocExp.Range(oRng3.Start, oRng4.Start).SetListLevel (Len(Replace(Cells(i, Cocher_C + 2).Value, ".", "")))
End If
End If
ElseIf sFin = "" Then
Set oRng1 = oWdDoc.Range
If oRng1.Find.Execute(FindText:=sDebut) Then
oWdDoc.Range(oRng1.Start).Copy
oWdDocExp.Parent.Selection.Paste
End If
Set oRng3 = oWdDocExp.Range
If oRng3.Find.Execute(FindText:=sDebut) Then
Set oRng4 = oWdDocExp.Range
oWdDocExp.Range(oRng3.Start, oRng4.End).SetListLevel (Len(Replace(Cells(i, Cocher_C + 2).Value, ".", "")))
End If
End If
End If
Next i
End Sub
Function bWordIsOpen(sPath As String) As Boolean
Dim oWdApp As Object
Dim oWdDoc As Object
On Error Resume Next
Set oWdApp = GetObject(, "Word.Application")
Set oWdDoc = oWdApp.Documents(sPath)
On Error GoTo 0
If oWdDoc Is Nothing Then
bWordIsOpen = False
Else
bWordIsOpen = True
End If
End Function |
Partager