使用互操作将多个单词表添加到单词中
Adding multiple word tables into word using interop
我正在尝试使用 c# 将多个 table 插入到一个 word 文档中,但是当我添加另一个代码块以在其中添加一个 table 时,我收到了一个错误,第二个table 未插入。如何将范围向下移动到页面底部,然后添加另一个 table?我尝试使用文档参考结尾创建一个新范围,但这似乎不起作用,有人能帮我一些忙吗?
Word._Application objApp;
Word._Document objDoc;
try
{
object objMiss = System.Reflection.Missing.Value;
object objEndOfDocFlag = "\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and create a new document.
objApp = new Word.Application();
objApp.Visible = true;
objDoc = objApp.Documents.Add(ref objMiss, ref objMiss,
ref objMiss, ref objMiss);
//Insert a paragraph at the end of the document.
Word.Paragraph objPara2; //define paragraph object
object oRng = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of the page
objPara2 = objDoc.Content.Paragraphs.Add(ref oRng); //add paragraph at end of document
objPara2.Range.Text = "Test Table Caption"; //add some text in paragraph
objPara2.Format.SpaceAfter = 10; //defind some style
objPara2.Range.InsertParagraphAfter(); //insert paragraph
//Insert a table
Word.Table objTab1; //create table object
Word.Range objWordRng = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of document
objTab1 = objDoc.Tables.Add(objWordRng, 9, 2, ref objMiss, ref objMiss); //add table object in word document
objTab1.Range.ParagraphFormat.SpaceAfter = 6;
objTab1.Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
objTab1.Range.Borders[Word.WdBorderType.wdBorderHorizontal].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Range.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Range.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Range.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Columns.Borders[Word.WdBorderType.wdBorderVertical].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Columns[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
objTab1.Columns[1].Width = objApp.CentimetersToPoints(3.63f);
objTab1.Columns[2].Width = objApp.CentimetersToPoints(13.11f);
int iRow, iCols;
string[] col = new string[9];
col[0] = "Row1";
col[1] = "row2";
col[2] = "Row3";
col[3] = "row4";
col[4] = "row5";
col[5] = "row6";
col[6] = "row7";
col[7] = "row8";
col[8] = "tow9";
for (iRow = 1; iRow <= 9; iRow++)
{
objTab1.Rows[iRow].Range.Font.Bold = 1;
for (int i = 0; i <= col.Length; i++)
{
string s = col[i];
objTab1.Rows[iRow++].Range.Text = s;
objTab1.Rows[iRow].Range.Font.Bold = 1;
}
}
objApp.Selection.TypeParagraph();
//Insert a paragraph at the end of the document.
Word.Paragraph objPara3; //define paragraph object
object oRng2 = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of the page
objPara3 = objDoc.Content.Paragraphs.Add(ref oRng2); //add paragraph at end of document
objPara3.Range.Text = "hello"; //add some text in paragraph
objPara3.Format.SpaceAfter = 10; //defind some style
objPara3.Range.InsertParagraphAfter(); //insert paragraph
//Insert a 2 x 2 table, (table with 2 row and 2 column)
Word.Table objTab2; //create table object
Word.Range objWordRng2 = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of document
objTab2 = objDoc.Tables.Add(objWordRng2, 9, 2, ref objMiss, ref objMiss); //add table object in word document
objTab2.Range.ParagraphFormat.SpaceAfter = 6;
object stylename2 = "Table Grid";
我得到以下异常 "the requested member of the collection does not exist"
没有完全遵循您希望布局的显示方式。发布的代码有几个问题。首先,在 for
循环中,您将文本添加到第一个 table,我不确定您在用以下几行做什么:
objTab1.Rows[iRow++].Range.Text = s;
objTab1.Rows[iRow].Range.Font.Bold = 1;
第一行中的 iRow++
增量将在 table 中的行中删除。我猜你可能想要:
objTab1.Rows[iRow].Range.Font.Bold = 1;
objTab1.Rows[iRow].Range.Text = s;
iRow++;
另一个问题是代码如何获取最后一段,如下所示:
object oRng2 = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range;
objPara3 = objDoc.Content.Paragraphs.Add(ref oRng);
oRng2
范围是文档范围的末尾,但是,下一行使用 oRng
,这是文档的顶部。将添加段落更改为适当的范围应该可以解决此问题。
objPara3 = objDoc.Content.Paragraphs.Add(ref oRng2);
希望对您有所帮助。
我正在尝试使用 c# 将多个 table 插入到一个 word 文档中,但是当我添加另一个代码块以在其中添加一个 table 时,我收到了一个错误,第二个table 未插入。如何将范围向下移动到页面底部,然后添加另一个 table?我尝试使用文档参考结尾创建一个新范围,但这似乎不起作用,有人能帮我一些忙吗?
Word._Application objApp;
Word._Document objDoc;
try
{
object objMiss = System.Reflection.Missing.Value;
object objEndOfDocFlag = "\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and create a new document.
objApp = new Word.Application();
objApp.Visible = true;
objDoc = objApp.Documents.Add(ref objMiss, ref objMiss,
ref objMiss, ref objMiss);
//Insert a paragraph at the end of the document.
Word.Paragraph objPara2; //define paragraph object
object oRng = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of the page
objPara2 = objDoc.Content.Paragraphs.Add(ref oRng); //add paragraph at end of document
objPara2.Range.Text = "Test Table Caption"; //add some text in paragraph
objPara2.Format.SpaceAfter = 10; //defind some style
objPara2.Range.InsertParagraphAfter(); //insert paragraph
//Insert a table
Word.Table objTab1; //create table object
Word.Range objWordRng = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of document
objTab1 = objDoc.Tables.Add(objWordRng, 9, 2, ref objMiss, ref objMiss); //add table object in word document
objTab1.Range.ParagraphFormat.SpaceAfter = 6;
objTab1.Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;
objTab1.Range.Borders[Word.WdBorderType.wdBorderHorizontal].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Range.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Range.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Range.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Columns.Borders[Word.WdBorderType.wdBorderVertical].LineStyle = Word.WdLineStyle.wdLineStyleDouble;
objTab1.Columns[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
objTab1.Columns[1].Width = objApp.CentimetersToPoints(3.63f);
objTab1.Columns[2].Width = objApp.CentimetersToPoints(13.11f);
int iRow, iCols;
string[] col = new string[9];
col[0] = "Row1";
col[1] = "row2";
col[2] = "Row3";
col[3] = "row4";
col[4] = "row5";
col[5] = "row6";
col[6] = "row7";
col[7] = "row8";
col[8] = "tow9";
for (iRow = 1; iRow <= 9; iRow++)
{
objTab1.Rows[iRow].Range.Font.Bold = 1;
for (int i = 0; i <= col.Length; i++)
{
string s = col[i];
objTab1.Rows[iRow++].Range.Text = s;
objTab1.Rows[iRow].Range.Font.Bold = 1;
}
}
objApp.Selection.TypeParagraph();
//Insert a paragraph at the end of the document.
Word.Paragraph objPara3; //define paragraph object
object oRng2 = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of the page
objPara3 = objDoc.Content.Paragraphs.Add(ref oRng2); //add paragraph at end of document
objPara3.Range.Text = "hello"; //add some text in paragraph
objPara3.Format.SpaceAfter = 10; //defind some style
objPara3.Range.InsertParagraphAfter(); //insert paragraph
//Insert a 2 x 2 table, (table with 2 row and 2 column)
Word.Table objTab2; //create table object
Word.Range objWordRng2 = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range; //go to end of document
objTab2 = objDoc.Tables.Add(objWordRng2, 9, 2, ref objMiss, ref objMiss); //add table object in word document
objTab2.Range.ParagraphFormat.SpaceAfter = 6;
object stylename2 = "Table Grid";
我得到以下异常 "the requested member of the collection does not exist"
没有完全遵循您希望布局的显示方式。发布的代码有几个问题。首先,在 for
循环中,您将文本添加到第一个 table,我不确定您在用以下几行做什么:
objTab1.Rows[iRow++].Range.Text = s;
objTab1.Rows[iRow].Range.Font.Bold = 1;
第一行中的 iRow++
增量将在 table 中的行中删除。我猜你可能想要:
objTab1.Rows[iRow].Range.Font.Bold = 1;
objTab1.Rows[iRow].Range.Text = s;
iRow++;
另一个问题是代码如何获取最后一段,如下所示:
object oRng2 = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range;
objPara3 = objDoc.Content.Paragraphs.Add(ref oRng);
oRng2
范围是文档范围的末尾,但是,下一行使用 oRng
,这是文档的顶部。将添加段落更改为适当的范围应该可以解决此问题。
objPara3 = objDoc.Content.Paragraphs.Add(ref oRng2);
希望对您有所帮助。