⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 在C#中直接打开Word文件并编辑
💻 CS
📖 第 1 页 / 共 2 页
字号:
		{

			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{}
		}
		
	}
}



/*
 
 

*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -