用word插件在当前位置添加table
Add table in current position with a word add-in
我想用我的 Word 加载项添加一个 table,其中包含我数据库中的数据。
我已经成功地做到了,但是现在 table 的位置有问题。
我想把它准确地放在我当前在 Word 文档中的位置。
但它总是在开头添加。
有谁知道如何调整我的起始值始终是我当前位置的范围?
这是我的代码的一部分:
private void createTable_Click(object sender, EventArgs e) {
object start = 0, end = 0;
Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
Word.Range rng = document.Range(ref start, ref end);
// Insert a title for the table and paragraph marks.
rng.InsertBefore("List");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
// Add the table.
rng.Tables.Add(document.Paragraphs[2].Range, 1, 7, ref missing, ref missing);
// Format the table and apply a style.
Word.Table tbl = document.Tables[1]; tbl.Range.Font.Size = 8;
tbl.Borders[WdBorderType.wdBorderLeft].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderRight].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderTop].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderBottom].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderHorizontal].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderVertical].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderBottom].Color = WdColor.wdColorBlack; tbl.Rows.Alignment = WdRowAlignment.wdAlignRowCenter; tbl.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);
重新阅读...要在当前位置插入 - 如果您指的是光标所在的位置:
Word.Range rngSel = wdApp.Selection.Range;
rngSel.Tables.Add(//params here);
否则,如果您的意思是在代码插入的信息末尾,而不是这两行
rng.InsertBefore("List");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
使用
rng.Text = "List\n\n"
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.Collapse(WdCollapseDirection.wdCollapseEnd);
\n
插入一个新段落(回车 return)并且可以作为字符串的一部分包含在内。
将文本直接分配给 Range
并使用 Collapse
方法(在我看来)比各种 Insert 方法更容易预测。有些 Insert 方法包括范围内插入的内容,有些则不。
FWIW 当不清楚可能是什么问题时,将 rng.Select();
放在代码的关键点并注释掉其余行,以便代码以以下范围结尾问题可见。这通常可以提供有关范围问题的起源的信息。
我想用我的 Word 加载项添加一个 table,其中包含我数据库中的数据。 我已经成功地做到了,但是现在 table 的位置有问题。 我想把它准确地放在我当前在 Word 文档中的位置。 但它总是在开头添加。 有谁知道如何调整我的起始值始终是我当前位置的范围? 这是我的代码的一部分:
private void createTable_Click(object sender, EventArgs e) {
object start = 0, end = 0;
Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
Word.Range rng = document.Range(ref start, ref end);
// Insert a title for the table and paragraph marks.
rng.InsertBefore("List");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
// Add the table.
rng.Tables.Add(document.Paragraphs[2].Range, 1, 7, ref missing, ref missing);
// Format the table and apply a style.
Word.Table tbl = document.Tables[1]; tbl.Range.Font.Size = 8;
tbl.Borders[WdBorderType.wdBorderLeft].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderRight].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderTop].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderBottom].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderHorizontal].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderVertical].LineStyle =
WdLineStyle.wdLineStyleSingle;
tbl.Borders[WdBorderType.wdBorderBottom].Color = WdColor.wdColorBlack; tbl.Rows.Alignment = WdRowAlignment.wdAlignRowCenter; tbl.AutoFitBehavior(WdAutoFitBehavior.wdAutoFitWindow);
重新阅读...要在当前位置插入 - 如果您指的是光标所在的位置:
Word.Range rngSel = wdApp.Selection.Range;
rngSel.Tables.Add(//params here);
否则,如果您的意思是在代码插入的信息末尾,而不是这两行
rng.InsertBefore("List");
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.InsertParagraphAfter();
rng.InsertParagraphAfter();
rng.SetRange(rng.End, rng.End);
使用
rng.Text = "List\n\n"
rng.Font.Name = "Verdana";
rng.Font.Size = 16;
rng.Collapse(WdCollapseDirection.wdCollapseEnd);
\n
插入一个新段落(回车 return)并且可以作为字符串的一部分包含在内。
将文本直接分配给 Range
并使用 Collapse
方法(在我看来)比各种 Insert 方法更容易预测。有些 Insert 方法包括范围内插入的内容,有些则不。
FWIW 当不清楚可能是什么问题时,将 rng.Select();
放在代码的关键点并注释掉其余行,以便代码以以下范围结尾问题可见。这通常可以提供有关范围问题的起源的信息。