Share via


HOW TO:在 Office 程式設計中使用具名和選擇性引數 (C# 程式設計手冊)

Visual C# 2010 中引進的具名引數和選擇性引數可以加強 C# 程式設計的方便性、彈性和可讀性。此外,這些功能還可大幅加速對 COM 介面 (例如 Microsoft Office Automation API) 的存取。

在下列範例中,方法 ConvertToTable 有 16 個參數,這些參數代表資料表的特性,例如欄數和列數、格式化、邊框、字型和顏色。 所有 16 個參數都是選擇性,因為大部分時間您不會想要指定每個參數的特定值。 不過,如果不使用具名和選擇性引數,則每個參數都必須提供值或預留位置值。 使用具名和選擇性引數,您只需指定專案所需的參數值。

您必須已在電腦上安裝 Microsoft Office Word,才能完成下列程序。

注意事項注意事項

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱 Visual Studio 設定

若要建立新的主控台應用程式

  1. 啟動 Visual Studio。

  2. 在 [檔案] 功能表上,指向 [新增],然後按一下 [專案]。

  3. 在 [範本類別] 窗格中,展開 [Visual C#],然後按一下 [Windows]。

  4. 查看 [範本] 窗格的頂端,確定 [.NET Framework 4] 顯示在 [目標 Framework] 方塊中。

  5. 按一下 [範本] 窗格中的 [主控台應用程式]。

  6. 在 [名稱] 欄位中,輸入專案的名稱。

  7. 按一下 [確定]。

    新專案即會出現於 [方案總管] 中。

若要加入參考

  1. 在 [方案總管] 中,以滑鼠右鍵按一下您的專案名稱,然後按一下 [加入參考]。 [加入參考] 對話方塊隨即出現。

  2. 在 [.NET] 頁面上,選取 [元件名稱] 清單中的 [Microsoft.Office.Interop.Word]。

  3. 按一下 [確定]。

若要加入必要的 using 指示詞

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 [Program.cs] 檔案,再按一下 [檢視程式碼]。

  2. 在程式碼檔案的頂端加入下列 using 指示詞。

    using Word = Microsoft.Office.Interop.Word;
    

若要在 Word 文件中顯示文字

  1. 將下列方法加入至 Program.cs 的 Program 類別中,以便建立 Word 應用程式和 Word 文件。 Add 方法有 4 個選擇性參數。 這個範例使用其預設值。 因此,呼叫陳述式中不需要引數。

    static void DisplayInWord()
    {
        var wordApp = new Word.Application();
        wordApp.Visible = true;
        // docs is a collection of all the Document objects currently 
        // open in Word.
        Word.Documents docs = wordApp.Documents;
    
        // Add a document to the collection and name it doc. 
        Word.Document doc = docs.Add();
    }
    
  2. 將下列程式碼加入至方法的結尾,定義在文件何處顯示文字,以及顯示哪些文字。

    // Define a range, a contiguous area in the document, by specifying
    // a starting and ending character position. Currently, the document
    // is empty.
    Word.Range range = doc.Range(0, 0);
    
    // Use the InsertAfter method to insert a string at the end of the
    // current range.
    range.InsertAfter("Testing, testing, testing. . .");
    

若要執行應用程式

  1. 將下列陳述式加入至 Main。

    DisplayInWord();
    
  2. 按 CTRL+F5 執行專案。 會出現一個 Word 文件,包含所指定的文字。

若要將文字變更為表格

  1. 使用 ConvertToTable 方法,將文字框在表格中。 此方法有 16 個選擇性參數。 IntelliSense 會以括弧括住選擇性參數,如下圖所示。

    ConvertToTable 參數

    ConvertToTable 方法的參數清單。

    具名和選擇性引數讓您可以僅指定您要變更之參數的值。 將下列程式碼加入至 DisplayInWord 方法的結尾,建立簡單表格。 引數指定 range 中文字字串的逗號用於分隔資料表儲存格。

    // Convert to a simple table. The table will have a single row with
    // three columns.
    range.ConvertToTable(Separator: ",");
    

    在舊版 C# 中呼叫 ConvertToTable 時,每個參數都需要參考引數,如以下程式碼所示。

    // Call to ConvertToTable in Visual C# 2008 or earlier. This code
    // is not part of the solution.
    var missing = Type.Missing;
    object separator = ",";
    range.ConvertToTable(ref separator, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing);
    
  2. 按 CTRL+F5 執行專案。

若要實驗其他參數

  1. 若要變更表格,讓它具有一欄和三列,請將 DisplayInWord 的最後一行改為下列陳述式,然後按 CTRL+F5。

    range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1);
    
  2. 若要指定表格的預先定義格式,請將 DisplayInWord 的最後一行改為下列陳述式,然後按 CTRL+F5。 格式可以是任一 WdTableFormat 常數。

    range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1,
        Format: Word.WdTableFormat.wdTableFormatElegant);
    

範例

下列程式碼包含完整範例。

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


namespace OfficeHowTo
{
    class WordProgram
    {
        static void Main(string[] args)
        {
            DisplayInWord();
        }

        static void DisplayInWord()
        {
            var wordApp = new Word.Application();
            wordApp.Visible = true;
            // docs is a collection of all the Document objects currently 
            // open in Word.
            Word.Documents docs = wordApp.Documents;

            // Add a document to the collection and name it doc. 
            Word.Document doc = docs.Add();

            // Define a range, a contiguous area in the document, by specifying
            // a starting and ending character position. Currently, the document
            // is empty.
            Word.Range range = doc.Range(0, 0);

            // Use the InsertAfter method to insert a string at the end of the
            // current range.
            range.InsertAfter("Testing, testing, testing. . .");

            // You can comment out any or all of the following statements to
            // see the effect of each one in the Word document.

            // Next, use the ConvertToTable method to put the text into a table. 
            // The method has 16 optional parameters. You only have to specify
            // values for those you want to change.

            // Convert to a simple table. The table will have a single row with
            // three columns.
            range.ConvertToTable(Separator: ",");

            // Change to a single column with three rows..
            range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1);

            // Format the table.
            range.ConvertToTable(Separator: ",", AutoFit: true, NumColumns: 1,
                Format: Word.WdTableFormat.wdTableFormatElegant);
        }
    }
}

請參閱

概念

具名和選擇性引數 (C# 程式設計手冊)