C# 读写Word :以及Normal.dot读写模板问题

时间:2022-02-14 10:03:57

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop;

namespace DealDoc
{
    public partial class Form1 : Form
    {
        string docContent = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {     
           
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            //if (folderBrowserDialog1.ShowDialog() == DialogResult.OK && folderBrowserDialog1.SelectedPath != "")
            if(openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
            {
                this.textBox1.Text = openFileDialog1.FileName;
                docContent = Doc2Text(this.textBox1.Text);
                this.richTextBox1.Text = docContent;
                this.richTextBox1.Enabled = false;
            }

        }

        //编辑
        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Enabled = true;
            this.richTextBox1.Focus();
        }
       
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog1 = new SaveFileDialog();  
            saveFileDialog1.Filter = "Doc文件(*.doc)|*.doc|Txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            saveFileDialog1.Title = "保存文件";
            //StreamWriter   myStream   =   new   StreamWriter(saveFileDialog1.FileName);  
            if ((saveFileDialog1.ShowDialog()) == DialogResult.OK)
            {
                if (saveFileDialog1.FileName != null)
                {
                    //myStream.Write(this.richTextBox1.Text);  
                    //myStream.Close();  
                    this.richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                }
            }
        }

        //删除
        private void btnDel_Click(object sender, EventArgs e)
        {
           
        }

        //关闭
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        //获得word文本
        public string Doc2Text(string docFileName)
        {
            Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

            //Normal.dot 模板使用
            wordApp.NormalTemplate.Saved = true;

            object fileobj = docFileName;
            object nullobj = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
                );
         
            string outText = doc.Content.Text;

            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
            return outText;

            ///关闭Word时调用
            //object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            //object missing = System.Reflection.Missing.Value;
            //wordApp.Quit(ref saveOption, ref missing, ref missing);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //垃圾处理
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
}

********************************************************************************

 

详细的:将 Word 用作自动化服务器时提示保存 Normal.dot

http://support.microsoft.com/kb/285885/zh-cn

 

 

同时自动化多个 Microsoft Word 实例时,用户可能收到下面的一个或多个警告:“Normal.dot was being edited by another Word session.If you save this document with the original name, you will overwrite any changes made in the other session.Do you want to save the document using the original name anyway?” - 或 - This file is in use by another application or user.(C:/Documents and Settings/.../Normal.dot) 如果对 Normal.dot 模板进行了更改,就可能会出现这些警告。C# 读写Word :以及Normal.dot读写模板问题回到顶端

解决方案要解决此问题,请执行下列操作之一: 在退出 Word 或将控制权移交给用户之前,应将 Normal.dot 模板的 Saved 属性设置为 True,如下所示:...

要解决此问题,请执行下列操作之一:
  • 在退出 Word 或将控制权移交给用户之前,应将 Normal.dot 模板的 Saved 属性设置为 True,如下所示:
    Application.NormalTemplate.Saved = True
    - 或 -
  • 设置 Quit 方法的 SaveChanges 参数,如下所示:
    Application.Quit SaveChanges:=wdDoNotSaveChanges
C# 读写Word :以及Normal.dot读写模板问题回到顶端

更多信息重现此问题的步骤 在 Visual Basic 中,新建一个标准 EXE 项目。默认情况下会创建 Form1。 在“项目”菜单上,单击“引用”,然后添加一个指向...

重现此问题的步骤

  1. 在 Visual Basic 中,新建一个标准 EXE 项目。默认情况下会创建 Form1。
  2. 在“项目”菜单上,单击“引用”,然后添加一个指向 Microsoft Word 对象库版本的引用。
  3. 向 Form1 中添加一个 CommandButton 控件。
  4. 向窗体中添加以下代码:
    Private Sub Command1_Click()
    Dim wdApp1 As Word.Application
    Dim wdApp2 As Word.Application

    Set wdApp1 = CreateObject("Word.Application")
    wdApp1.Visible = True
    wdApp1.Documents.Add

    Set wdApp2 = CreateObject("Word.Application")
    wdApp2.Visible = True
    wdApp2.Documents.Add

    MsgBox "Change the default font of document 2."

    wdApp2.ActiveDocument.Close False
    wdApp2.Quit
    Set wdApp2 = Nothing

    wdApp1.Quit
    Set wdApp1 = Nothing
    End Sub
  5. 运行该 Visual Basic 项目并单击命令按钮。
  6. 将出现一个消息框,指导您更改第二个文档的默认字体。在“格式”菜单上,单击“字体”,然后单击“默认”。当询问您是否更改默认字体时,单击“是”,然后单击“确定”取消消息框。
当关闭第二个 Word 实例时,将出现在“摘要”部分显示的警告之一。

要解决以上代码中的此问题,请执行以下操作之一:
  • 在 wdApp2.Quit 方法的调用语句之前,添加以下行:
        wdApp2.NormalTemplate.Saved = True

    - 或 -
  • 使用 Quit 方法的 SaveChanges 参数,如下所示:
       object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            wa.QuIT(ref saveOption, ref Missing, ref Missing);

  • 【******后转】