Bonjour,
Avant de poster ce message, j'ai cherché sur le forum (et plus généralement sur tout le net) et n'ait évidemment rien trouvé (enfin rien qui fonctionne.
J'essaie de créer une page web (ASP avec C# en code-behind sur IIS), qui me permette (pour l'instant) d'ouvrir, modifier, enregistrer puis fermer un document word en local sur le serveur.
Mes versions sont les suivantes : .Net Framework 1.1 SP1, Office 2003 SP2 avec toutes les mises à jour de sécurité, Windows XP SP2 (pour IIS, celui qui va avec), et je code avec Visual Studio .Net 2003 (SP1) (Mes codes sources sont après)
Avant d'essayer cette manip', j'ai fait une appli windows classique en C#, qui faisait la même chose, et ça marchait sans problème. (sachant que j'ai fait le test avec Excel et Word)
Quand j'ai copié le code dans le code-behind de mon appli serveur, il marchait très bien avec Excel, mais pas avec Word : le programme tourne en permanence, en bloquant à la commande Documents.Open(ref "test.doc",...); c'est à dire à l'ouverture du fichier.
Après plein de tentatives, il semblerait que ce problème vienne des droits de l'utilisateur ASPNET (qui exécute la commande), à qui l'on "interdit" de prendre trop de place en mémoire vive.
J'ai essayé de trouver où on pouvait changer ces fameux droits, sans succès (ah oui une autre chose : il a déjà fallu que je donne l'accès aux objets COM de mon PC au user "ASPNET" pour interagir avec Office).
Vous pouvez m'aider?
Ca serait vachement cool.
Sur ce, mes sources :
L'objet du délit :
La classe de code-behind qui contient le main (quasi-entièrement générée par visual studio) :
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
115
116
117 using System; using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Excel; namespace TestsExportsEtConnexionBdD { /// <summary> /// Description résumée de exportOffice. /// </summary> public class exportOffice { public exportOffice() { // // TODO : ajoutez ici la logique du constructeur // } public System.String genererXL() { // Ouverture du Fichier Excel Microsoft.Office.Interop.Excel.ApplicationClass oExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); oExcelApp.Visible = false; Workbooks oBooks = oExcelApp.Workbooks; Workbook oBook = oBooks.Open(@"D:\test.xls", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); Worksheet oSheet = (Worksheet)oBook.Worksheets[1]; // Nettoyage du Fichier for(int row=8; row<10; row++) for(int col=1; col<6; col++) oSheet.Cells[row,col] = System.DateTime.Now.ToString(); for(int row=13; row<53; row++) for(int col=1; col<4; col++) oSheet.Cells[row,col] = System.DateTime.Now.ToString(); // Sauvegarde et Fermeture du Fichier oBook.Save(); if (oBook != null) oBook.Close(true, oBook, null); if (oSheet != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet); oSheet = null; if (oBook != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook); oBook = null; if (oBooks != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks); oBooks = null; if (oExcelApp != null) { oExcelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcelApp); oExcelApp = null; } return ("Export Excel fini à ce moment : \n" + System.DateTime.Now.ToString()); } public System.String genererWord() { // Objets nécessaires à l'API de MS Word object chemin = @"D:\test.doc"; //object oXML = "XML"; object oFalse = false; object oTrue = true; object oDynamic = 2; object oUn = 1; object oMax = 65535; object oMissing = System.Reflection.Missing.Value; Document oDocument; // Ouverture du logiciel Word Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); oWordApp.Visible = false; Documents oDocuments = oWordApp.Documents; try { // Ouverture du fichier Word oDocument = oDocuments.Open(ref chemin, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDocument.Activate(); // Ecriture du texte // oWordApp.Selection.MoveDown(ref oMissing, ref oUn, ref oMissing); // Permet de déplacer le curseur en bas (en l'occurence, d'un pas) // oWordApp.Selection.MoveEnd(ref oMissing, ref oUn); // Pas compis à quoi ça sert oWordApp.Selection.MoveDown(ref oMissing, ref oMax, ref oMissing); oWordApp.Selection.TypeText(System.DateTime.Now.ToString()); oWordApp.Selection.TypeParagraph(); // Sauvegarde oDocument.Save(); // Fermeture du fichier Word if (oDocument != null) oDocument.Close(ref oFalse, ref oMissing, ref oMissing); if (oDocument != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocument); oDocument = null; } catch (Exception e) { return e.ToString(); } // Fermeture du logiciel Word if (oDocuments != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocuments); oDocuments = null; if (oWordApp != null) { oWordApp.Quit(ref oFalse, ref oMissing, ref oMissing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp); oWordApp = null; } return ("Export Word fini à ce moment : \n" + System.DateTime.Now.ToString()); } } }
Et le code de mon appli windows classique, qui marche sans problème :
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 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace TestsExportsEtConnexionBdD { /// <summary> /// Description résumée de WebForm1. /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button excelButton; protected System.Web.UI.WebControls.Button wordButton; private exportOffice eo = new exportOffice(); private void Page_Load(object sender, System.EventArgs e) { // Placer ici le code utilisateur pour initialiser la page } #region Code généré par le Concepteur Web Form override protected void OnInit(EventArgs e) { // // CODEGEN : Cet appel est requis par le Concepteur Web Form ASP.NET. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas /// le contenu de cette méthode avec l'éditeur de code. /// </summary> private void InitializeComponent() { this.excelButton.Click += new System.EventHandler(this.excelButton_Click); this.wordButton.Click += new System.EventHandler(this.wordButton_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void excelButton_Click(object sender, System.EventArgs e) { this.Label1.Text = eo.genererXL(); } private void wordButton_Click(object sender, System.EventArgs e) { this.Label1.Text = eo.genererWord(); } } }
Vous remarquerez que les fichiers de l'appli classique sont sur le lecteur K, contrairement aux fichiers exploités par le code-behind : le lecteur K est un lecteur réseau, du coup les applis normales ont accès directement à mon répertoire d'utilisateur sur ce disque, contrairement aux applis lancées par ASPNET
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Word; namespace WindowsApplication2 { /// <summary> /// Description résumée de Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button2; private System.Windows.Forms.Button excel; private System.Windows.Forms.Button word; private System.Windows.Forms.Label label1; /// <summary> /// Variable nécessaire au concepteur. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Requis pour la prise en charge du Concepteur Windows Forms // InitializeComponent(); // // TODO : ajoutez le code du constructeur après l'appel à InitializeComponent // } /// <summary> /// Nettoyage des ressources utilisées. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Code généré par le Concepteur Windows Form /// <summary> /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas /// le contenu de cette méthode avec l'éditeur de code. /// </summary> private void InitializeComponent() { this.excel = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.word = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // excel // this.excel.Location = new System.Drawing.Point(280, 24); this.excel.Name = "excel"; this.excel.TabIndex = 0; this.excel.Text = "excel"; this.excel.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(392, 88); this.button2.Name = "button2"; this.button2.TabIndex = 1; this.button2.Text = "stop"; this.button2.Click += new System.EventHandler(this.button2_Click); // // word // this.word.Location = new System.Drawing.Point(496, 24); this.word.Name = "word"; this.word.TabIndex = 2; this.word.Text = "word"; this.word.Click += new System.EventHandler(this.word_Click); // // label1 // this.label1.Location = new System.Drawing.Point(8, 128); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(752, 264); this.label1.TabIndex = 3; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(768, 397); this.Controls.Add(this.label1); this.Controls.Add(this.word); this.Controls.Add(this.button2); this.Controls.Add(this.excel); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// Point d'entrée principal de l'application. /// </summary> [STAThread] static void Main() { System.Windows.Forms.Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { genererXL(); } private void genererXL() { // Ouverture du Fichier Excel Microsoft.Office.Interop.Excel.ApplicationClass oExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); oExcelApp.Visible = false; Workbooks oBooks = oExcelApp.Workbooks; Workbook oBook = oBooks.Open(@"K:\test.xls", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); Worksheet oSheet = (Worksheet)oBook.Worksheets[1]; // Nettoyage du Fichier for(int row=8; row<10; row++) for(int col=1; col<6; col++) oSheet.Cells[row,col] = System.DateTime.Now.ToString(); for(int row=13; row<53; row++) for(int col=1; col<4; col++) oSheet.Cells[row,col] = System.DateTime.Now.ToString(); // Remplissage du Fichier // Sauvegarde et Fermeture du Fichier oBook.Save(); if (oBook != null) oBook.Close(true, oBook, null); if (oSheet != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet); oSheet = null; if (oBook != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook); oBook = null; if (oBooks != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks); oBooks = null; if (oExcelApp != null) { oExcelApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcelApp); oExcelApp = null; } // Envoie du fichier this.label1.Text = "Export Excel fini à ce moment : \n" + System.DateTime.Now.ToString(); } private void button2_Click(object sender, System.EventArgs e) { this.Close(); } private void word_Click(object sender, System.EventArgs e) { genererWord(); } private void genererWord() { // Objets nécessaires à l'API de MS Word object chemin = "K:\\test.doc"; object oFalse = false; object oTrue = true; object oDynamic = 2; object oUn = 1; object oMax = 65535; object oMissing = System.Reflection.Missing.Value; Document oDocument; // Ouverture du logiciel Word Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); oWordApp.Visible = false; Documents oDocuments = oWordApp.Documents; try { // Ouverture du fichier Word oDocument = oDocuments.Open(ref chemin, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oDocument.Activate(); // Ecriture du texte // oWordApp.Selection.MoveDown(ref oMissing, ref oUn, ref oMissing); // Permet de déplacer le curseur en bas (en l'occurence, d'un pas) // oWordApp.Selection.MoveEnd(ref oMissing, ref oUn); // Pas compis à quoi ça sert oWordApp.Selection.MoveDown(ref oMissing, ref oMax, ref oMissing); oWordApp.Selection.TypeText(System.DateTime.Now.ToString()); oWordApp.Selection.TypeParagraph(); // Sauvegarde oDocument.Save(); // Fermeture du fichier Word if (oDocument != null) oDocument.Close(ref oFalse, ref oMissing, ref oMissing); if (oDocument != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocument); oDocument = null; } catch (Exception e) { this.label1.Text = e.ToString(); } // Fermeture du logiciel Word if (oDocuments != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocuments); oDocuments = null; if (oWordApp != null) { oWordApp.Quit(ref oFalse, ref oMissing, ref oMissing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp); oWordApp = null; } this.label1.Text = "Export Word fini à ce moment : \n" + System.DateTime.Now.ToString(); } } }![]()
Partager