frmMain.cs
上传用户:zili2008
上传日期:2020-12-03
资源大小:477k
文件大小:18k
源码类别:

编辑框

开发平台:

Visual Basic

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace WordInDOTNET
  8. {
  9. /// <summary>
  10. /// Summary description for Form1.
  11. /// </summary>
  12. public class frmMain : System.Windows.Forms.Form
  13. {
  14. private System.Windows.Forms.Button button1;
  15. private WinWordControl.WinWordControl objWinWordControl;
  16. private System.Windows.Forms.Label label1;
  17. private System.Windows.Forms.Label label2;
  18. private System.Windows.Forms.TextBox txtClientName;
  19. private System.Windows.Forms.TextBox txtRecNo;
  20. private System.Windows.Forms.Button btnClientName;
  21. private System.Windows.Forms.Button btnRecNo;
  22. private System.Windows.Forms.Button btnCurrentDate;
  23. private System.Windows.Forms.Button btnMarkError;
  24. private System.Windows.Forms.Button btnRemoveError;
  25. private System.Windows.Forms.Button btnSaveDocument;
  26. private System.Windows.Forms.Button btnRemoveBookMarks;
  27. private System.Windows.Forms.Button btnPrintView;
  28. private System.Windows.Forms.Button btnNormalView;
  29. private System.Windows.Forms.Button btnWebView;
  30. private System.Windows.Forms.Button btnShowMenuBar;
  31. private System.Windows.Forms.OpenFileDialog openFileDialog1;
  32. /// <summary>
  33. /// Required designer variable.
  34. /// </summary>
  35. private System.ComponentModel.Container components = null;
  36. public frmMain()
  37. {
  38. //
  39. // Required for Windows Form Designer support
  40. //
  41. InitializeComponent();
  42. //
  43. // TODO: Add any constructor code after InitializeComponent call
  44. //
  45. }
  46. /// <summary>
  47. /// Clean up any resources being used.
  48. /// </summary>
  49. protected override void Dispose( bool disposing )
  50. {
  51. if( disposing )
  52. {
  53. if (components != null) 
  54. {
  55. components.Dispose();
  56. }
  57. }
  58. base.Dispose( disposing );
  59. }
  60. #region Windows Form Designer generated code
  61. /// <summary>
  62. /// Required method for Designer support - do not modify
  63. /// the contents of this method with the code editor.
  64. /// </summary>
  65. private void InitializeComponent()
  66. {
  67. this.button1 = new System.Windows.Forms.Button();
  68. this.objWinWordControl = new WinWordControl.WinWordControl();
  69. this.label1 = new System.Windows.Forms.Label();
  70. this.label2 = new System.Windows.Forms.Label();
  71. this.txtClientName = new System.Windows.Forms.TextBox();
  72. this.txtRecNo = new System.Windows.Forms.TextBox();
  73. this.btnClientName = new System.Windows.Forms.Button();
  74. this.btnRecNo = new System.Windows.Forms.Button();
  75. this.btnCurrentDate = new System.Windows.Forms.Button();
  76. this.btnMarkError = new System.Windows.Forms.Button();
  77. this.btnRemoveError = new System.Windows.Forms.Button();
  78. this.btnSaveDocument = new System.Windows.Forms.Button();
  79. this.btnRemoveBookMarks = new System.Windows.Forms.Button();
  80. this.btnPrintView = new System.Windows.Forms.Button();
  81. this.btnNormalView = new System.Windows.Forms.Button();
  82. this.btnWebView = new System.Windows.Forms.Button();
  83. this.btnShowMenuBar = new System.Windows.Forms.Button();
  84. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  85. this.SuspendLayout();
  86. // 
  87. // button1
  88. // 
  89. this.button1.Location = new System.Drawing.Point(16, 8);
  90. this.button1.Name = "button1";
  91. this.button1.Size = new System.Drawing.Size(96, 23);
  92. this.button1.TabIndex = 1;
  93. this.button1.Text = "Load Document";
  94. this.button1.Click += new System.EventHandler(this.button1_Click);
  95. // 
  96. // objWinWordControl
  97. // 
  98. this.objWinWordControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  99. | System.Windows.Forms.AnchorStyles.Left) 
  100. | System.Windows.Forms.AnchorStyles.Right)));
  101. this.objWinWordControl.BackColor = System.Drawing.SystemColors.ActiveCaption;
  102. this.objWinWordControl.Location = new System.Drawing.Point(168, 0);
  103. this.objWinWordControl.Name = "objWinWordControl";
  104. this.objWinWordControl.Size = new System.Drawing.Size(544, 517);
  105. this.objWinWordControl.TabIndex = 3;
  106. // 
  107. // label1
  108. // 
  109. this.label1.Location = new System.Drawing.Point(8, 48);
  110. this.label1.Name = "label1";
  111. this.label1.Size = new System.Drawing.Size(80, 16);
  112. this.label1.TabIndex = 4;
  113. this.label1.Text = "Client's Name";
  114. // 
  115. // label2
  116. // 
  117. this.label2.Location = new System.Drawing.Point(8, 104);
  118. this.label2.Name = "label2";
  119. this.label2.Size = new System.Drawing.Size(88, 16);
  120. this.label2.TabIndex = 5;
  121. this.label2.Text = "Record Number";
  122. // 
  123. // txtClientName
  124. // 
  125. this.txtClientName.Location = new System.Drawing.Point(16, 64);
  126. this.txtClientName.Name = "txtClientName";
  127. this.txtClientName.TabIndex = 6;
  128. this.txtClientName.Text = "Some Name";
  129. // 
  130. // txtRecNo
  131. // 
  132. this.txtRecNo.Location = new System.Drawing.Point(16, 120);
  133. this.txtRecNo.Name = "txtRecNo";
  134. this.txtRecNo.TabIndex = 7;
  135. this.txtRecNo.Text = "456789123";
  136. // 
  137. // btnClientName
  138. // 
  139. this.btnClientName.Location = new System.Drawing.Point(120, 64);
  140. this.btnClientName.Name = "btnClientName";
  141. this.btnClientName.Size = new System.Drawing.Size(32, 16);
  142. this.btnClientName.TabIndex = 9;
  143. this.btnClientName.Text = "-->";
  144. this.btnClientName.Click += new System.EventHandler(this.btnClientName_Click);
  145. // 
  146. // btnRecNo
  147. // 
  148. this.btnRecNo.Location = new System.Drawing.Point(120, 120);
  149. this.btnRecNo.Name = "btnRecNo";
  150. this.btnRecNo.Size = new System.Drawing.Size(32, 16);
  151. this.btnRecNo.TabIndex = 10;
  152. this.btnRecNo.Text = "-->";
  153. this.btnRecNo.Click += new System.EventHandler(this.btnRecNo_Click);
  154. // 
  155. // btnCurrentDate
  156. // 
  157. this.btnCurrentDate.Location = new System.Drawing.Point(8, 160);
  158. this.btnCurrentDate.Name = "btnCurrentDate";
  159. this.btnCurrentDate.Size = new System.Drawing.Size(128, 24);
  160. this.btnCurrentDate.TabIndex = 11;
  161. this.btnCurrentDate.Text = "Insert Current Date";
  162. this.btnCurrentDate.Click += new System.EventHandler(this.btnCurrentDate_Click);
  163. // 
  164. // btnMarkError
  165. // 
  166. this.btnMarkError.Location = new System.Drawing.Point(32, 200);
  167. this.btnMarkError.Name = "btnMarkError";
  168. this.btnMarkError.TabIndex = 12;
  169. this.btnMarkError.Text = "Mark Error";
  170. this.btnMarkError.Click += new System.EventHandler(this.btnMarkError_Click);
  171. // 
  172. // btnRemoveError
  173. // 
  174. this.btnRemoveError.Location = new System.Drawing.Point(8, 240);
  175. this.btnRemoveError.Name = "btnRemoveError";
  176. this.btnRemoveError.Size = new System.Drawing.Size(152, 23);
  177. this.btnRemoveError.TabIndex = 15;
  178. this.btnRemoveError.Text = "Remove Error Highlighting";
  179. this.btnRemoveError.Click += new System.EventHandler(this.btnRemoveError_Click);
  180. // 
  181. // btnSaveDocument
  182. // 
  183. this.btnSaveDocument.Location = new System.Drawing.Point(24, 320);
  184. this.btnSaveDocument.Name = "btnSaveDocument";
  185. this.btnSaveDocument.Size = new System.Drawing.Size(112, 23);
  186. this.btnSaveDocument.TabIndex = 16;
  187. this.btnSaveDocument.Text = "Save Document";
  188. this.btnSaveDocument.Click += new System.EventHandler(this.btnSaveDocument_Click);
  189. // 
  190. // btnRemoveBookMarks
  191. // 
  192. this.btnRemoveBookMarks.Location = new System.Drawing.Point(8, 280);
  193. this.btnRemoveBookMarks.Name = "btnRemoveBookMarks";
  194. this.btnRemoveBookMarks.Size = new System.Drawing.Size(152, 23);
  195. this.btnRemoveBookMarks.TabIndex = 17;
  196. this.btnRemoveBookMarks.Text = "Remove bookmarks";
  197. this.btnRemoveBookMarks.Click += new System.EventHandler(this.btnRemoveBookMarks_Click);
  198. // 
  199. // btnPrintView
  200. // 
  201. this.btnPrintView.Location = new System.Drawing.Point(24, 368);
  202. this.btnPrintView.Name = "btnPrintView";
  203. this.btnPrintView.Size = new System.Drawing.Size(120, 23);
  204. this.btnPrintView.TabIndex = 18;
  205. this.btnPrintView.Text = "Print Layout View";
  206. this.btnPrintView.Click += new System.EventHandler(this.btnPrintView_Click);
  207. // 
  208. // btnNormalView
  209. // 
  210. this.btnNormalView.Location = new System.Drawing.Point(24, 400);
  211. this.btnNormalView.Name = "btnNormalView";
  212. this.btnNormalView.Size = new System.Drawing.Size(120, 23);
  213. this.btnNormalView.TabIndex = 19;
  214. this.btnNormalView.Text = "Normal View";
  215. this.btnNormalView.Click += new System.EventHandler(this.btnNormalView_Click);
  216. // 
  217. // btnWebView
  218. // 
  219. this.btnWebView.Location = new System.Drawing.Point(24, 432);
  220. this.btnWebView.Name = "btnWebView";
  221. this.btnWebView.Size = new System.Drawing.Size(120, 23);
  222. this.btnWebView.TabIndex = 20;
  223. this.btnWebView.Text = "Web View";
  224. this.btnWebView.Click += new System.EventHandler(this.btnWebView_Click);
  225. // 
  226. // btnShowMenuBar
  227. // 
  228. this.btnShowMenuBar.Location = new System.Drawing.Point(16, 472);
  229. this.btnShowMenuBar.Name = "btnShowMenuBar";
  230. this.btnShowMenuBar.Size = new System.Drawing.Size(136, 23);
  231. this.btnShowMenuBar.TabIndex = 21;
  232. this.btnShowMenuBar.Text = "Show MenuBar";
  233. this.btnShowMenuBar.Click += new System.EventHandler(this.btnShowMenuBar_Click);
  234. // 
  235. // frmMain
  236. // 
  237. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  238. this.ClientSize = new System.Drawing.Size(712, 517);
  239. this.Controls.Add(this.btnShowMenuBar);
  240. this.Controls.Add(this.btnWebView);
  241. this.Controls.Add(this.btnNormalView);
  242. this.Controls.Add(this.btnPrintView);
  243. this.Controls.Add(this.btnRemoveBookMarks);
  244. this.Controls.Add(this.btnSaveDocument);
  245. this.Controls.Add(this.btnRemoveError);
  246. this.Controls.Add(this.btnMarkError);
  247. this.Controls.Add(this.btnCurrentDate);
  248. this.Controls.Add(this.btnRecNo);
  249. this.Controls.Add(this.btnClientName);
  250. this.Controls.Add(this.txtRecNo);
  251. this.Controls.Add(this.txtClientName);
  252. this.Controls.Add(this.label2);
  253. this.Controls.Add(this.label1);
  254. this.Controls.Add(this.objWinWordControl);
  255. this.Controls.Add(this.button1);
  256. this.Name = "frmMain";
  257. this.Text = "Main";
  258. this.Closing += new System.ComponentModel.CancelEventHandler(this.frmMain_Closing);
  259. this.Load += new System.EventHandler(this.frmMain_Load);
  260. this.ResumeLayout(false);
  261. }
  262. #endregion
  263. /// <summary>
  264. /// The main entry point for the application.
  265. /// </summary>
  266. [STAThread]
  267. static void Main() 
  268. {
  269. Application.Run(new frmMain());
  270. }
  271. private void frmMain_Load(object sender, System.EventArgs e)
  272. {
  273. }
  274. /// <summary>
  275. /// Loads the document. If it is already loaded, it will first unload and load again.
  276. /// </summary>
  277. /// <param name="sender"></param>
  278. /// <param name="e"></param>
  279. private void button1_Click(object sender, System.EventArgs e)
  280. {
  281. string filNm;
  282. openFileDialog1.Multiselect = false;
  283. openFileDialog1.Filter = "MS-Word Files (*.doc,*.dot) | *.doc;*.dot";
  284. if(openFileDialog1.ShowDialog() == DialogResult.OK)
  285. {
  286.             filNm = openFileDialog1.FileName;
  287. }
  288. else return;
  289. MessageBox.Show("Please wait while the document is being displayed");
  290. try
  291. {
  292. objWinWordControl.CloseControl();
  293. }
  294. catch{}
  295. finally
  296. objWinWordControl.document=null;
  297. WinWordControl.WinWordControl.wd=null;
  298. WinWordControl.WinWordControl.wordWnd=0;
  299. }
  300. try
  301. {
  302. //Load the template used for testing.
  303. objWinWordControl.LoadDocument(filNm);
  304. }
  305. catch(Exception ex){String err = ex.Message;}
  306. btnClientName.Enabled=true;
  307. btnRecNo.Enabled=true;
  308. btnCurrentDate.Enabled=true;
  309. }
  310. private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  311. {
  312. objWinWordControl.RestoreCommandBars();
  313. objWinWordControl.CloseControl();
  314. }
  315. /// <summary>
  316. /// Searches the bookmark by name and places text at the text
  317. /// </summary>
  318. /// <param name="BookMarkName"></param>
  319. /// <param name="BookMarkText"></param>
  320.  
  321. private void WriteToBookMark(string BookMarkName, string BookMarkText)
  322. {
  323. try
  324. {
  325. Word.Document wd = objWinWordControl.document;
  326. Word.Application wa = wd.Application;
  327. int bookmark_cnt = wd.Bookmarks.Count;
  328. int i;
  329. for(i=1;i<=bookmark_cnt;i++)
  330. {
  331. object o = (object)i;
  332. if(BookMarkName.ToLower().Trim() == wd.Bookmarks.Item(ref o).Name.ToLower().Trim())
  333. {
  334. wd.Bookmarks.Item(ref o).Select();
  335. wa.Selection.TypeText(BookMarkText);
  336. }
  337. }
  338. }
  339. catch(Exception ex)
  340. {
  341. String err = ex.Message;
  342. }
  343. }
  344. /// <summary>
  345. /// Custom: Writes the client name at the bookmark.
  346. /// This template is pre-defined with 3 bookmarks.
  347. /// </summary>
  348. /// <param name="sender"></param>
  349. /// <param name="e"></param>
  350. private void btnClientName_Click(object sender, System.EventArgs e)
  351. {
  352. WriteToBookMark("bmrkClientName",txtClientName.Text);
  353. btnClientName.Enabled=false;
  354. }
  355. /// <summary>
  356. /// Custom: Writes the client name at the bookmark.
  357. /// This template is pre-defined with 3 bookmarks.
  358. /// </summary>
  359. /// <param name="sender"></param>
  360. /// <param name="e"></param>
  361. private void btnRecNo_Click(object sender, System.EventArgs e)
  362. {
  363. WriteToBookMark("bmrkRecNo",txtRecNo.Text);
  364. btnRecNo.Enabled=false;
  365. }
  366. /// <summary>
  367. /// Custom: Writes the client name at the bookmark.
  368. /// This template is pre-defined with 3 bookmarks.
  369. /// </summary>
  370. /// <param name="sender"></param>
  371. /// <param name="e"></param>
  372. private void btnCurrentDate_Click(object sender, System.EventArgs e)
  373. {
  374. WriteToBookMark("bmrkCurrentDate",DateTime.Now.ToString());
  375. btnCurrentDate.Enabled=false;
  376. }
  377. /// <summary>
  378. /// Mark the error by selecting some text
  379. /// </summary>
  380. /// <param name="sender"></param>
  381. /// <param name="e"></param>
  382. private void btnMarkError_Click(object sender, System.EventArgs e)
  383. {
  384. try
  385. {
  386. string strText = objWinWordControl.document.Application.Selection.Text;
  387. frmMarkError f= new frmMarkError();
  388. f.txtChanged.Text = strText;
  389. f.txtOriginal.Text = strText;
  390. DialogResult dr= f.ShowDialog();
  391. if(dr==DialogResult.OK)
  392. {
  393. objWinWordControl.document.Application.Selection.Text=f.txtChanged.Text;
  394. objWinWordControl.document.Application.Selection.FormattedText.HighlightColorIndex=Word.WdColorIndex.wdYellow;
  395. 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');
  396. object o = objWinWordControl.document.Application.Selection.Range;
  397. objWinWordControl.document.Bookmarks.Add(bkmrkname,ref o);
  398. objWinWordControl.document.Application.Selection.FormattedText.HighlightColorIndex=Word.WdColorIndex.wdYellow;
  399. }
  400. // Do something else like making entry to database.
  401. }
  402. catch{}
  403. }
  404. /// <summary>
  405. /// Remove the Error bookmarks
  406. /// This might be required if the document is being sent for further quality check.
  407. /// In that case, the errors marked by current QA must not be shown to next QA
  408. /// </summary>
  409. /// <param name="sender"></param>
  410. /// <param name="e"></param>
  411. private void btnRemoveError_Click(object sender, System.EventArgs e)
  412. {
  413. try
  414. {
  415. string BookMarkName="";
  416. Word.Document wd = objWinWordControl.document;
  417. Word.Application wa = wd.Application;
  418. int bookmark_cnt = wd.Bookmarks.Count;
  419. int i;
  420. for(i=1;i<=bookmark_cnt;i++)
  421. {
  422. object o = (object)i;
  423. BookMarkName=wd.Bookmarks.Item(ref o).Name;
  424. if(BookMarkName.Substring(0,10)=="bkmrk_err_")
  425. {
  426. wd.Bookmarks.Item(ref o).Select();
  427. wa.Selection.FormattedText.HighlightColorIndex = Word.WdColorIndex.wdNoHighlight;
  428. }
  429. }
  430. //Do something else
  431. }
  432. catch(Exception ex)
  433. {
  434. String err = ex.Message;
  435. }
  436. }
  437. /// <summary>
  438. /// Save the document. Calls the Word's save method
  439. /// </summary>
  440. /// <param name="sender"></param>
  441. /// <param name="e"></param>
  442. private void btnSaveDocument_Click(object sender, System.EventArgs e)
  443. {
  444. try
  445. {
  446. objWinWordControl.document.Save();
  447. }
  448. catch
  449. {}
  450. }
  451. /// <summary>
  452. /// Clear all the bookmarks. 
  453. /// This may be required before submitting the transcript to the client.
  454. /// </summary>
  455. /// <param name="sender"></param>
  456. /// <param name="e"></param>
  457. private void btnRemoveBookMarks_Click(object sender, System.EventArgs e)
  458. {
  459. try
  460. {
  461. Word.Document wd = objWinWordControl.document;
  462. Word.Application wa = wd.Application;
  463. int bookmark_cnt = wd.Bookmarks.Count;
  464. int i;
  465. for(i=1;i<=bookmark_cnt;i++)
  466. {
  467. object o = (object)wd.Bookmarks.Count;
  468. wd.Bookmarks.Item(ref o).Delete();
  469. }
  470. MessageBox.Show("Bookmarks removed");
  471. }
  472. catch(Exception ex)
  473. {
  474. String err = ex.Message;
  475. MessageBox.Show("Error occured while removing bookmarks");
  476. }
  477. }
  478. /// <summary>
  479. /// Change the view
  480. /// </summary>
  481. /// <param name="sender"></param>
  482. /// <param name="e"></param>
  483. private void btnPrintView_Click(object sender, System.EventArgs e)
  484. {
  485. try
  486. {
  487. objWinWordControl.document.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
  488. }
  489. catch{}
  490. }
  491. /// <summary>
  492. /// Change the view
  493. /// </summary>
  494. /// <param name="sender"></param>
  495. /// <param name="e"></param>
  496. private void btnNormalView_Click(object sender, System.EventArgs e)
  497. {
  498. try
  499. {
  500. objWinWordControl.document.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdNormalView;
  501. }
  502. catch{}
  503. }
  504. /// <summary>
  505. /// Change the view
  506. /// </summary>
  507. /// <param name="sender"></param>
  508. /// <param name="e"></param>
  509. private void btnWebView_Click(object sender, System.EventArgs e)
  510. {
  511. try
  512. {
  513. objWinWordControl.document.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView;
  514. }
  515. catch{}
  516. }
  517. /// <summary>
  518. /// If you want to show the menubar to the user. 
  519. /// (Useful in cases where too many functionalities of word are being used)
  520. /// </summary>
  521. /// <param name="sender"></param>
  522. /// <param name="e"></param>
  523. private void btnShowMenuBar_Click(object sender, System.EventArgs e)
  524. {
  525. try
  526. {
  527. objWinWordControl.document.ActiveWindow.Application.CommandBars["Menu Bar"].Enabled=true;
  528. }
  529. catch{}
  530. }
  531. }
  532. }
  533. /*
  534.  
  535.  
  536. */