Word文档始终以只读模式打开

时间:2016-07-14 16:33:11

标签: c# .net winforms ms-word

我正在尝试使用c#代码将一些文本写入word文档。但是,每当我打开文档时,它都以只读模式打开。下面是我的代码: -

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

namespace WFA1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            string textboxText = textBox1.Text.ToString();
            InsertToFile(textboxText);
        }

        void InsertToFile(string inputString) // function to insert string to word doc
        {
            object missing = System.Reflection.Missing.Value;
            object readOnly = false;
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\\Users\\SS5014874\\Desktop\\JohnH.docx", ref missing, ref readOnly, 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);                        
            app.ActiveDocument.Characters.Last.Select();
            app.Selection.Collapse();
            app.Selection.TypeText(inputString.ToString());            
            app.ActiveDocument.Save();
            MessageBox.Show("Inserted");
        }             

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {            
            string comboBoxText = comboBox1.Text.ToString();
            string comboBoxTextExpanded = "";
            if (comboBoxText == "BP")
            {
                comboBoxTextExpanded = "Balance paid";
            }
            else
            {
                if (comboBoxText == "FA")
                {
                    comboBoxTextExpanded = "Financial advisor";
                }                
            }

            InsertToFile(comboBoxTextExpanded);               
        }              

        private void button2_Click(object sender, EventArgs e)
        {
            string searchKeyword = textBox2.Text.ToString();
            searchText(searchKeyword);
        }

        void searchText(string txt) // function to search string and delete line
        {
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();                       
            Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\\Users\\SS5014874\\Desktop\\JohnH.docx");
            object missing = System.Reflection.Missing.Value;
            doc.Content.Find.ClearFormatting();
            object keyword = txt.ToString();            
            var range = doc.Content;
            if (range.Find.Execute(ref keyword, 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))            
            {                                
                range.Expand(WdUnits.wdParagraph);
                range.Delete();
                MessageBox.Show("removed para");
            }
            else
            {
                MessageBox.Show("Not found");
            }
            //doc.Close(ref missing, ref missing, ref missing);
            //app.Quit(ref missing, ref missing, ref missing);            
        }        


    }
}
你可以帮我解决这个问题吗?我在第app.ActiveDocument.Save();行找到了例外

2 个答案:

答案 0 :(得分:0)

app.ActiveDocument.Close();

之后添加app.ActiveDocument.Quit();app.ActiveDocument.Save();

更新的代码:

    void InsertToFile(string inputString) // function to insert string to word doc
    {
        object missing = System.Reflection.Missing.Value;
        object readOnly = false;
        Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\\Users\\SS5014874\\Desktop\\JohnH.docx", ref missing, ref readOnly, 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);                        
        app.ActiveDocument.Characters.Last.Select();
        app.Selection.Collapse();
        app.Selection.TypeText(inputString.ToString());            
        app.ActiveDocument.Save();
        app.ActiveDocument.Close();
        app.Quit();
        MessageBox.Show("Inserted");
    }       

答案 1 :(得分:0)

使用此

app.ActiveDocument.Close(WdSaveOptions.wdSaveChanges);