共用方式為


HOW TO:建立新文件

更新:2007 年 11 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

  • 應用程式層級專案

Microsoft Office 版本

  • Word 2003

  • Word 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

當您以程式設計方式建立文件時,新文件是原生 (Native) Microsoft.Office.Interop.Word.Document 物件。這個物件沒有 Microsoft.Office.Tools.Word.Document 主項目的額外事件和資料繫結 (Data Binding) 功能。如需詳細資訊,請參閱主項目和主控制項的程式設計限制

從 SP1 開始,您可以使用應用程式層級增益集,將任何 Microsoft.Office.Interop.Word.Document 物件轉換為 Microsoft.Office.Tools.Word.Document 主項目。如需詳細資訊,請參閱在應用程式層級增益集的執行階段中擴充 Word 文件和 Excel 活頁簿

當您開發文件層級專案時,無法透過程式設計方式將 Microsoft.Office.Tools.Word.Document 主項目加入至專案。

若要依據 Normal.dot 建立新文件

  • 使用 Documents 集合的 Add 方法,依據 Normal.dot 建立新文件。

    Me.Application.Documents.Add()
    
    this.Application.Documents.Add(ref missing, ref missing, ref missing, ref missing);
    

使用自訂範本

Add 方法具有選擇性 (Optional) Template 引數,可以根據 Normal.dot 以外的範本來建立新文件。您必須提供範本的檔名和完整路徑。

若要依據自訂範本建立新文件

  • 呼叫 Documents 集合的 Add 方法,並指定範本的路徑。

    Me.Application.Documents.Add(Template:="C:\Test\SampleTemplate.Dot")
    
    object template = @"C:\Test\SampleTemplate.dot"; 
    this.Application.Documents.Add(ref template, ref missing, ref missing, ref missing);
    

請參閱

工作

HOW TO:開啟現有的文件

概念

主項目和主控制項概觀

主項目和主控制項的程式設計限制

了解 Office 方案中的選擇性參數