我正在开发一个自动保存所有打开的办公文档的文件备份程序。目前,我正在使用WORD,遇到了一个问题。只要只有一个对话框在运行,我就可以成功地保存和关闭word的活动实例,而不会出现任何对话框。如果我打开了多个word文档,当第一个文档关闭时,第二个文档会显示一个“另存为”对话框。有没有人知道我怎么才能解决这个问题,或者这是否可能?
保存和关闭代码,
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;
发布于 2017-09-13 21:32:04
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);
}
}发布于 2017-09-14 05:55:28
您需要在foreach循环中使用document而不是oWordApp.ActiveDocument。
因此,举例来说,而不是:
oWordApp.ActiveDocument.Close您应该使用:
document.Closehttps://stackoverflow.com/questions/46198743
复制相似问题