如何找到正在插入的 table 的索引
How to find the index of the table being inserted
我正在开发我的第一个 C# 应用程序,它将向 MS Word 功能区添加一个按钮,该按钮将在文档中插入一个新的 table。 table 的内容需要使用文档中前一个 table 的值(如果存在)填充。我可以插入 table,但无法找出找到前一个 table 的最佳方法。我创建了新的 table 然后得到 table 的总数:
Word.Range rng = Application.Selection.Range;
rng.Font.Name = "Times New Roman";
rng.Font.Size = 10;
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
// Add the table.
Word.Table tbl = rng.Tables.Add(rng, 13, 2, ref missing, ref missing);
var number_of_tables = this.Application.Documents[1].Tables.Count;
然而,我被卡住的地方是试图找出新插入的索引 table 所以我可以做这样的事情:
var new_table_index = tbl...some code here...
if (new_table_index > 1)
{
previous_table = this.Application.Documents[1].Tables[new_table_index - 1];
}
如何找到我刚刚插入的 table 的索引?谢谢!
通常的做法是计算从文档开头到有问题的 table 的 table 的数量。这样的话,到新加的范围table,再减一得到前面的table.
// Add the table.
Word.Table tbl = rng.Tables.Add(rng, 13, 2, ref missing, ref missing);
//Define a range from the start of doc to new table
Word.Range rngDocToTable = tbl.Range;
rng.Start = doc.Content.Start;
int nrTablesInRange = rng.Tables.Count;
//Get index of previous table
int indexPrevTable = nrTablesInRange - 1;
Word.Table previousTable = null;
if (indexPrevTable > 0)
{
previousTable = doc.Tables[indexPrevTable];
}
else
{
System.Windows.Forms.MessageBox.Show("No previous tables");
}
我正在开发我的第一个 C# 应用程序,它将向 MS Word 功能区添加一个按钮,该按钮将在文档中插入一个新的 table。 table 的内容需要使用文档中前一个 table 的值(如果存在)填充。我可以插入 table,但无法找出找到前一个 table 的最佳方法。我创建了新的 table 然后得到 table 的总数:
Word.Range rng = Application.Selection.Range;
rng.Font.Name = "Times New Roman";
rng.Font.Size = 10;
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
// Add the table.
Word.Table tbl = rng.Tables.Add(rng, 13, 2, ref missing, ref missing);
var number_of_tables = this.Application.Documents[1].Tables.Count;
然而,我被卡住的地方是试图找出新插入的索引 table 所以我可以做这样的事情:
var new_table_index = tbl...some code here...
if (new_table_index > 1)
{
previous_table = this.Application.Documents[1].Tables[new_table_index - 1];
}
如何找到我刚刚插入的 table 的索引?谢谢!
通常的做法是计算从文档开头到有问题的 table 的 table 的数量。这样的话,到新加的范围table,再减一得到前面的table.
// Add the table.
Word.Table tbl = rng.Tables.Add(rng, 13, 2, ref missing, ref missing);
//Define a range from the start of doc to new table
Word.Range rngDocToTable = tbl.Range;
rng.Start = doc.Content.Start;
int nrTablesInRange = rng.Tables.Count;
//Get index of previous table
int indexPrevTable = nrTablesInRange - 1;
Word.Table previousTable = null;
if (indexPrevTable > 0)
{
previousTable = doc.Tables[indexPrevTable];
}
else
{
System.Windows.Forms.MessageBox.Show("No previous tables");
}