Search code examples
c#office-interopword-automation

Adding multiple word tables into word using interop


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"


Solution

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

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

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

    Hope this helps.