Bonjour à tous,
J'ai le code suivant :
qui me fait sans cesse le message d'erreur suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 'Déclaration des variables Public nom As String Public NomDoc As String Public chemin As String Public appWD As Word.Application Public oDoc As Word.Document Public nparagraphe As Integer Public date_t As String Public debug1 As Integer Private Sub createline() nparagraphe = nparagraphe + 1 If nparagraphe = 1 Then MsgBox "Debug1 / nparagraphe vaut : " & nparagraphe 'On créer l'objet et on vérifie sont existance Set appWD = CreateObject("Word.Application") If Err.Number <> 0 Then Set appWD = CreateObject("Word.Application") End If Set oDoc = appWD.Documents.Add With oDoc With .Paragraphs(1).Range .Font.Bold = True 'en gras .Font.Italic = True 'en italique .Hyperlinks.Add Anchor:=.Paragraphs(nparagraphe).Range, _ Address:="http://toto.com", _ ScreenTip:="totobulle" & nparagraphe, _ TextToDisplay:="totolink" & nparagraphe End With .SaveAs chemin .Close End With With appWD .Quit End With Else MsgBox "Debug2 / nparagraphe vaut : " & nparagraphe Set appWD = CreateObject("Word.Application") If Err.Number <> 0 Then Set appWD = CreateObject("Word.Application") End If Set oDoc = appWD.Documents.Open(chemin) With oDoc With .Paragraphs(1).Range .Font.Bold = True 'en gras .Font.Italic = True 'en italique debug1 = ActiveDocument.BuiltinDocumentProperties(wdPropertyParas) MsgBox "debug1 total 1 : " & debug1 .InsertAfter vbLf debug1 = ActiveDocument.BuiltinDocumentProperties(wdPropertyParas) MsgBox "debug1 total 2 : " & debug1 .Hyperlinks.Add Anchor:=.Paragraphs(nparagraphe).Range, _ Address:="http://toto1.com", _ ScreenTip:="totobulle" & nparagraphe, _ TextToDisplay:="totolink" & nparagraphe End With .SaveAs chemin .Close End With With appWD .Quit End With End If End Sub Sub Test() 'Initialisation variables nom = "" NomDoc = "" chemin = "" nparagraphe = 0 date_t = "" 'Remplissage des variables date_t = Format(Now(), "dd.mm.yy_hh.mm.ss") nom = "ListeLiens" & "-" & date_t NomDoc = nom & ".doc" nparagraphe = 0 'Vérification du nom pour etre sur qu'il n'est pas vide If Len(nom) = 1 Then MsgBox "Le nom est vide" Exit Sub 'si la ligne ne contient ni nom ni prénom on quitte End If 'On indique le dossier d'enregistrement du .doc chemin = "P:\" & NomDoc 'On appelle la fonction d'ecriture du document For i = 1 To 5 createline Next 'On reinitialise les variables nom = "" NomDoc = "" chemin = "" nparagraphe = 0 date_t = "" End Sub
Cela est du au fait que le nombre de paragraphes ne s’incrémente pas... Ma variable debug1 le prouve... Elle reste figée à "1"...
J'ai tenté de remplacer ".InsertAfter vbLf" par :
Mais rien à faire ce nombre ne s'incrémente pas... Pouvez-vous m'aidez s'il vous plaît ? Merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 .Paragraphs.Add .InsertParagraphAfter .InsertAfter vbCrLf
PS1 : Le doc word qui se créé a bien une ligne de plus pourtant...
PS2 : C'est une macro faite dans Excel... Le but est de générer un .doc à partir de mon excel... Les .markbook ne m’intéresse pas car je ne sais pas à l'avance combien de boucle va faire ma boucle for à l'avenir.
Partager