建立 及 開啟 Word 檔案 (沒有儲存檔案)

 
建立 及 開啟 Word 檔案 (沒有儲存檔案)
 
需要 加上 (Microsoft.Office.Interop.Word) Reference
 

   Form1.cs
 
   using Word = Microsoft.Office.Interop.Word;
 
   private void Create_Word()
   {
 
      object oMissing = System.Reflection.Missing.Value;
 
      Word.Application WordApp;
      Word.Document WordoDoc;
 
      WordApp = new Word.Application();
      WordApp.Visible = true;
      WordoDoc = WordApp.Documents.Add(ref oMissing,
      ref oMissing, ref oMissing, ref oMissing);
 
      WordApp.Visible = true;
 
      releaseObject(WordoDoc);
      releaseObject(WordApp);
 
   }
 
   private void releaseObject(object obj)
   {
      try
      {
         System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
         obj = null;
      }
      catch (Exception ex)
      {
         obj = null;
         MessageBox.Show("Unable to release the Object " + ex.ToString());
      }
      finally
      {
         GC.Collect();
      }
   }