1
using
System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Runtime.InteropServices;
9
10 namespace WinWordControl
11 {
12 public partial class WinWordControl : UserControl
13 {
14 public WinWordControl()
15 {
16 InitializeComponent();
17
18 }
19
20 #region "API usage declarations"
21
22 [DllImport( " user32.dll " )]
23 public static extern int FindWindow( string strclassName, string strWindowName);
24
25 [DllImport( " user32.dll " )]
26 static extern int SetParent( int hWndChild, int hWndNewParent);
27
28 [DllImport( " user32.dll " , EntryPoint = " SetWindowPos " )]
29 static extern bool SetWindowPos(
30 int hWnd, // handle to window
31 int hWndInsertAfter, // placement-order handle
32 int X, // horizontal position
33 int Y, // vertical position
34 int cx, // width
35 int cy, // height
36 uint uFlags // window-positioning options
37 );
38
39 [DllImport( " user32.dll " , EntryPoint = " MoveWindow " )]
40 static extern bool MoveWindow(
41 int Wnd,
42 int X,
43 int Y,
44 int Width,
45 int Height,
46 bool Repaint
47 );
48
49 [DllImport( " user32.dll " , EntryPoint = " DrawMenuBar " )]
50 static extern Int32 DrawMenuBar(
51 Int32 hWnd
52 );
53
54 [DllImport( " user32.dll " , EntryPoint = " GetMenuItemCount " )]
55 static extern Int32 GetMenuItemCount(
56 Int32 hMenu
57 );
58
59 [DllImport( " user32.dll " , EntryPoint = " GetSystemMenu " )]
60 static extern Int32 GetSystemMenu(
61 Int32 hWnd,
62 bool Revert
63 );
64
65 [DllImport( " user32.dll " , EntryPoint = " RemoveMenu " )]
66 static extern Int32 RemoveMenu(
67 Int32 hMenu,
68 Int32 nPosition,
69 Int32 wFlags
70 );
71
72
73 private const int MF_BYPOSITION = 0x400 ;
74 private const int MF_REMOVE = 0x1000 ;
75
76
77 const int SWP_DRAWFRAME = 0x20 ;
78 const int SWP_NOMOVE = 0x2 ;
79 const int SWP_NOSIZE = 0x1 ;
80 const int SWP_NOZORDER = 0x4 ;
81
82 #endregion
83
84 public Word.Document document;
85 public static Word.ApplicationClass wd = null ;
86 public static int wordWnd = 0 ;
87 public static string filename = null ;
88 private static bool deactivateevents = false ;
89
90 /// <summary>
91 /// needed designer variable
92 /// </summary>
93 // private System.ComponentModel.Container components = null;
94
95 /// <summary>
96 /// Preactivation
97 /// It's usefull, if you need more speed in the main Program
98 /// so you can preload Word.
99 /// </summary>
100 public void PreActivate()
101 {
102 if (wd == null ) wd = new Word.ApplicationClass();
103 }
104
105 /// <summary>
106 /// Close the current Document in the control --> you can
107 /// load a new one with LoadDocument
108 /// </summary>
109 public void CloseControl()
110 {
111 /*
112 * this code is to reopen Word.
113 */
114
115 try
116 {
117 deactivateevents = true ;
118 object dummy = null ;
119 object dummy2 = ( object ) false ;
120 document.Close( ref dummy, ref dummy, ref dummy);
121 // Change the line below.
122 wd.Quit( ref dummy2, ref dummy, ref dummy);
123 deactivateevents = false ;
124 }
125 catch (Exception ex)
126 {
127 String strErr = ex.Message;
128 }
129 }
130
131
132 /// <summary>
133 /// catches Word's close event
134 /// starts a Thread that send a ESC to the word window ;)
135 /// </summary>
136 /// <param name="doc"></param>
137 /// <param name="test"></param>
138 private void OnClose(Word.Document doc, ref bool cancel)
139 {
140 if ( ! deactivateevents)
141 {
142 cancel = true ;
143 }
144 }
145
146 /// <summary>
147 /// catches Word's open event
148 /// just close
149 /// </summary>
150 /// <param name="doc"></param>
151 private void OnOpenDoc(Word.Document doc)
152 {
153 OnNewDoc(doc);
154 }
155
156 /// <summary>
157 /// catches Word's newdocument event
158 /// just close
159 /// </summary>
160 /// <param name="doc"></param>
161 private void OnNewDoc(Word.Document doc)
162 {
163 if ( ! deactivateevents)
164 {
165 deactivateevents = true ;
166 object dummy = null ;
167 doc.Close( ref dummy, ref dummy, ref dummy);
168 deactivateevents = false ;
169 }
170 }
171
172 /// <summary>
173 /// catches Word's quit event
174 /// normally it should not fire, but just to be shure
175 /// safely release the internal Word Instance
176 /// </summary>
177 private void OnQuit()
178 {
179 // wd=null;
180 }
181
182
183 /// <summary>
184 /// Loads a document into the control
185 /// </summary>
186 /// <param name="t_filename"> path to the file (every type word can handle) </param>
187 public void LoadDocument( string t_filename)
188 {
189 deactivateevents = true ;
190 filename = t_filename;
191
192 if (wd == null ) wd = new Word.ApplicationClass();
193 try
194 {
195 wd.CommandBars.AdaptiveMenus = false ;
196 wd.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
197 wd.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
198 wd.DocumentOpen += new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
199 wd.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit);
200
201 }
202 catch { }
203
204 if (document != null )
205 {
206 try
207 {
208 object dummy = null ;
209 wd.Documents.Close( ref dummy, ref dummy, ref dummy);
210 }
211 catch { }
212 }
213
214 if (wordWnd == 0 ) wordWnd = FindWindow( " Opusapp " , null );
215 if (wordWnd != 0 )
216 {
217 SetParent(wordWnd, this .Handle.ToInt32());
218
219 object fileName = filename;
220 object newTemplate = false ;
221 object docType = 0 ;
222 object readOnly = true ;
223 object isVisible = true ;
224 object missing = System.Reflection.Missing.Value;
225
226 try
227 {
228 if (wd == null )
229 {
230 throw new WordInstanceException();
231 }
232
233 if (wd.Documents == null )
234 {
235 throw new DocumentInstanceException();
236 }
237
238 if (wd != null && wd.Documents != null )
239 {
240 document = wd.Documents.Add( ref fileName, ref newTemplate, ref docType, ref isVisible);
241 }
242
243 if (document == null )
244 {
245 throw new ValidDocumentException();
246 }
247 }
248 catch
249 {
250 }
251
252 try
253 {
254 wd.ActiveWindow.DisplayRightRuler = false ;
255 wd.ActiveWindow.DisplayScreenTips = false ;
256 wd.ActiveWindow.DisplayVerticalRuler = false ;
257 wd.ActiveWindow.DisplayRightRuler = false ;
258 wd.ActiveWindow.ActivePane.DisplayRulers = false ;
259 wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView;
260 // wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView; // wdWebView; // .wdNormalView;
261 }
262 catch
263 {
264
265 }
266
267
268 /// Code Added
269 /// Disable the specific buttons of the command bar
270 /// By default, we disable/hide the menu bar
271 /// The New/Open buttons of the command bar are disabled
272 /// Other things can be added as required (and supported ..:) )
273 /// Lots of commented code in here, if somebody needs to disable specific menu or sub-menu items.
274 ///
275 int counter = wd.ActiveWindow.Application.CommandBars.Count;
276 for ( int i = 1 ; i <= counter; i ++ )
277 {
278 try
279 {
280
281 String nm = wd.ActiveWindow.Application.CommandBars[i].Name;
282 if (nm == " Standard " )
283 {
284 // nm=i.ToString()+" "+nm;
285 // MessageBox.Show(nm);
286 int count_control = wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
287 for ( int j = 1 ; j <= 2 ; j ++ )
288 {
289 // MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
290 wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false ;
291
292 }
293 }
294
295 if (nm == " Menu Bar " )
296 {
297 // To disable the menubar, use the following (1) line
298 wd.ActiveWindow.Application.CommandBars[i].Enabled = false ;
299
300 /// If you want to have specific menu or sub-menu items, write the code here.
301 /// Samples commented below
302
303 // MessageBox.Show(nm);
304 // int count_control=wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
305 // MessageBox.Show(count_control.ToString());
306
307 /*
308 for(int j=1;j<=count_control;j++)
309 {
310 /// The following can be used to disable specific menuitems in the menubar
311 /// wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled=false;
312
313 //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
314 //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption);
315 //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].accChildCount.ToString());
316
317
318 ///The following can be used to disable some or all the sub-menuitems in the menubar
319
320
321 Office.CommandBarPopup c;
322 c = (Office.CommandBarPopup)wd.ActiveWindow.Application.CommandBars[i].Controls[j];
323
324 for(int k=1;k<=c.Controls.Count;k++)
325 {
326 //MessageBox.Show(k.ToString()+" "+c.Controls[k].Caption + " -- " + c.Controls[k].DescriptionText + " -- " );
327 try
328 {
329 c.Controls[k].Enabled=false;
330 c.Controls["Close Window"].Enabled=false;
331 }
332 catch
333 {
334
335 }
336 }
337
338
339
340 //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Control Controls[0].Enabled=false;
341 }
342 */
343
344 }
345
346 nm = "" ;
347 }
348 catch (Exception ex)
349 {
350 MessageBox.Show(ex.ToString());
351 }
352 }
353
354
355
356 // Show the word-document
357 try
358 {
359 wd.Visible = true ;
360 // wd.Activate(); // 抛出异常:捕捉到COMException 无法激活应用程序
361
362 SetWindowPos(wordWnd, this .Handle.ToInt32(), 0 , 0 , this .Bounds.Width, this .Bounds.Height, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME | SWP_NOSIZE);
363
364 // Call onresize--I dont want to write the same lines twice
365 OnResize();
366 }
367 catch
368 {
369 MessageBox.Show( " Error: do not load the document into the control until the parent window is shown! " );
370 }
371
372 /// We want to remove the system menu also. The title bar is not visible, but we want to avoid accidental minimize, maximize, etc ..by disabling the system menu(Alt+Space)
373 try
374 {
375 int hMenu = GetSystemMenu(wordWnd, false );
376 if (hMenu > 0 )
377 {
378 int menuItemCount = GetMenuItemCount(hMenu);
379 RemoveMenu(hMenu, menuItemCount - 1 , MF_REMOVE | MF_BYPOSITION);
380 RemoveMenu(hMenu, menuItemCount - 2 , MF_REMOVE | MF_BYPOSITION);
381 RemoveMenu(hMenu, menuItemCount - 3 , MF_REMOVE | MF_BYPOSITION);
382 RemoveMenu(hMenu, menuItemCount - 4 , MF_REMOVE | MF_BYPOSITION);
383 RemoveMenu(hMenu, menuItemCount - 5 , MF_REMOVE | MF_BYPOSITION);
384 RemoveMenu(hMenu, menuItemCount - 6 , MF_REMOVE | MF_BYPOSITION);
385 RemoveMenu(hMenu, menuItemCount - 7 , MF_REMOVE | MF_BYPOSITION);
386 RemoveMenu(hMenu, menuItemCount - 8 , MF_REMOVE | MF_BYPOSITION);
387 DrawMenuBar(wordWnd);
388 }
389 }
390 catch { };
391
392
393
394 this .Parent.Focus();
395
396 }
397 deactivateevents = false ;
398 }
399
400
401 /// <summary>
402 /// restores Word.
403 /// If the program crashed somehow.
404 /// Sometimes Word saves it's temporary settings :(
405 /// </summary>
406 public void RestoreWord()
407 {
408 try
409 {
410 int counter = wd.ActiveWindow.Application.CommandBars.Count;
411 for ( int i = 0 ; i < counter; i ++ )
412 {
413 try
414 {
415 wd.ActiveWindow.Application.CommandBars[i].Enabled = true ;
416 }
417 catch
418 {
419
420 }
421 }
422 }
423 catch { };
424
425 }
426
427 /// <summary>
428 /// internal resize function
429 /// utilizes the size of the surrounding control
430 ///
431 /// optimzed for Word2000 but it works pretty good with WordXP too.
432 /// </summary>
433 /// <param name="sender"></param>
434 /// <param name="e"></param>
435 private void OnResize()
436 {
437 // The original one that I used is shown below. Shows the complete window, but its buttons (min, max, restore) are disabled
438 /// / MoveWindow(wordWnd,0,0,this.Bounds.Width,this.Bounds.Height,true);
439
440
441 /// Change below
442 /// The following one is better, if it works for you. We donot need the title bar any way. Based on a suggestion.
443 int borderWidth = SystemInformation.Border3DSize.Width;
444 int borderHeight = SystemInformation.Border3DSize.Height;
445 int captionHeight = SystemInformation.CaptionHeight;
446 int statusHeight = SystemInformation.ToolWindowCaptionHeight;
447 MoveWindow(
448 wordWnd,
449 - 2 * borderWidth,
450 - 2 * borderHeight - captionHeight,
451 this .Bounds.Width + 4 * borderWidth,
452 this .Bounds.Height + captionHeight + 4 * borderHeight + statusHeight,
453 true );
454
455 }
456
457 private void OnResize( object sender, System.EventArgs e)
458 {
459 OnResize();
460 }
461
462
463 /// Required.
464 /// Without this, the command bar buttons that have been disabled
465 /// will remain disabled permanently (does not occur at every machine or every time)
466 public void RestoreCommandBars()
467 {
468 try
469 {
470 int counter = wd.ActiveWindow.Application.CommandBars.Count;
471 for ( int i = 1 ; i <= counter; i ++ )
472 {
473 try
474 {
475
476 String nm = wd.ActiveWindow.Application.CommandBars[i].Name;
477 if (nm == " Standard " )
478 {
479 int count_control = wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
480 for ( int j = 1 ; j <= 2 ; j ++ )
481 {
482 wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = true ;
483 }
484 }
485 if (nm == " Menu Bar " )
486 {
487 wd.ActiveWindow.Application.CommandBars[i].Enabled = true ;
488 }
489 nm = "" ;
490 }
491 catch (Exception ex)
492 {
493 MessageBox.Show(ex.ToString());
494 }
495 }
496 }
497 catch { }
498
499 }
500
501 }
502 public class DocumentInstanceException : Exception
503 { }
504
505 public class ValidDocumentException : Exception
506 { }
507
508 public class WordInstanceException : Exception
509 { }
510
511 }
512
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Runtime.InteropServices;
9
10 namespace WinWordControl
11 {
12 public partial class WinWordControl : UserControl
13 {
14 public WinWordControl()
15 {
16 InitializeComponent();
17
18 }
19
20 #region "API usage declarations"
21
22 [DllImport( " user32.dll " )]
23 public static extern int FindWindow( string strclassName, string strWindowName);
24
25 [DllImport( " user32.dll " )]
26 static extern int SetParent( int hWndChild, int hWndNewParent);
27
28 [DllImport( " user32.dll " , EntryPoint = " SetWindowPos " )]
29 static extern bool SetWindowPos(
30 int hWnd, // handle to window
31 int hWndInsertAfter, // placement-order handle
32 int X, // horizontal position
33 int Y, // vertical position
34 int cx, // width
35 int cy, // height
36 uint uFlags // window-positioning options
37 );
38
39 [DllImport( " user32.dll " , EntryPoint = " MoveWindow " )]
40 static extern bool MoveWindow(
41 int Wnd,
42 int X,
43 int Y,
44 int Width,
45 int Height,
46 bool Repaint
47 );
48
49 [DllImport( " user32.dll " , EntryPoint = " DrawMenuBar " )]
50 static extern Int32 DrawMenuBar(
51 Int32 hWnd
52 );
53
54 [DllImport( " user32.dll " , EntryPoint = " GetMenuItemCount " )]
55 static extern Int32 GetMenuItemCount(
56 Int32 hMenu
57 );
58
59 [DllImport( " user32.dll " , EntryPoint = " GetSystemMenu " )]
60 static extern Int32 GetSystemMenu(
61 Int32 hWnd,
62 bool Revert
63 );
64
65 [DllImport( " user32.dll " , EntryPoint = " RemoveMenu " )]
66 static extern Int32 RemoveMenu(
67 Int32 hMenu,
68 Int32 nPosition,
69 Int32 wFlags
70 );
71
72
73 private const int MF_BYPOSITION = 0x400 ;
74 private const int MF_REMOVE = 0x1000 ;
75
76
77 const int SWP_DRAWFRAME = 0x20 ;
78 const int SWP_NOMOVE = 0x2 ;
79 const int SWP_NOSIZE = 0x1 ;
80 const int SWP_NOZORDER = 0x4 ;
81
82 #endregion
83
84 public Word.Document document;
85 public static Word.ApplicationClass wd = null ;
86 public static int wordWnd = 0 ;
87 public static string filename = null ;
88 private static bool deactivateevents = false ;
89
90 /// <summary>
91 /// needed designer variable
92 /// </summary>
93 // private System.ComponentModel.Container components = null;
94
95 /// <summary>
96 /// Preactivation
97 /// It's usefull, if you need more speed in the main Program
98 /// so you can preload Word.
99 /// </summary>
100 public void PreActivate()
101 {
102 if (wd == null ) wd = new Word.ApplicationClass();
103 }
104
105 /// <summary>
106 /// Close the current Document in the control --> you can
107 /// load a new one with LoadDocument
108 /// </summary>
109 public void CloseControl()
110 {
111 /*
112 * this code is to reopen Word.
113 */
114
115 try
116 {
117 deactivateevents = true ;
118 object dummy = null ;
119 object dummy2 = ( object ) false ;
120 document.Close( ref dummy, ref dummy, ref dummy);
121 // Change the line below.
122 wd.Quit( ref dummy2, ref dummy, ref dummy);
123 deactivateevents = false ;
124 }
125 catch (Exception ex)
126 {
127 String strErr = ex.Message;
128 }
129 }
130
131
132 /// <summary>
133 /// catches Word's close event
134 /// starts a Thread that send a ESC to the word window ;)
135 /// </summary>
136 /// <param name="doc"></param>
137 /// <param name="test"></param>
138 private void OnClose(Word.Document doc, ref bool cancel)
139 {
140 if ( ! deactivateevents)
141 {
142 cancel = true ;
143 }
144 }
145
146 /// <summary>
147 /// catches Word's open event
148 /// just close
149 /// </summary>
150 /// <param name="doc"></param>
151 private void OnOpenDoc(Word.Document doc)
152 {
153 OnNewDoc(doc);
154 }
155
156 /// <summary>
157 /// catches Word's newdocument event
158 /// just close
159 /// </summary>
160 /// <param name="doc"></param>
161 private void OnNewDoc(Word.Document doc)
162 {
163 if ( ! deactivateevents)
164 {
165 deactivateevents = true ;
166 object dummy = null ;
167 doc.Close( ref dummy, ref dummy, ref dummy);
168 deactivateevents = false ;
169 }
170 }
171
172 /// <summary>
173 /// catches Word's quit event
174 /// normally it should not fire, but just to be shure
175 /// safely release the internal Word Instance
176 /// </summary>
177 private void OnQuit()
178 {
179 // wd=null;
180 }
181
182
183 /// <summary>
184 /// Loads a document into the control
185 /// </summary>
186 /// <param name="t_filename"> path to the file (every type word can handle) </param>
187 public void LoadDocument( string t_filename)
188 {
189 deactivateevents = true ;
190 filename = t_filename;
191
192 if (wd == null ) wd = new Word.ApplicationClass();
193 try
194 {
195 wd.CommandBars.AdaptiveMenus = false ;
196 wd.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
197 wd.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
198 wd.DocumentOpen += new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
199 wd.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit);
200
201 }
202 catch { }
203
204 if (document != null )
205 {
206 try
207 {
208 object dummy = null ;
209 wd.Documents.Close( ref dummy, ref dummy, ref dummy);
210 }
211 catch { }
212 }
213
214 if (wordWnd == 0 ) wordWnd = FindWindow( " Opusapp " , null );
215 if (wordWnd != 0 )
216 {
217 SetParent(wordWnd, this .Handle.ToInt32());
218
219 object fileName = filename;
220 object newTemplate = false ;
221 object docType = 0 ;
222 object readOnly = true ;
223 object isVisible = true ;
224 object missing = System.Reflection.Missing.Value;
225
226 try
227 {
228 if (wd == null )
229 {
230 throw new WordInstanceException();
231 }
232
233 if (wd.Documents == null )
234 {
235 throw new DocumentInstanceException();
236 }
237
238 if (wd != null && wd.Documents != null )
239 {
240 document = wd.Documents.Add( ref fileName, ref newTemplate, ref docType, ref isVisible);
241 }
242
243 if (document == null )
244 {
245 throw new ValidDocumentException();
246 }
247 }
248 catch
249 {
250 }
251
252 try
253 {
254 wd.ActiveWindow.DisplayRightRuler = false ;
255 wd.ActiveWindow.DisplayScreenTips = false ;
256 wd.ActiveWindow.DisplayVerticalRuler = false ;
257 wd.ActiveWindow.DisplayRightRuler = false ;
258 wd.ActiveWindow.ActivePane.DisplayRulers = false ;
259 wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView;
260 // wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView; // wdWebView; // .wdNormalView;
261 }
262 catch
263 {
264
265 }
266
267
268 /// Code Added
269 /// Disable the specific buttons of the command bar
270 /// By default, we disable/hide the menu bar
271 /// The New/Open buttons of the command bar are disabled
272 /// Other things can be added as required (and supported ..:) )
273 /// Lots of commented code in here, if somebody needs to disable specific menu or sub-menu items.
274 ///
275 int counter = wd.ActiveWindow.Application.CommandBars.Count;
276 for ( int i = 1 ; i <= counter; i ++ )
277 {
278 try
279 {
280
281 String nm = wd.ActiveWindow.Application.CommandBars[i].Name;
282 if (nm == " Standard " )
283 {
284 // nm=i.ToString()+" "+nm;
285 // MessageBox.Show(nm);
286 int count_control = wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
287 for ( int j = 1 ; j <= 2 ; j ++ )
288 {
289 // MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
290 wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false ;
291
292 }
293 }
294
295 if (nm == " Menu Bar " )
296 {
297 // To disable the menubar, use the following (1) line
298 wd.ActiveWindow.Application.CommandBars[i].Enabled = false ;
299
300 /// If you want to have specific menu or sub-menu items, write the code here.
301 /// Samples commented below
302
303 // MessageBox.Show(nm);
304 // int count_control=wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
305 // MessageBox.Show(count_control.ToString());
306
307 /*
308 for(int j=1;j<=count_control;j++)
309 {
310 /// The following can be used to disable specific menuitems in the menubar
311 /// wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled=false;
312
313 //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].ToString());
314 //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].Caption);
315 //MessageBox.Show(wd.ActiveWindow.Application.CommandBars[i].Controls[j].accChildCount.ToString());
316
317
318 ///The following can be used to disable some or all the sub-menuitems in the menubar
319
320
321 Office.CommandBarPopup c;
322 c = (Office.CommandBarPopup)wd.ActiveWindow.Application.CommandBars[i].Controls[j];
323
324 for(int k=1;k<=c.Controls.Count;k++)
325 {
326 //MessageBox.Show(k.ToString()+" "+c.Controls[k].Caption + " -- " + c.Controls[k].DescriptionText + " -- " );
327 try
328 {
329 c.Controls[k].Enabled=false;
330 c.Controls["Close Window"].Enabled=false;
331 }
332 catch
333 {
334
335 }
336 }
337
338
339
340 //wd.ActiveWindow.Application.CommandBars[i].Controls[j].Control Controls[0].Enabled=false;
341 }
342 */
343
344 }
345
346 nm = "" ;
347 }
348 catch (Exception ex)
349 {
350 MessageBox.Show(ex.ToString());
351 }
352 }
353
354
355
356 // Show the word-document
357 try
358 {
359 wd.Visible = true ;
360 // wd.Activate(); // 抛出异常:捕捉到COMException 无法激活应用程序
361
362 SetWindowPos(wordWnd, this .Handle.ToInt32(), 0 , 0 , this .Bounds.Width, this .Bounds.Height, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME | SWP_NOSIZE);
363
364 // Call onresize--I dont want to write the same lines twice
365 OnResize();
366 }
367 catch
368 {
369 MessageBox.Show( " Error: do not load the document into the control until the parent window is shown! " );
370 }
371
372 /// We want to remove the system menu also. The title bar is not visible, but we want to avoid accidental minimize, maximize, etc ..by disabling the system menu(Alt+Space)
373 try
374 {
375 int hMenu = GetSystemMenu(wordWnd, false );
376 if (hMenu > 0 )
377 {
378 int menuItemCount = GetMenuItemCount(hMenu);
379 RemoveMenu(hMenu, menuItemCount - 1 , MF_REMOVE | MF_BYPOSITION);
380 RemoveMenu(hMenu, menuItemCount - 2 , MF_REMOVE | MF_BYPOSITION);
381 RemoveMenu(hMenu, menuItemCount - 3 , MF_REMOVE | MF_BYPOSITION);
382 RemoveMenu(hMenu, menuItemCount - 4 , MF_REMOVE | MF_BYPOSITION);
383 RemoveMenu(hMenu, menuItemCount - 5 , MF_REMOVE | MF_BYPOSITION);
384 RemoveMenu(hMenu, menuItemCount - 6 , MF_REMOVE | MF_BYPOSITION);
385 RemoveMenu(hMenu, menuItemCount - 7 , MF_REMOVE | MF_BYPOSITION);
386 RemoveMenu(hMenu, menuItemCount - 8 , MF_REMOVE | MF_BYPOSITION);
387 DrawMenuBar(wordWnd);
388 }
389 }
390 catch { };
391
392
393
394 this .Parent.Focus();
395
396 }
397 deactivateevents = false ;
398 }
399
400
401 /// <summary>
402 /// restores Word.
403 /// If the program crashed somehow.
404 /// Sometimes Word saves it's temporary settings :(
405 /// </summary>
406 public void RestoreWord()
407 {
408 try
409 {
410 int counter = wd.ActiveWindow.Application.CommandBars.Count;
411 for ( int i = 0 ; i < counter; i ++ )
412 {
413 try
414 {
415 wd.ActiveWindow.Application.CommandBars[i].Enabled = true ;
416 }
417 catch
418 {
419
420 }
421 }
422 }
423 catch { };
424
425 }
426
427 /// <summary>
428 /// internal resize function
429 /// utilizes the size of the surrounding control
430 ///
431 /// optimzed for Word2000 but it works pretty good with WordXP too.
432 /// </summary>
433 /// <param name="sender"></param>
434 /// <param name="e"></param>
435 private void OnResize()
436 {
437 // The original one that I used is shown below. Shows the complete window, but its buttons (min, max, restore) are disabled
438 /// / MoveWindow(wordWnd,0,0,this.Bounds.Width,this.Bounds.Height,true);
439
440
441 /// Change below
442 /// The following one is better, if it works for you. We donot need the title bar any way. Based on a suggestion.
443 int borderWidth = SystemInformation.Border3DSize.Width;
444 int borderHeight = SystemInformation.Border3DSize.Height;
445 int captionHeight = SystemInformation.CaptionHeight;
446 int statusHeight = SystemInformation.ToolWindowCaptionHeight;
447 MoveWindow(
448 wordWnd,
449 - 2 * borderWidth,
450 - 2 * borderHeight - captionHeight,
451 this .Bounds.Width + 4 * borderWidth,
452 this .Bounds.Height + captionHeight + 4 * borderHeight + statusHeight,
453 true );
454
455 }
456
457 private void OnResize( object sender, System.EventArgs e)
458 {
459 OnResize();
460 }
461
462
463 /// Required.
464 /// Without this, the command bar buttons that have been disabled
465 /// will remain disabled permanently (does not occur at every machine or every time)
466 public void RestoreCommandBars()
467 {
468 try
469 {
470 int counter = wd.ActiveWindow.Application.CommandBars.Count;
471 for ( int i = 1 ; i <= counter; i ++ )
472 {
473 try
474 {
475
476 String nm = wd.ActiveWindow.Application.CommandBars[i].Name;
477 if (nm == " Standard " )
478 {
479 int count_control = wd.ActiveWindow.Application.CommandBars[i].Controls.Count;
480 for ( int j = 1 ; j <= 2 ; j ++ )
481 {
482 wd.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = true ;
483 }
484 }
485 if (nm == " Menu Bar " )
486 {
487 wd.ActiveWindow.Application.CommandBars[i].Enabled = true ;
488 }
489 nm = "" ;
490 }
491 catch (Exception ex)
492 {
493 MessageBox.Show(ex.ToString());
494 }
495 }
496 }
497 catch { }
498
499 }
500
501 }
502 public class DocumentInstanceException : Exception
503 { }
504
505 public class ValidDocumentException : Exception
506 { }
507
508 public class WordInstanceException : Exception
509 { }
510
511 }
512