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

3

我正在尝试使用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
你尝试使用不同的文档了吗?确保文件本身在文件属性框中没有只读检查。 - slayernoah
@slayernoah- 是的,我也尝试了新文档,但是它们也以只读模式打开。一旦代码第一次运行后,文档就会变成只读模式。第一次它可以正常工作。 - Sourav
在保存文件之后,尝试使用 app.ActiveDocument.Close(); - slayernoah
在尝试之前,您需要进入任务管理器并结束所有winword.exe进程,因为上一个文档未关闭。并且在关闭文档后使用app.Quit(); - slayernoah
这有帮助到您吗? - slayernoah
2个回答

1
app.ActiveDocument.Save(); 后面添加 app.ActiveDocument.Close();app.ActiveDocument.Quit(); 更新后的代码:
    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");
    }       

0
使用这个。
app.ActiveDocument.Close(WdSaveOptions.wdSaveChanges);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接