实例介绍
核心源码:
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WordInDOTNET { /// <summary> /// Summary description for Form1. /// </summary> public class frmMain : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private WinWordControl.WinWordControl objWinWordControl; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtClientName; private System.Windows.Forms.TextBox txtRecNo; private System.Windows.Forms.Button btnClientName; private System.Windows.Forms.Button btnRecNo; private System.Windows.Forms.Button btnCurrentDate; private System.Windows.Forms.Button btnMarkError; private System.Windows.Forms.Button btnRemoveError; private System.Windows.Forms.Button btnSaveDocument; private System.Windows.Forms.Button btnRemoveBookMarks; private System.Windows.Forms.Button btnPrintView; private System.Windows.Forms.Button btnNormalView; private System.Windows.Forms.Button btnWebView; private System.Windows.Forms.Button btnShowMenuBar; private System.Windows.Forms.OpenFileDialog openFileDialog1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null ; public frmMain() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if ( disposing ) { if (components != null ) { components.Dispose(); } } base .Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this .button1 = new System.Windows.Forms.Button(); this .objWinWordControl = new WinWordControl.WinWordControl(); this .label1 = new System.Windows.Forms.Label(); this .label2 = new System.Windows.Forms.Label(); this .txtClientName = new System.Windows.Forms.TextBox(); this .txtRecNo = new System.Windows.Forms.TextBox(); this .btnClientName = new System.Windows.Forms.Button(); this .btnRecNo = new System.Windows.Forms.Button(); this .btnCurrentDate = new System.Windows.Forms.Button(); this .btnMarkError = new System.Windows.Forms.Button(); this .btnRemoveError = new System.Windows.Forms.Button(); this .btnSaveDocument = new System.Windows.Forms.Button(); this .btnRemoveBookMarks = new System.Windows.Forms.Button(); this .btnPrintView = new System.Windows.Forms.Button(); this .btnNormalView = new System.Windows.Forms.Button(); this .btnWebView = new System.Windows.Forms.Button(); this .btnShowMenuBar = new System.Windows.Forms.Button(); this .openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this .SuspendLayout(); // // button1 // this .button1.Location = new System.Drawing.Point(16, 8); this .button1.Name = "button1" ; this .button1.Size = new System.Drawing.Size(96, 23); this .button1.TabIndex = 1; this .button1.Text = "Load Document" ; this .button1.Click = new System.EventHandler( this .button1_Click); // // objWinWordControl // this .objWinWordControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this .objWinWordControl.BackColor = System.Drawing.SystemColors.ActiveCaption; this .objWinWordControl.Location = new System.Drawing.Point(168, 0); this .objWinWordControl.Name = "objWinWordControl" ; this .objWinWordControl.Size = new System.Drawing.Size(544, 517); this .objWinWordControl.TabIndex = 3; // // label1 // this .label1.Location = new System.Drawing.Point(8, 48); this .label1.Name = "label1" ; this .label1.Size = new System.Drawing.Size(80, 16); this .label1.TabIndex = 4; this .label1.Text = "Client\'s Name" ; // // label2 // this .label2.Location = new System.Drawing.Point(8, 104); this .label2.Name = "label2" ; this .label2.Size = new System.Drawing.Size(88, 16); this .label2.TabIndex = 5; this .label2.Text = "Record Number" ; // // txtClientName // this .txtClientName.Location = new System.Drawing.Point(16, 64); this .txtClientName.Name = "txtClientName" ; this .txtClientName.TabIndex = 6; this .txtClientName.Text = "Some Name" ; // // txtRecNo // this .txtRecNo.Location = new System.Drawing.Point(16, 120); this .txtRecNo.Name = "txtRecNo" ; this .txtRecNo.TabIndex = 7; this .txtRecNo.Text = "456789123" ; // // btnClientName // this .btnClientName.Location = new System.Drawing.Point(120, 64); this .btnClientName.Name = "btnClientName" ; this .btnClientName.Size = new System.Drawing.Size(32, 16); this .btnClientName.TabIndex = 9; this .btnClientName.Text = "-->" ; this .btnClientName.Click = new System.EventHandler( this .btnClientName_Click); // // btnRecNo // this .btnRecNo.Location = new System.Drawing.Point(120, 120); this .btnRecNo.Name = "btnRecNo" ; this .btnRecNo.Size = new System.Drawing.Size(32, 16); this .btnRecNo.TabIndex = 10; this .btnRecNo.Text = "-->" ; this .btnRecNo.Click = new System.EventHandler( this .btnRecNo_Click); // // btnCurrentDate // this .btnCurrentDate.Location = new System.Drawing.Point(8, 160); this .btnCurrentDate.Name = "btnCurrentDate" ; this .btnCurrentDate.Size = new System.Drawing.Size(128, 24); this .btnCurrentDate.TabIndex = 11; this .btnCurrentDate.Text = "Insert Current Date" ; this .btnCurrentDate.Click = new System.EventHandler( this .btnCurrentDate_Click); // // btnMarkError // this .btnMarkError.Location = new System.Drawing.Point(32, 200); this .btnMarkError.Name = "btnMarkError" ; this .btnMarkError.TabIndex = 12; this .btnMarkError.Text = "Mark Error" ; this .btnMarkError.Click = new System.EventHandler( this .btnMarkError_Click); // // btnRemoveError // this .btnRemoveError.Location = new System.Drawing.Point(8, 240); this .btnRemoveError.Name = "btnRemoveError" ; this .btnRemoveError.Size = new System.Drawing.Size(152, 23); this .btnRemoveError.TabIndex = 15; this .btnRemoveError.Text = "Remove Error Highlighting" ; this .btnRemoveError.Click = new System.EventHandler( this .btnRemoveError_Click); // // btnSaveDocument // this .btnSaveDocument.Location = new System.Drawing.Point(24, 320); this .btnSaveDocument.Name = "btnSaveDocument" ; this .btnSaveDocument.Size = new System.Drawing.Size(112, 23); this .btnSaveDocument.TabIndex = 16; this .btnSaveDocument.Text = "Save Document" ; this .btnSaveDocument.Click = new System.EventHandler( this .btnSaveDocument_Click); // // btnRemoveBookMarks // this .btnRemoveBookMarks.Location = new System.Drawing.Point(8, 280); this .btnRemoveBookMarks.Name = "btnRemoveBookMarks" ; this .btnRemoveBookMarks.Size = new System.Drawing.Size(152, 23); this .btnRemoveBookMarks.TabIndex = 17; this .btnRemoveBookMarks.Text = "Remove bookmarks" ; this .btnRemoveBookMarks.Click = new System.EventHandler( this .btnRemoveBookMarks_Click); // // btnPrintView // this .btnPrintView.Location = new System.Drawing.Point(24, 368); this .btnPrintView.Name = "btnPrintView" ; this .btnPrintView.Size = new System.Drawing.Size(120, 23); this .btnPrintView.TabIndex = 18; this .btnPrintView.Text = "Print Layout View" ; this .btnPrintView.Click = new System.EventHandler( this .btnPrintView_Click); // // btnNormalView // this .btnNormalView.Location = new System.Drawing.Point(24, 400); this .btnNormalView.Name = "btnNormalView" ; this .btnNormalView.Size = new System.Drawing.Size(120, 23); this .btnNormalView.TabIndex = 19; this .btnNormalView.Text = "Normal View" ; this .btnNormalView.Click = new System.EventHandler( this .btnNormalView_Click); // // btnWebView // this .btnWebView.Location = new System.Drawing.Point(24, 432); this .btnWebView.Name = "btnWebView" ; this .btnWebView.Size = new System.Drawing.Size(120, 23); this .btnWebView.TabIndex = 20; this .btnWebView.Text = "Web View" ; this .btnWebView.Click = new System.EventHandler( this .btnWebView_Click); // // btnShowMenuBar // this .btnShowMenuBar.Location = new System.Drawing.Point(16, 472); this .btnShowMenuBar.Name = "btnShowMenuBar" ; this .btnShowMenuBar.Size = new System.Drawing.Size(136, 23); this .btnShowMenuBar.TabIndex = 21; this .btnShowMenuBar.Text = "Show MenuBar" ; this .btnShowMenuBar.Click = new System.EventHandler( this .btnShowMenuBar_Click); // // frmMain // this .AutoScaleBaseSize = new System.Drawing.Size(5, 13); this .ClientSize = new System.Drawing.Size(712, 517); this .Controls.Add( this .btnShowMenuBar); this .Controls.Add( this .btnWebView); this .Controls.Add( this .btnNormalView); this .Controls.Add( this .btnPrintView); this .Controls.Add( this .btnRemoveBookMarks); this .Controls.Add( this .btnSaveDocument); this .Controls.Add( this .btnRemoveError); this .Controls.Add( this .btnMarkError); this .Controls.Add( this .btnCurrentDate); this .Controls.Add( this .btnRecNo); this .Controls.Add( this .btnClientName); this .Controls.Add( this .txtRecNo); this .Controls.Add( this .txtClientName); this .Controls.Add( this .label2); this .Controls.Add( this .label1); this .Controls.Add( this .objWinWordControl); this .Controls.Add( this .button1); this .Name = "frmMain" ; this .Text = "Main" ; this .Closing = new System.ComponentModel.CancelEventHandler( this .frmMain_Closing); this .Load = new System.EventHandler( this .frmMain_Load); this .ResumeLayout( false ); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run( new frmMain()); } private void frmMain_Load( object sender, System.EventArgs e) { } /// <summary> /// Loads the document. If it is already loaded, it will first unload and load again. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click( object sender, System.EventArgs e) { string filNm; openFileDialog1.Multiselect = false ; openFileDialog1.Filter = "MS-Word Files (*.doc,*.dot) | *.doc;*.dot" ; if (openFileDialog1.ShowDialog() == DialogResult.OK) { filNm = openFileDialog1.FileName; } else return ; MessageBox.Show( "Please wait while the document is being displayed" ); try { objWinWordControl.CloseControl(); } catch {} finally { objWinWordControl.document= null ; WinWordControl.WinWordControl.wd= null ; WinWordControl.WinWordControl.wordWnd=0; } try { //Load the template used for testing. objWinWordControl.LoadDocument(filNm); } catch (Exception ex){String err = ex.Message;} btnClientName.Enabled= true ; btnRecNo.Enabled= true ; btnCurrentDate.Enabled= true ; } private void frmMain_Closing( object sender, System.ComponentModel.CancelEventArgs e) { objWinWordControl.RestoreCommandBars(); objWinWordControl.CloseControl(); } /// <summary> /// Searches the bookmark by name and places text at the text /// </summary> /// <param name="BookMarkName"></param> /// <param name="BookMarkText"></param> private void WriteToBookMark( string BookMarkName, string BookMarkText) { try { Word.Document wd = objWinWordControl.document; Word.Application wa = wd.Application; int bookmark_cnt = wd.Bookmarks.Count; int i; for (i=1;i<=bookmark_cnt;i ) { object o = ( object )i; if (BookMarkName.ToLower().Trim() == wd.Bookmarks.Item( ref o).Name.ToLower().Trim()) { wd.Bookmarks.Item( ref o).Select(); wa.Selection.TypeText(BookMarkText); } } } catch (Exception ex) { String err = ex.Message; } } /// <summary> /// Custom: Writes the client name at the bookmark. /// This template is pre-defined with 3 bookmarks. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClientName_Click( object sender, System.EventArgs e) { WriteToBookMark( "bmrkClientName" ,txtClientName.Text); btnClientName.Enabled= false ; } /// <summary> /// Custom: Writes the client name at the bookmark. /// This template is pre-defined with 3 bookmarks. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRecNo_Click( object sender, System.EventArgs e) { WriteToBookMark( "bmrkRecNo" ,txtRecNo.Text); btnRecNo.Enabled= false ; } /// <summary> /// Custom: Writes the client name at the bookmark. /// This template is pre-defined with 3 bookmarks. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCurrentDate_Click( object sender, System.EventArgs e) { WriteToBookMark( "bmrkCurrentDate" ,DateTime.Now.ToString()); btnCurrentDate.Enabled= false ; } /// <summary> /// Mark the error by selecting some text /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMarkError_Click( object sender, System.EventArgs e) { try { string strText = objWinWordControl.document.Application.Selection.Text; frmMarkError f= new frmMarkError(); f.txtChanged.Text = strText; f.txtOriginal.Text = strText; DialogResult dr= f.ShowDialog(); if (dr==DialogResult.OK) { objWinWordControl.document.Application.Selection.Text=f.txtChanged.Text; objWinWordControl.document.Application.Selection.FormattedText.HighlightColorIndex=Word.WdColorIndex.wdYellow; string bkmrkname = "bkmrk_err_" DateTime.Now.Day.ToString().PadLeft(2, '0' ) DateTime.Now.Month.ToString().PadLeft(2, '0' ) DateTime.Now.Year.ToString() DateTime.Now.Hour.ToString().PadLeft(2, '0' ) DateTime.Now.Minute.ToString().PadLeft(2, '0' ) DateTime.Now.Second.ToString().PadLeft(2, '0' ) DateTime.Now.Ticks.ToString().PadLeft(8, '0' ); object o = objWinWordControl.document.Application.Selection.Range; objWinWordControl.document.Bookmarks.Add(bkmrkname, ref o); objWinWordControl.document.Application.Selection.FormattedText.HighlightColorIndex=Word.WdColorIndex.wdYellow; } // Do something else like making entry to database. } catch {} } /// <summary> /// Remove the Error bookmarks /// This might be required if the document is being sent for further quality check. /// In that case, the errors marked by current QA must not be shown to next QA /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRemoveError_Click( object sender, System.EventArgs e) { try { string BookMarkName= "" ; Word.Document wd = objWinWordControl.document; Word.Application wa = wd.Application; int bookmark_cnt = wd.Bookmarks.Count; int i; for (i=1;i<=bookmark_cnt;i ) { object o = ( object )i; BookMarkName=wd.Bookmarks.Item( ref o).Name; if (BookMarkName.Substring(0,10)== "bkmrk_err_" ) { wd.Bookmarks.Item( ref o).Select(); wa.Selection.FormattedText.HighlightColorIndex = Word.WdColorIndex.wdNoHighlight; } } //Do something else } catch (Exception ex) { String err = ex.Message; } } /// <summary> /// Save the document. Calls the Word's save method /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSaveDocument_Click( object sender, System.EventArgs e) { try { objWinWordControl.document.Save(); } catch {} } /// <summary> /// Clear all the bookmarks. /// This may be required before submitting the transcript to the client. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRemoveBookMarks_Click( object sender, System.EventArgs e) { try { Word.Document wd = objWinWordControl.document; Word.Application wa = wd.Application; int bookmark_cnt = wd.Bookmarks.Count; int i; for (i=1;i<=bookmark_cnt;i ) { object o = ( object )wd.Bookmarks.Count; wd.Bookmarks.Item( ref o).Delete(); } MessageBox.Show( "Bookmarks removed" ); } catch (Exception ex) { String err = ex.Message; MessageBox.Show( "Error occured while removing bookmarks" ); } } /// <summary> /// Change the view /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrintView_Click( object sender, System.EventArgs e) { try { objWinWordControl.document.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView; } catch {} } /// <summary> /// Change the view /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNormalView_Click( object sender, System.EventArgs e) { try { objWinWordControl.document.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdNormalView; } catch {} } /// <summary> /// Change the view /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnWebView_Click( object sender, System.EventArgs e) { try { objWinWordControl.document.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView; } catch {} } /// <summary> /// If you want to show the menubar to the user. /// (Useful in cases where too many functionalities of word are being used) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnShowMenuBar_Click( object sender, System.EventArgs e) { try { objWinWordControl.document.ActiveWindow.Application.CommandBars[ "Menu Bar" ].Enabled= true ; } catch {} } } } /* */ |
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)