none
Nested Tables in First Cell of a Row also how to give color to a whole column in a table. RRS feed

  • Question

  • Hi, 

    I want to Create a Nested table in first cell of a row. but when i create a table of 4,4 it show only 1,4 

     

     

                            // Insert Another Table in Cell(1,1)

                            Range rng1 = oTable.Cell(1, 1).Range;

                            Word.Table embtable = wordDocument.Tables.Add(rng1, 4, 4, WdDefaultTableBehavior.wdWord9TableBehavior,  WdAutoFitBehavior.wdAutoFitContent);

                            embtable.set_Style("Table Grid 1");

    Please find the picture below


    Can any one help me in creating the same.

     

    Also I want to assing i color to a whole column of a table. how can i achieve this using C# and Word Interop.

    Regards

    Hemant

     




    • Changed type RobinSa Wednesday, November 16, 2011 1:29 PM
    • Edited by RobinSa Wednesday, November 16, 2011 2:07 PM
    Wednesday, November 16, 2011 1:27 PM

Answers

  • Hi Sakpalhemant,

    Thank you for posting.

    I made a sample of your problem via console application and can reproduce the problem. I found that we need to use the collapse method for the cell range before inserting the table. Please refer to  my modified code snippet:

    using Word = Microsoft.Office.Interop.Word;
    using Microsoft.Office.Interop.Word;
     
    namespace WordInsertTableToTable
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordApp = new Word.Application();
                wordApp.Visible = true;
                Word.Document doc = wordApp.Documents.Open(@"D:\MyWorkingDocuments\WordCases\WordInsertTableToTable\WordInsertTableToTable\bin\Debug\table.docx");
                Word.Table table = doc.Tables[1];
                Word.Range rng = table.Cell(1,1).Range;
     
                object oCollapseStart = Word.WdCollapseDirection.wdCollapseStart;
                // continue after a range
                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                rng.Collapse(ref oCollapseStart);
     
                Word.Table nestTable = doc.Tables.Add(rng, 4, 4, WdDefaultTableBehavior.wdWord9TableBehavior, WdAutoFitBehavior.wdAutoFitFixed);
                nestTable.Columns[1].Shading.BackgroundPatternColorIndex = WdColorIndex.wdBlue; 
            }
        }
    }
    

    The code also contains your requirement to assing the color to a whole column of a table. Please see my screen shot:

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Bruce Song Thursday, November 17, 2011 10:04 AM upload screenshot
    • Marked as answer by Bruce Song Wednesday, November 23, 2011 12:39 AM
    Thursday, November 17, 2011 10:02 AM
  • Hi Hemant

    <<I want to Create a Nested table in first cell of a row. but when i create a table of 4,4 it show only 1,4>>

    Yes, this is a long-standing bug in Word. The only way to work around it is to first create the table, then add the number of rows you want.

    <<Also I want to assing i color to a whole column of a table. how can i achieve this using C# and Word Interop.>>

    You need to do this either cell-by-cell OR by selecting the column, then assigning the formatting.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, November 23, 2011 12:39 AM
    Thursday, November 17, 2011 10:15 AM
    Moderator

All replies

  • Hi Sakpalhemant,

    Thank you for posting.

    I made a sample of your problem via console application and can reproduce the problem. I found that we need to use the collapse method for the cell range before inserting the table. Please refer to  my modified code snippet:

    using Word = Microsoft.Office.Interop.Word;
    using Microsoft.Office.Interop.Word;
     
    namespace WordInsertTableToTable
    {
        class Program
        {
            static void Main(string[] args)
            {
                Word.Application wordApp = new Word.Application();
                wordApp.Visible = true;
                Word.Document doc = wordApp.Documents.Open(@"D:\MyWorkingDocuments\WordCases\WordInsertTableToTable\WordInsertTableToTable\bin\Debug\table.docx");
                Word.Table table = doc.Tables[1];
                Word.Range rng = table.Cell(1,1).Range;
     
                object oCollapseStart = Word.WdCollapseDirection.wdCollapseStart;
                // continue after a range
                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                rng.Collapse(ref oCollapseStart);
     
                Word.Table nestTable = doc.Tables.Add(rng, 4, 4, WdDefaultTableBehavior.wdWord9TableBehavior, WdAutoFitBehavior.wdAutoFitFixed);
                nestTable.Columns[1].Shading.BackgroundPatternColorIndex = WdColorIndex.wdBlue; 
            }
        }
    }
    

    The code also contains your requirement to assing the color to a whole column of a table. Please see my screen shot:

    Hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Bruce Song Thursday, November 17, 2011 10:04 AM upload screenshot
    • Marked as answer by Bruce Song Wednesday, November 23, 2011 12:39 AM
    Thursday, November 17, 2011 10:02 AM
  • Hi Hemant

    <<I want to Create a Nested table in first cell of a row. but when i create a table of 4,4 it show only 1,4>>

    Yes, this is a long-standing bug in Word. The only way to work around it is to first create the table, then add the number of rows you want.

    <<Also I want to assing i color to a whole column of a table. how can i achieve this using C# and Word Interop.>>

    You need to do this either cell-by-cell OR by selecting the column, then assigning the formatting.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, November 23, 2011 12:39 AM
    Thursday, November 17, 2011 10:15 AM
    Moderator
  • Hemant, how about the problem on your side? Do the suggestions help you? If you still show any concern on the problem, just feel free to let us know.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 21, 2011 8:43 AM