【问题标题】:How to generate multiple tables in Word Document programmatically如何以编程方式在 Word 文档中生成多个表格
【发布时间】:2016-09-08 21:43:04
【问题描述】:

我正在尝试创建一个 MS 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);
}

【问题讨论】:

标签: c# office-interop


【解决方案1】:

我想出了答案。我将 Range 设置为每个循环结束时文档内容的最后一个索引 -1。

myRange = myDocument.Range(myDocument.Content.End - 1, ref myMissingValue);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    相关资源
    最近更新 更多