使用互操作将多个单词表添加到单词中 [英] Adding multiple word tables into word using interop

查看:107
本文介绍了使用互操作将多个单词表添加到单词中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用c#将多个表插入到Word文档中,但是当我在其中添加另一个代码块以添加表时,出现了错误,并且没有插入第二个表.如何将范围下移到页面底部,然后添加另一个表?我尝试使用文档参考结尾创建新范围,但这似乎不起作用,有人可以给我一些帮助吗?

I'm trying to insert multiple tables into a word document using c#, but when I add another block of code to add a table in I am getting an error and the second table is not being inserted. How do I move the range down to the bottom of the page and then add another table? I tried creating a new range using the end of doc reference but this doesn't seem to work, can anyone give me some help?

    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";

我收到以下异常消息:所请求的集合成员不存在"

I get the following exception "the requested member of the collection does not exist"

推荐答案

没有完全遵循您希望布局显示的方式.发布的代码存在两个问题.首先,在将文本添加到第一个表的for循环中,我不确定您在使用以下几行做什么:

Without fully following how you want the layout to appear. There are a couple of issues with the posted code. First in the for loops where you are adding the text to the first table, I am not sure what you are doing with the following lines:

objTab1.Rows[iRow++].Range.Text = s;
objTab1.Rows[iRow].Range.Font.Bold = 1;

第一行中的iRow++增量将抛出表中该行的位置.我猜你可能想要:

The iRow++ increment in the first line is going to throw off where the row is in the table. I am guessing you may want:

objTab1.Rows[iRow].Range.Font.Bold = 1;
objTab1.Rows[iRow].Range.Text = s;
iRow++;

另一个问题是代码如何获取如下所示的最后一段:

The other issue is how the code is getting the last paragraph like below:

object oRng2 = objDoc.Bookmarks.get_Item(ref objEndOfDocFlag).Range;
objPara3 = objDoc.Content.Paragraphs.Add(ref oRng);

oRng2范围是doc范围的结尾,但是,下一行使用oRng,它是文档的顶部.将添加段落更改为适当的范围应该可以解决此问题.

The oRng2 range is the end of doc range however, the next line uses oRng which is the top of the document. Changing the add paragraphs to the proper range should fix this.

objPara3 = objDoc.Content.Paragraphs.Add(ref oRng2); 

希望这会有所帮助.

这篇关于使用互操作将多个单词表添加到单词中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆