关于如何将word文件读入RichTextBox
我在别的论坛经常看到有的朋友们问怎么样将word文件读入到richtextbox,(包括图片)而且要求如要修改后能保存,我想的一个办法:使用剪贴板的办法。我做了一个类,可以用于我们以后开发WORD的程序:
1、在运行这个程序之前请先导入三个dll 它们是:Interop.Microsoft.Office.Core.dll、Interop.VBIDE.dll、Interop.Word.dll)它们如何得到请查看一下以前的贴子,有很多是讲如何将com组件转为受限代码的。如果实现找不到也可以和我联系,我发给你!huanghai@bdfsz.com.cn
2、其实我们可以这样控制word ,在启动word后选录制新宏,开始我们的动作,比如我们想看一下用VBA如何控制全选-复制-剪切,就可以用这个办法。停止录制。用VBA编辑器看一下代码吧!
3 、废话不说了,看我的程序吧!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WordApplication
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
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.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 296);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 32);
this.button1.TabIndex = 0;
this.button1.Text = "开始读取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(16, 16);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(432, 264);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// openFileDialog1
//
this.openFileDialog1.DefaultExt = "*.doc";
this.openFileDialog1.Filter = "Word文件|*.doc";
//
// button2
//
this.button2.Location = new System.Drawing.Point(248, 296);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 32);
this.button2.TabIndex = 2;
this.button2.Text = "修改后保存";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 365);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "这是一个用于读取WORD文件到RICHEDIT的例子";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
richTextBox1.Clear();
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != "")
{
CCWordApp test;
test = new CCWordApp();
test.Open(openFileDialog1.FileName);
test.CopyAll();
richTextBox1.Paste();
test.Quit();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
richTextBox1.SelectAll();
richTextBox1.Copy();
CCWordApp test;
test = new CCWordApp();
//上面代码正常
test.Open(openFileDialog1.FileName);
test.Clear();
test.PasetAll();
test.Save();
test.Quit();
}
}
public class CCWordApp
{
private Word.ApplicationClass oWordApplic; // a reference to Word application
private Word.Document oDoc; // a reference to the document
public CCWordApp()
{
oWordApplic = new Word.ApplicationClass();
}
public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.value;
oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing);
oDoc.Activate();
}
public void Open()
{
object missing = System.Reflection.Missing.value;
oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
oDoc.Activate();
}
public void Quit()
{
object missing = System.Reflection.Missing.value;
oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
}
public void Save()
{
oDoc.Save();
}
public void SaveAs(string strFileName)
{
object missing = System.Reflection.Missing.value;
object fileName = strFileName;
oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
public void SaveAsHtml(string strFileName)
{
object missing = System.Reflection.Missing.value;
object fileName = strFileName;
object Format = (int)Word.WdSaveFormat.wdFormatHTML;
oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
oDoc.Close(ref missing, ref missing, ref missing);
}
public void CopyAll()
{
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Copy();
}
public void PasetAll()
{
oWordApplic.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
}
public void Clear()
{
object Unit = (int)Word.WdUnits.wdCharacter;
object Count = 1;
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Delete(ref Unit, ref Count);
}
public void InsertText(string strText)
{
oWordApplic.Selection.TypeText(strText);
}
public void InsertLineBreak()
{
oWordApplic.Selection.TypeParagraph();
}
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
oWordApplic.Selection.TypeParagraph();
}
public void SetAlignment(string strType)
{
switch (strType)
{
case "Center":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
break;
case "Left":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
break;
case "Right":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
break;
case "Justify":
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
break;
}
}
public void SetFont(string strType)
{
switch (strType)
{
case "Bold":
oWordApplic.Selection.Font.Bold = 1;
break;
case "Italic":
oWordApplic.Selection.Font.Italic = 1;
break;
case "Underlined":
oWordApplic.Selection.Font.Subscript = 0;
break;
}
}
public void SetFont()
{
oWordApplic.Selection.Font.Bold = 0;
oWordApplic.Selection.Font.Italic = 0;
oWordApplic.Selection.Font.Subscript = 0;
}
public void SetFontName(string strType)
{
oWordApplic.Selection.Font.Name = strType;
}
public void SetFontSize(int nSize)
{
oWordApplic.Selection.Font.Size = nSize;
}
public void InsertPagebreak()
{
object pBreak = (int)Word.WdBreakType.wdPageBreak;
oWordApplic.Selection.InsertBreak(ref pBreak);
}
public void GotoBookMark(string strBookMarkName)
{
object missing = System.Reflection.Missing.value;
object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark;
object NameBookMark = strBookMarkName;
oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
}
public void GoToTheEnd()
{
object missing = System.Reflection.Missing.value;
object unit;
unit = Word.WdUnits.wdStory;
oWordApplic.Selection.EndKey(ref unit, ref missing);
}
public void GoToTheBeginning()
{
object missing = System.Reflection.Missing.value;
object unit;
unit = Word.WdUnits.wdStory;
oWordApplic.Selection.HomeKey(ref unit, ref missing);
}
public void GoToTheTable(int ntable)
{
object missing = System.Reflection.Missing.value;
object what;
what = Word.WdUnits.wdTable;
object which;
which = Word.WdGoToDirection.wdGoToFirst;
object count;
count = 1;
oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
oWordApplic.Selection.Find.ClearFormatting();
oWordApplic.Selection.Text = "";
}
public void GoToRightCell()
{
object missing = System.Reflection.Missing.value;
object direction;
direction = Word.WdUnits.wdCell;
oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
}
public void GoToLeftCell()
{
object missing = System.Reflection.Missing.value;
object direction;
direction = Word.WdUnits.wdCell;
oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
}
public void GoToDownCell()
{
object missing = System.Reflection.Missing.value;
object direction;
direction = Word.WdUnits.wdLine;
oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
}
public void GoToUpCell()
{
object missing = System.Reflection.Missing.value;
object direction;
direction = Word.WdUnits.wdLine;
oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
}
public void InsertPageNumber(string strType, bool bHeader)
{
object missing = System.Reflection.Missing.value;
object alignment;
object bFirstPage = false;
object bF = true;
switch (strType)
{
case "Center":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
break;
case "Right":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
break;
case "Left":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
break;
}
}
}
}