?? formword.cs
字號(hào):
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>
/// 必需的設(shè)計(jì)器變量。
/// </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 窗體設(shè)計(jì)器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </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_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 );
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -