예제 #1
0
 private void NewFile_Click(object sender, EventArgs e)
 {
     if (doc != null)
     {
         doc.Close();
     }
     //窗体记录数++
     wCount++;
     //创建文档窗体对象
     doc = new DocForm();
     //设置主窗口为文档窗体的父窗口
     doc.MdiParent = this;
     //设置文档窗体的标题
     doc.Text = "文档" + wCount;
     //显示文档窗体
     doc.Show();
 }
예제 #2
0
        //保存richTextBox数据到已经存在的word文档中。
        //先打开word文档,全选word文档中的数据,然后全选richTextBox中的数据并复制,粘贴到word文档中,最后保存word文档,并关闭文档
        public void SaveWord(string fileName)
        {
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document    doc = null;
            object missing   = System.Reflection.Missing.Value;
            object File      = fileName;
            object readOnly  = false;
            object isVisible = true;

            try
            {
                doc = app.Documents.Open(ref File, 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();                            //全选
                this.doc.TxtSource.SelectAll();
                Clipboard.SetData(DataFormats.Rtf, this.doc.TxtSource.SelectedRtf); //复制RTF数据到剪贴板
                doc.ActiveWindow.Selection.Paste();
                //doc.Paragraphs.Last.Range.Text = richTextBox1.Text;//word文档赋值数据,不带格式
                doc.Save();
                MessageBox.Show("文件保存成功");
            }
            finally
            {
                if (doc != null)
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    doc = null;
                }

                if (app != null)
                {
                    app.Quit(ref missing, ref missing, ref missing);
                    app = null;
                }
            }
        }