首页 > 解决方案 > 在 C# 中编辑 Microsoft Word。但是,会弹出一个 Word 窗口并且有人正在编辑它

问题描述

我正在尝试使用 Microsoft.Office.Interop.Word API 编辑一个 word 文件(序列:创建一个没有任何内容的 .docx 文件(流)=> 使用 API 编辑 => 保存)但是当程序运行时,会弹出一个 word 窗口起来,就会出现这个窗口。说“(用户)正在编辑并被锁定。” 我应该怎么办?(当我关闭word窗口或单击“只读”按钮时出现错误。(我有一个办公室帐户))

代码:

            string NewFile = rootPath + filename;
            if (!File.Exists(NewFile)) 
            {
                FileStream stream = File.Create(NewFile);
                stream.Close();
            }
            object end_of_doc = "\\endofdoc";
            Word.Application writing = new Word.Application();
            object fileName = rootPath + filename;
            Word.Document doc = writing.Documents.Open(ref fileName);
            //.........(Lots of codes)
            doc.SaveAs2(ref fileName);

标签: c#.netms-wordio

解决方案


推荐阅读