none
automating Word - Table Not Measuring Up RRS feed

  • Question

  • I'm using VB.Net 2010 (Pro) to create a Word 2003 document.

    As part of the document, I need to create a table where the overall height of the table, as well as the height of individual rows, are set to a specific value.

    To that end, I use the following code:

    '----------------------------------------

    For i = 1 To UBound(Sections)
        oTable.Rows(i).Height = m_oWord.InchesToPoints(7.25 * (Val(Sections(i).SectionLength) / m_fOverallHeight))
    Next
    oTable.Rows(UBound(Sections) + 1).Height = m_oWord.InchesToPoints(0.75)

    '----------------------------------------

    In the above case the number of sections is 5, the table will have six rows, the first five will be set to the relative height of each section and the last row (6) will be .75" high, for a total height of 8".

    In reality, what's happening is that the overall height of the table is ~8.25" and each row, including the last row, is a little over what the code is supposed to set it.

    So what's going on, and how do I get a little more accuracy in setting the heights? TIA


    Clif

    • Edited by Clif001 Wednesday, December 7, 2011 2:33 PM
    • Moved by Mike Feng Thursday, December 8, 2011 9:40 AM Word (From:Visual Basic Interop and Upgrade)
    Wednesday, December 7, 2011 2:31 PM

Answers

  • Hi Clif

    I'm bearing :-) I'm just glad they're running at the moment. Last night was a big, fat 0.

    So, how is the content of that first row formatted? Is it possible that the font or paragraph formatting is demanding more space than you want to give it?


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Clif001 Thursday, December 8, 2011 3:10 PM
    Thursday, December 8, 2011 2:46 PM
    Moderator
  • Ok, I narrowed it down to one (important) thing.

    For my document, in this case, the table has to be rotated 90° CCW so I added the line:

    oTable.Range.Orientation = wdTextOrientationUpward

    This appears to be the offending line. 

    After doing a little hit r miss, I tried moving the offending line around and found that I needed to place it right after the line that created (added) the table.

    In other words, it now appears to be working correctly thank to you leading me to look at the formatting.

    Thanks.


    Clif
    Thursday, December 8, 2011 3:10 PM

All replies

  • Hi Clif,

    Welcome to the MSDN Forum.

    I have moved this thread to a dedicated forum for better support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, December 8, 2011 9:39 AM
  • Hi Clif

    How much is a "little over"? Word doesn't implement measurement settings exactly. There's always some margin involved that depends on the layouting engine and how it works together with the selected printer driver.


    Cindy Meister, VSTO/Word MVP
    Thursday, December 8, 2011 10:34 AM
    Moderator
  • Actually it's only the first row that seems to be far to high. Here's the rows, what I attempted to set them at and what they actually came out as (approximate as my micrometer's battery died):

    Row  Set       Result 
    1      0.6329  .7812 
    2      1.7500  1.750 
    3      1.6382  1.625 
    4      1.6196  1.625 
    5      1.3590  1.375 
    6      1.0000  1.000 
    
    

    Oh, and since MSDN forums are having issues, I can't format the columns very well so bear with me.


    Clif

    • Edited by Clif001 Thursday, December 8, 2011 2:16 PM
    Thursday, December 8, 2011 2:13 PM
  • Hi Clif

    I'm bearing :-) I'm just glad they're running at the moment. Last night was a big, fat 0.

    So, how is the content of that first row formatted? Is it possible that the font or paragraph formatting is demanding more space than you want to give it?


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Clif001 Thursday, December 8, 2011 3:10 PM
    Thursday, December 8, 2011 2:46 PM
    Moderator
  • I tried removing the text contents of the cells.  No joy.  They are still the same size as before (Row 1 too high, rows 2-6 reasonably the correct height).

    Wait, I tried removing all the formatting, just drew the table, and it seems to be correct.  Now to put back the formatting one at a time and see which one fails.


    Clif
    Thursday, December 8, 2011 3:01 PM
  • Ok, I narrowed it down to one (important) thing.

    For my document, in this case, the table has to be rotated 90° CCW so I added the line:

    oTable.Range.Orientation = wdTextOrientationUpward

    This appears to be the offending line. 

    After doing a little hit r miss, I tried moving the offending line around and found that I needed to place it right after the line that created (added) the table.

    In other words, it now appears to be working correctly thank to you leading me to look at the formatting.

    Thanks.


    Clif
    Thursday, December 8, 2011 3:10 PM