Bonjour.
J'essaie de mettre en forme 2 tableaux Word situés dans 2 fichiers différents, je précise que ces 2 codes marchent parfaitement (forcément ils sont inspirés de données glanées sur le site développer.com). Le problème est que si j'exécute un des 2 codes à la suite de l'autre j'ai une erreur 462:
voici les 2 codes:
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
114 Sub MiseEnFormeTableau1() 'Dim WordApp As Word.Application Dim WordApp As Object 'Dim WordDoc As Word.Document Dim WordDoc As Object Dim sChemin As String 'Dim objTable As Word.Table 'Dim myRange As Word.Range Dim objTable As Object Dim myRange As Object 'Affectations sChemin = ExtractFolder(CurrentDb.Name) Set WordApp = CreateObject("word.application") WordApp.Visible = False 'Word reste masqué pendant l'opération Set WordDoc = WordApp.Documents.Open(sChemin & "Tableau_1.rtf") 'ouvre le document Word Set objTable = WordDoc.Tables(1) 'Selection de la 1ere ligne objTable.Rows(1).Select 'Couleur 1ere ligne fond et police Selection.Shading.Texture = wdTextureNone Selection.Shading.Texture = wdTextureNone Selection.Shading.ForegroundPatternColor = wdColorAutomatic Selection.Shading.BackgroundPatternColor = -738148353 Selection.Font.Color = -603914241 'Selection de la 3eme col objTable.Columns(3).Select 'Ajustement largeur de colonne 3 Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=76.05, RulerStyle:= _ wdAdjustFirstColumn Selection.Tables(1).Columns(3).SetWidth ColumnWidth:=76.05, RulerStyle:= _ wdAdjustFirstColumn Set myRange = WordDoc.Range(objTable.Cell(1, 1) _ .Range.Start, objTable.Cell(objTable.Rows.Count - 2, 4).Range.End) myRange.Select 'Bordures With Selection.Borders(wdBorderTop) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderLeft) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderBottom) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderRight) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderHorizontal) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderVertical) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With Set myRange = WordDoc.Range(objTable.Cell(objTable.Rows.Count - 1, 3) _ .Range.Start, objTable.Cell(objTable.Rows.Count, 4).Range.End) myRange.Select With Selection.Borders(wdBorderTop) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderLeft) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderBottom) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderRight) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderHorizontal) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderVertical) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With 'Libération des objets Set objTable = Nothing Set myRange = Nothing WordDoc.Close 'fermeture document Word WordApp.Quit 'fermeture session Word MsgBox "La mise en forme du tableau est terminée.", vbInformation, "Mise en forme" End Sub
J'ai utilisé une liaison tardive mais cela n'a pas résolu le problème. J'ai changé le nom des objets entre les 2 codes , sans succès . Le 1er code exécuté après le 2d donne une erreur ligne 24 (du premier code). Si je la résous en faisant référence à l'objet et à la ligne : objTable.Rows(1) à la place de selection , l'erreur se propage plus bas.
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 Sub MiseEnFormeTableau2() 'Dim WordApp2 As Word.Application 'Dim WordDoc2 As Word.Document Dim WordApp2 As Object Dim WordDoc2 As Object Dim sChemin As String 'Dim objTable2 As Word.Table 'Dim myRange2 As Word.Range Dim objTable2 As Object Dim myRange2 As Object 'Affectations sChemin = ExtractFolder(CurrentDb.Name) Set WordApp2 = CreateObject("word.application") WordApp2.Visible = False 'Word reste masqué pendant l'opération Set WordDoc2 = WordApp2.Documents.Open(sChemin & "Tableau_2.rtf") 'ouvre le document Word Set objTable2 = WordDoc2.Tables(1) 'Selection de la 1ere ligne objTable2.Rows(1).Select 'Couleur 1ere ligne fond et police Selection.Shading.Texture = wdTextureNone Selection.Shading.ForegroundPatternColor = wdColorAutomatic Selection.Shading.BackgroundPatternColor = -738148353 Selection.Font.Color = -603914241 'Bordures Set myRange2 = WordDoc2.Range(objTable2.Cell(1, 1) _ .Range.Start, objTable2.Cell(3, 4).Range.End) myRange2.Select With Selection.Borders(wdBorderTop) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderLeft) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderBottom) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderRight) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderHorizontal) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With With Selection.Borders(wdBorderVertical) .LineStyle = Options.DefaultBorderLineStyle .LineWidth = Options.DefaultBorderLineWidth .Color = Options.DefaultBorderColor End With 'Libération des objets Set objTable2 = Nothing Set myRange2 = Nothing WordDoc2.Close 'fermeture document Word WordApp2.Quit 'fermeture session Word MsgBox "La mise en forme du tableau est terminée.", vbInformation, "Mise en forme" End Sub
J'avoue que je n'ai pas trouvé la réponse dans les différentes discussions traitant du même sujet.
Pouvez vous m'aider s'il vous plaît merci
Partager