none
How can I define multi line column headers in a table RRS feed

  • Question

  • Hi,

    I am programmatically creating a table in a Word document using C# 4 and would like to be able to precisely define how my column header cells text should wrap.

    Here are my 2 rows of column headers (extracted from the object[,] array I am using to populate the Word table):

     {"Sample ID", "Midpoint Depth", "Total Organic Carbon", "S1", "S2", "S3", "Tmax", "Hydrogen Index", "Oxygen Index", "S2-S3 Ratio", "S1-TOC Ratio", "Production Index (PI)"},
     {"", "ft", "wt frac", "mg HC/g Rock", "mg HC/g Rock", "mc CO2/g Rock", "°C", "mg HC/g TOC", "mg CO2/g TOC", "", "", ""},

    I would have liked to be able to specify where the column headers should wrap using \n for example, as follows:

     {"Sample ID", "Midpoint\nDepth", "Total\nOrganic\nCarbon", "S1", "S2", "S3", "Tmax", "Hydrogen\nIndex", "Oxygen\nIndex", "S2-S3\nRatio", "S1-TOC\nRatio", "Production\nIndex (PI)"},
     {"", "ft", "wt frac", "mg HC/g\nRock", "mg HC/g\nRock", "mc CO2/g\nRock", "°C", "mg HC/g\nTOC", "mg CO2/g\nTOC", "", "", ""},

    but that does not work. 

    Note that I do not want to wrap at each blank.  For example, I want the "mg HC/g Rock"  column header to be displayed on 2 lines:

    mg HC/g
    Rock

    not as:

    mg
    HC/g
    Rock

    i.e. I need a way to tell precisely where I want the tetx wrapping to occur, for each header cell.

    How should I do that?
    Please provide some sample C# 4 code.

    Thanks.

    • Changed type Bruce Song Monday, February 14, 2011 9:04 AM
    Thursday, February 3, 2011 11:10 PM

Answers

  • Hi Ericmar,

    Thank you for posting and we are glad to help with you.

    After reading your post, I knew your problem. You want to wrap text in word table cells only via '\n' in C# 4.0. If I have misunderstood you, please let me know.

    I made a small code snippet, it can wrap text via '\n', but before wrapping the text, the table property should be set to be autofitcontent, just like follows:

                    wordTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
                    wordTable.Cell(1, 1).Range.Text = "Happy/g\n New Year!";

    After excuting the code, the Text in Cell(1,1) looks like:

    Happy/g

    New Year!

    I hope it can help you and feel free to follow up after you 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.

    • Marked as answer by Bruce Song Monday, February 14, 2011 9:05 AM
    Friday, February 4, 2011 6:54 AM
  • Hi Bruce,

    Thanks very much for your answers.  I appreciate your fast and useful answers.
    Yes, your answer helped me solve the problem.

    I have however had to:

    • set the Cell WordWrap property to true
    • replace \n by \r in your code snippet as I wanted no vertical space between my cell header lines

    Something like:

    wordTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
    wordTable.Cell(1, 1).WrodWrap = true;
    wordTable.Cell(1, 1).Range.Text = "Happy/g\r New Year!";
    

    Eric

    • Marked as answer by Bruce Song Monday, February 14, 2011 9:05 AM
    Thursday, February 10, 2011 9:44 AM

All replies

  • Hi Ericmar,

    Thank you for posting and we are glad to help with you.

    After reading your post, I knew your problem. You want to wrap text in word table cells only via '\n' in C# 4.0. If I have misunderstood you, please let me know.

    I made a small code snippet, it can wrap text via '\n', but before wrapping the text, the table property should be set to be autofitcontent, just like follows:

                    wordTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
                    wordTable.Cell(1, 1).Range.Text = "Happy/g\n New Year!";

    After excuting the code, the Text in Cell(1,1) looks like:

    Happy/g

    New Year!

    I hope it can help you and feel free to follow up after you 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.

    • Marked as answer by Bruce Song Monday, February 14, 2011 9:05 AM
    Friday, February 4, 2011 6:54 AM
  • Hi again Ericmar,

    Have you got the way to resolve your problem? Does the suggestion work for you or not? If you still show any concern on your problem, just feel free to follow up.

    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.

    Thursday, February 10, 2011 3:52 AM
  • Hi Bruce,

    Thanks very much for your answers.  I appreciate your fast and useful answers.
    Yes, your answer helped me solve the problem.

    I have however had to:

    • set the Cell WordWrap property to true
    • replace \n by \r in your code snippet as I wanted no vertical space between my cell header lines

    Something like:

    wordTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
    wordTable.Cell(1, 1).WrodWrap = true;
    wordTable.Cell(1, 1).Range.Text = "Happy/g\r New Year!";
    

    Eric

    • Marked as answer by Bruce Song Monday, February 14, 2011 9:05 AM
    Thursday, February 10, 2011 9:44 AM
  • Hi Ericmar,

    Thank you for your timely feedback. You are welcome. If you still have any problems, just feel free to follow up. We will try our best to help with you.

    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.

    Monday, February 14, 2011 9:04 AM