C#关闭已创建但从未保存的Microsoft WORD文档

C#关闭已创建但从未保存的Microsoft WORD文档,c#,ms-word,vsto,office-interop,C#,Ms Word,Vsto,Office Interop,我下面的代码创建了一个WORD文档,我的C#代码可以对其进行编辑。但是如果发生错误,我想关闭try catch语句的catch块中的文档。问题:我们如何以编程方式关闭它。我知道什么时候文档被保存在硬盘上,你知道文档的路径吗 Microsoft.Office.Interop.Word.Application myWordApp = Globals.ThisAddIn.Application Microsoft.Office.Interop.Word.Document tempDoc = myWor

我下面的代码创建了一个WORD文档,我的
C#
代码可以对其进行编辑。但是如果发生错误,我想关闭
try catch
语句的
catch
块中的文档。问题:我们如何以编程方式关闭它。我知道什么时候文档被保存在硬盘上,你知道文档的路径吗

Microsoft.Office.Interop.Word.Application myWordApp = Globals.ThisAddIn.Application
Microsoft.Office.Interop.Word.Document tempDoc = myWordApp.Documents.Add(Visible: false);

这似乎有效。。。您的问题中是否有未包括的特殊要求

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;

namespace WordAddIn1
{
  public partial class ThisAddIn
  {
private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
      var app = Application;
      var doc = app.Documents.Add(Visible: false);

      doc.Close();

      var docCount = Application.Documents.Count ; // =0
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region Von VSTO generierter Code

    /// <summary>
    /// Erforderliche Methode für die Designerunterstützung.
    /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
    /// </summary>
    private void InternalStartup()
    {
      this.Startup += new System.EventHandler(ThisAddIn_Startup);
      this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml.Linq;
使用Word=Microsoft.Office.Interop.Word;
使用Office=Microsoft.Office.Core;
使用Microsoft.Office.Tools.Word;
名称空间WordAddIn1
{
公共部分类ThisAddIn
{
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
var-app=应用程序;
var doc=app.Documents.Add(可见:false);
doc.Close();
var docCount=Application.Documents.Count;//=0
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
}
#区域VSTO generierter代码
/// 
///模具设计人员的经验方法。
///这是一个非常复杂的数字高程模型代码编辑器geändert werden。
/// 
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
}
}

为什么不能使用此选项:
对象不保存更改=
Word.WdSaveOptions.wdDoNotSaveChanges;tempDoc.Close(ref doNotSaveChanges、ref缺失、ref缺失)?是否将tempDoc.Close();可以吗?你在想什么样的错误?请举一个或多个例子。