首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c#一次打开多个实例/文件时,保存和关闭office文档

c#一次打开多个实例/文件时,保存和关闭office文档
EN

Stack Overflow用户
提问于 2017-09-13 21:20:33
回答 2查看 1.5K关注 0票数 0

我正在开发一个自动保存所有打开的办公文档的文件备份程序。目前,我正在使用WORD,遇到了一个问题。只要只有一个对话框在运行,我就可以成功地保存和关闭word的活动实例,而不会出现任何对话框。如果我打开了多个word文档,当第一个文档关闭时,第二个文档会显示一个“另存为”对话框。有没有人知道我怎么才能解决这个问题,或者这是否可能?

保存和关闭代码,

代码语言:javascript
复制
 using Microsoft.Office.Interop.Word;

   public static bool WordClass1(string doc,string sloc)

        {

            if (System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") != null)

            {

                Object oMissing = System.Reflection.Missing.Value;

                Object oTrue = true;

                Microsoft.Office.Interop.Word.Application oWordApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

                int i = 0;
                i++;
                string num = i.ToString();
                Object oSaveAsFileWord = sloc;
                foreach (Microsoft.Office.Interop.Word.Document document in oWordApp.Documents)
                {
                    if (string.Equals(document.Name, doc))
                    {
                        Console.WriteLine("Found Document");





                        document.SaveAs(ref oSaveAsFileWord, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);



                        object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

                        oWordApp.ActiveDocument.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
                    }

                }

返回true;

EN

回答 2

Stack Overflow用户

发布于 2017-09-13 21:32:04

代码语言:javascript
复制
 public static bool WordClass1(string doc,string sloc)

        {

            if (System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") != null)

            {

                Object oMissing = System.Reflection.Missing.Value;

                Object oTrue = true;

                Microsoft.Office.Interop.Word.Application oWordApp = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

                int i = 0;
                i++;
                string num = i.ToString();
                Object oSaveAsFileWord = sloc;
                foreach (Microsoft.Office.Interop.Word.Document document in oWordApp.Documents)
                {
                    if (string.Equals(document.Name, doc))
                    {
                        Console.WriteLine("Found Document");





                        document.SaveAs(ref oSaveAsFileWord, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);



                        object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

                        oWordApp.Document.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
                    }

                }
票数 0
EN

Stack Overflow用户

发布于 2017-09-14 05:55:28

您需要在foreach循环中使用document而不是oWordApp.ActiveDocument

因此,举例来说,而不是:

代码语言:javascript
复制
oWordApp.ActiveDocument.Close

您应该使用:

代码语言:javascript
复制
document.Close
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46198743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档