2016-09-06 35 views
0

我正在嘗試創建一個MS Word文檔,其中包含多個相互位於下方的表格。如何以編程方式在Word文檔中生成多個表格

以下代碼導致錯誤消息「請求的集合中的成員不存在」。我知道我在前一個插入新表,我只是不知道如何創建一個新的表。

object myMissingValue = System.Reflection.Missing.Value; 
Word._Application myApp = new Word.Application(); 
Word._Document myDocument = WordApplication.Documents.Add(ref myMissingValue, ref myMissingValue, ref myMissingValue, ref myMissingValue); 
Word.Range myRange = myDocument.Range(ref myMissingValue, ref myMissingValue); 

for (int i = 0; i < 5; i++) 
{ 
    myDocument.Tables.Add(myRange, 2, 2, ref myMissingValue, ref myMissingValue); 
    Word.Table myTable = WordDocument.Tables[i]; 
    myTable.Range.Borders.Enable = 1; 
    myTable.Rows[1].Cells[1].Range.Text = "Table number " + Convert.ToString(i); 
} 
+0

[由C#的.NET創建在Word動態表(http://stackoverflow.com/questions/283523/creating-dynamic-tables-in-word-by-c-net]) –

回答

1

我想出了答案。我將Range設置爲每個循環結束時文檔內容的最後索引-1。

myRange = myDocument.Range(myDocument.Content.End - 1, ref myMissingValue); 
相關問題