C#中打开word 文件并在richTextBox中显示
2012-07-16 18:25
232 查看
void OpenNewChildForm() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "(文本文件)|*.txt|(word文件)|*.doc"; ofd.FilterIndex = 1; if (ofd.ShowDialog(this) == DialogResult.OK) { MDIChild child = new MDIChild(); child.Text = ofd.FileName; child.MdiParent = this; // 如果打开的是 word 或者是 .txt 文件,就执行下面的代码; if (ofd.FilterIndex == 2 || ofd.FilterIndex == 1) { Word.Application wordApp = new Word.Application(); Word.Document doc = null; object missing = System.Reflection.Missing.Value; object fileName = ofd.FileName; object readOnly = false; object isVisible = true; object index = 0; try { doc = wordApp.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, ref missing); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); child.rtbChild.Text = data.GetData(DataFormats.Text).ToString(); child.Show(); //子窗体的显示; } catch { if (doc != null) { doc.Close(ref missing, ref missing, ref missing); doc = null; } if (wordApp != null) { wordApp.Quit(ref missing, ref missing, ref missing); wordApp = null; } } } } }对于 .txt文件也一样适合;
相关文章推荐
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- C# WinForm打开PDF文件并在窗体中显示
- C# Visual Studio 2005中RichTextBox打开文件为rtf格式
- 每次打开 Office 的 Word、Excel……时 都会显示安装程序正在准备必要的文件的解决办法
- 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)
- C#打开WORD文档内容并显示
- C#打开WORD文档内容并显示(未完善表格图片等排版显示的处理)
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- C#创建和打开word文件
- c#中RichTextBox显示word
- C#加载word文件 一切配置正常 wordApp.Documents.Open打开文件为null的解决fang'a
- C#初学者--建立一个button打开并显示dbf文件
- C#按键打开文件选择对话框,并把选择好的路径保存/显示到textBox
- 如何在C#下利用RichTextBox打开一个有文字格式和图片的Word文档
- C#显示PDF文件,winform打开PDF文件并在窗体中显示
- C#winform中打开PDF文件并在窗体中显示
- asp.net C# 打开word文件时 命令失败
- 打开Word文件并显示在RichtTextBox上