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
| Public Sub test()
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim i As Byte
Dim n As Long
Set WordApp = CreateObject("word.application") 'ouvre une session Word
Set WordDoc = WordApp.Documents.Open("monfichier.doc") 'ouvre le document Word
WordApp.Visible = False 'Word est masqué pendant l'opération
n = Range("A2").SpecialCells(xlCellTypeLastCell).Row 'Compte le nombre de lignes non vides
For i = 2 To n
If Cells(i, 5) = "A" Then
'les signets du document Word sont nommés Signet1 , Signet2 , Signet3 ,...
WordDoc.Bookmarks("Signet" & 1).Range.Text = Cells(i, 16) & vbCrLf
ElseIf Cells(i, 5) = "B" Then
WordDoc.Bookmarks("Signet" & 2).Range.Text = Cells(i, 16) & vbCrLf
ElseIf Cells(i, 5) = "C" Then
WordDoc.Bookmarks("Signet" & 3).Range.Text = Cells(i, 16) & vbCrLf
ElseIf Cells(i, 5) = "D" Then
WordDoc.Bookmarks("Signet" & 4).Range.Text = Cells(i, 16) & vbCrLf
ElseIf Cells(i, 5) = "E" Then
WordDoc.Bookmarks("Signet" & 5).Range.Text = Cells(i, 16) & vbCrLf
End If
Next i
WordApp.Visible = True 'affiche le document Word
End Sub |
Partager