代码之家  ›  专栏  ›  技术社区  ›  nam

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

  •  0
  • nam  · 技术社区  · 5 年前

    下面的代码创建了一个WORD文档 C# catch try-catch how to close it

    Microsoft.Office.Interop.Word.Application myWordApp = Globals.ThisAddIn.Application
    Microsoft.Office.Interop.Word.Document tempDoc = myWordApp.Documents.Add(Visible: false);
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   dba    5 年前

    这似乎有效。。。你的问题中有没有特别要求?

    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
      }
    }