none
Concern for Cell.Split method in word RRS feed

  • Question

  • Hi, I am using Cell.Split method to split one cell to multi ones. Everything is ok when width of per spilted cell is larger than 4.2 mm.

    However, when per spilted cell width is samller than 4.2mm, such as, width of previous cell  is 20mm, and splite it to 5 cells(means width of per cell is 4mm), the width of cell will become 4.2mm automatically. Please note that left/right padding of cell have been set to 0 already !

    I have been pending due to this for weeks, and found no documents/links elaborating this.

    Any idea/suggestion will be really appreciated !  Following is my code snippet. Thanks

    object oMissing = System.Reflection.Missing.Value;
    wordApp = new Application();
    wordApp.Visible = false;
    object fileName = @"D:\test.doc";
    wordDoc = wordApp.Documents.Open(FileName: ref fileName);

    //
    Table tableObj = wordDoc.Tables[1];
    tableObj.RightPadding = tableObj.LeftPadding = 0;

    object rowCount = 1;
    object columnCount = 10;
    tableObj.Cell(1, 1).Split(ref rowCount, ref columnCount);


    • Edited by Ruwu zhang Sunday, July 15, 2012 9:48 AM
    Sunday, July 15, 2012 9:47 AM

Answers

  • Cell widths are stored as twips, so there may be a theoretical minimum width of 1 twip, but the determination of actual cell widths is very complex, and depends on various factors such as settings on other cells, border widths, etc. What happens when you split a cell is not necessarily what you would first expect and you may need to explicitly set the widths you want after the split.
     
    It does seem awkward to manipulate cell widths below 1 point in the Word UI, but by massaging the XML, I can create one 6 twips wide - utterly useless of course, and far narrower than you would want - so you should be able to get what you want, albeit with a bit of trial and error.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, July 16, 2012 12:01 PM
  • Instead of using the SetWidth method, try just setting the Width property directly (and ignore the PreferredWidth - Word can ignore your preferences).
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, July 16, 2012 3:02 PM
  • Word cells do _not_ have an absolute minimum size. The minimum size of any table cell depends on a variety of factors and Word may override the value you try to set but that is because of other settings, not because of any limits. A cell can be a point or less in width.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, July 24, 2012 12:08 PM

All replies

  • Hi Ruwu,

    Thank you for posting in the MSDN Forum.

    I'll involve some experts who are more familiar with your issue. This may take some time.

    Much appreciate for your patient and understanding.

    Best regards,
    Quist

    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 16, 2012 7:54 AM
    Moderator
  • Word cells do have a minimum size, no matter how empty they are and how little padding they contain. The font size will require a certain amount of space, which may be what you're seeing. I can get it down to 2.8mm if I format the text as "hidden", but it won't go any smaller than that.

    Cindy Meister, VSTO/Word MVP

    Monday, July 16, 2012 8:04 AM
    Moderator
  • Cell widths are stored as twips, so there may be a theoretical minimum width of 1 twip, but the determination of actual cell widths is very complex, and depends on various factors such as settings on other cells, border widths, etc. What happens when you split a cell is not necessarily what you would first expect and you may need to explicitly set the widths you want after the split.
     
    It does seem awkward to manipulate cell widths below 1 point in the Word UI, but by massaging the XML, I can create one 6 twips wide - utterly useless of course, and far narrower than you would want - so you should be able to get what you want, albeit with a bit of trial and error.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, July 16, 2012 12:01 PM
  • Hi Tony, Thanks for your time and comments.

    Actually, I have tried to explicitly set widths of cells after splitting operation. However, if width is below 4mm, Cell.SetWidth will raise exception.

    And also tried Cell.PreferredWidth property, although it won't raise exception, the setting width won't be effective.

    That's really confused/frustrated me.

    Could u happen to know how to set narrow cell width with C# API ? When I operate in word directly, seems I can get narrow cell.

    Thanks in advance.

    Monday, July 16, 2012 1:43 PM
  • Instead of using the SetWidth method, try just setting the Width property directly (and ignore the PreferredWidth - Word can ignore your preferences).
     

    Enjoy,
    Tony
    www.WordArticles.com
    Monday, July 16, 2012 3:02 PM
  • Hi Ruwu,

    Please verify that the error you see is ‘4608’, which translates to “Value out of range.” The cell/column minimum width smaller than 11 points (220 twips) is out of range.

    Using the sample code in the Open XML SDK\V2.0\Doc\OpenXmlSdk.chm for “How to Insert a Table into a Word Processing Document”  You can set the width of the cell to 0 and still end up with a width of 11.2 points the equivalent of 0.15555555 inches, which translates to 3.9511097 mm.  The twips equivalent of 11.2 points is 224 twips.

    Word limits the width of a table cell to enough space to hold a single character assuming a character width of 224 twips. The point size of the default font does not make a difference.

    Here is the modified C# code from the sdk that shows xml that doesn’t control theminimum cell width.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;

    class Program { static void Main(string[] args) { CreateTable(@"D:\Forums\Cell Width\Cell Width.docx"); } // Insert a table into a word processing document. public static void CreateTable(string fileName) { // Use the file name and path passed in as an argument // to open an existing Word 2007 document. using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true)) { // Create an empty table. Table table = new Table(); // Create a TableProperties object and specify its border information. TableProperties tblProp = new TableProperties( new TableBorders( new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Thick), Size = 4 }, new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Thick), Size = 4 }, new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Thick), Size = 4 }, new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Thick), Size = 4 }, new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Thick), Size = 4 }, new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Thick), Size = 4 } ) ); // Append the TableProperties object to the empty table. table.AppendChild<TableProperties>(tblProp); // Create a row. TableRow tr = new TableRow(); // Create a cell. TableCell tc1 = new TableCell(); // Specify the width property of the table cell. tc1.Append(new TableCellProperties( new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = " " })); // Specify the table cell content. tc1.Append(new Paragraph(new Run(new Text(" ")))); // Append the table cell to the table row. tr.Append(tc1); // Create a second table cell by copying the OuterXml value of the first table cell. TableCell tc2 = new TableCell(tc1.OuterXml); // Append the table cell to the table row. tr.Append(tc2); // Append the table row to the table. table.Append(tr); // Append the table to the document. doc.MainDocumentPart.Document.Body.Append(table); } } } }

    The file passed by the console main call is an empty Word 2010 .docx document.



    Please remember to mark the replies as answer if they help and unmark them if they provide no help. and click "Vote as Helpful" this and other helpful posts, so other users will see your thread as useful. Best Regards, Chris Jensen

    • Proposed as answer by Sirisha95 Tuesday, January 8, 2013 6:04 AM
    Monday, July 23, 2012 5:54 PM
    Moderator
  • Word cells do _not_ have an absolute minimum size. The minimum size of any table cell depends on a variety of factors and Word may override the value you try to set but that is because of other settings, not because of any limits. A cell can be a point or less in width.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, July 24, 2012 12:08 PM