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

📄 formword.cs

📁 我们企业内部开发oa时用来测试如何调用word控件
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace word_test
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class FormWord : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.OpenFileDialog openFileD;
		private System.Windows.Forms.Button butSourceDocumen;
		/// MS Word COM Object
		/// This is where we create our WORD object
		private Word.ApplicationClass word_task=new Word.ApplicationClass();

		public FormWord()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormWord));
			this.butSourceDocumen = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// butSourceDocumen
			// 
			this.butSourceDocumen.Location = new System.Drawing.Point(24, 32);
			this.butSourceDocumen.Name = "butSourceDocumen";
			this.butSourceDocumen.TabIndex = 0;
			this.butSourceDocumen.Text = "open";
			this.butSourceDocumen.Click += new System.EventHandler(this.butSourceDocumen_Click);
			// 
			// FormWord
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(336, 189);
			this.Controls.Add(this.butSourceDocumen);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "FormWord";
			this.Text = "word_test";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FormWord());
		}
		/// Get source document. Open a FileDialog window for 
		/// user to select single/multiple files for
		/// parsing.
		private void butSourceDocumen_Click(object sender, System.EventArgs e)
		{
			openFileD=new OpenFileDialog();
			if( openFileD.ShowDialog() == DialogResult.OK )
			{
				object  fileName = openFileD.FileName;
				object  saveFile = fileName + "_Vk.doc";
        
        
				object  vk_read_only  = false;
				object  vk_visible  = true;
				object  vk_false    = false;
				object  vk_true    = true;
				object  vk_dynamic  = 2;
        
				object  vk_missing  = System.Reflection.Missing.Value;

				// Let make the word application visible
				word_task.Visible = true;
				word_task.Activate();

				// Let's open the document
				Word.Document vk_my_doc = word_task.Documents.Open(
					ref fileName, ref vk_missing, ref vk_read_only, 
					ref vk_missing, ref vk_missing,
					ref vk_missing, ref vk_missing, ref vk_missing, ref vk_missing,
					ref vk_missing, ref vk_missing, ref vk_visible, ref vk_missing, 
					ref vk_missing, ref vk_missing, ref vk_missing);
				
				// Let's create a new document
				Word.Document vk_new_doc = word_task.Documents.Add( 
					ref vk_missing, ref vk_missing, ref vk_missing, ref vk_visible );

				// Select and Copy from the original document
				vk_my_doc.Select();
				word_task.Selection.Copy();

				// Paste into new document as unformatted text
				vk_new_doc.Select();
				word_task.Selection.PasteSpecial( ref vk_missing, ref vk_false,
					ref vk_missing, ref vk_false, ref vk_dynamic, 
					ref vk_missing, ref vk_missing );

				// close the original document
				vk_my_doc.Close( ref vk_false, ref vk_missing, ref vk_missing );

				vk_new_doc.Select();
				FindAndReplace( "^t^t^t^t^t^t^t", "^t", 2);//vk_num );

				// Save the new document
				vk_new_doc.SaveAs(
					ref saveFile, ref vk_missing, 
					ref vk_missing, ref vk_missing, ref vk_missing,
					ref vk_missing, ref vk_missing, ref vk_missing, 
					ref vk_missing, ref vk_missing, ref vk_missing,
					ref vk_missing, ref vk_missing, ref vk_missing, 
					ref vk_missing, ref vk_missing);

				// close the new document
				vk_new_doc.Close( ref vk_false, ref vk_missing, ref vk_missing );

				// close word application
				word_task.Quit( ref vk_false, ref vk_missing, ref vk_missing );
			}

		}
	
		private void FindAndReplace( object vk_find, object vk_replace,object vk_num )
		{
			object  vk_read_only  = false;
			object  vk_visible    = true;
			object  vk_false      = false;
			object  vk_true      = true;
			object  vk_dynamic    = 2;

			word_task.Selection.Find.Execute( ref vk_find, 
				ref vk_false, ref vk_false,
				ref vk_false, ref vk_false, ref vk_false, ref vk_true, 
				ref vk_num, ref vk_false,
				ref vk_replace, ref vk_dynamic, ref vk_false, 
				ref vk_false, ref vk_false, ref vk_false );
		}

	}
}

⌨️ 快捷键说明

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