none
Table Row Height RRS feed

  • Question

  • I have a Word Document of tables that I've created programmatically (using Interop). The default is I'm not specifying a row height. However, when I copy and paste these tables into Microsoft Publisher (2010), the table row spacing becomes excessive.

    So, I've resorted to specifying a "Fixed Row Height" of .17" for each row height such as:

    mCurrentTable.Cell(mCurrentTableRow, column).Range.ParagraphFormat.LineSpacing = 12.0f;

    mCurrentTable.Cell(mCurrentTableRow, column).Range.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;


    Now, when I copy and paste the table, it looks fine in Publisher.

    But, my problem is that not all table rows are one line of text. Programmatically, how do I either:

    1) Determine the row height (prior to setting an Exact Line Spacing) so that I can use this line spacing for an Exact measurement.

    2) Determine part of the text in a row isn't visible if I set the LineSpacing to a fixed height.

    In short, how do I specify a fixed row height based on how many lines of text are visible in the row?


    • Edited by ZooGuy1492 Saturday, April 12, 2014 2:19 PM
    Saturday, April 12, 2014 2:18 PM

Answers

  • Actually, calculating the required row height addition for a given row is quite easy: simply get the vertical positions of the first and last characters in each cell on the row (or at least the one that is liable to have wrapped text. Subtract the first character's position from the last and anything more than 0 is the amount by which you need to increase the row height. For example:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim TblRw As Row, TblCll As Cell, sBaseHght As Single, sTmpHght As Single, sRwHght As Single
    sBaseHght = CentimetersToPoints(0.6)
    With ActiveDocument.Tables(1)
      For Each TblRw In .Rows
        sRwHght = 0
        With TblRw
          For Each TblCll In .Cells
            With TblCll.Range.Characters
              sTmpHght = .Last.Previous.Information(wdVerticalPositionRelativeToPage) - _
                .First.Information(wdVerticalPositionRelativeToPage)
            End With
            If sTmpHght > sRwHght Then sRwHght = sTmpHght
          Next
          .HeightRule = wdRowHeightExactly
          .Height = sRwHght + sBaseHght
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by ZooGuy1492 Thursday, April 24, 2014 1:38 PM
    Thursday, April 24, 2014 9:48 AM

All replies

  • Hi Zooguy

    Mmm. In contrast to Publisher, Word is based on laying out text flow dynamically, rather than statically, so what you're looking for isn't supported in the Word object model and there's no really reliable way to accomplish what you ask, it can only be done round-about.

    So it might make sense, first, to see if the cause for the "bad" row height pasting into Publisher can be established and try to do something about that...

    I'm not really familiar with Publisher so I have to ask: Do you know of circumstances where copying/pasting tables from Word do not result in the undesirable behavior?


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, April 12, 2014 3:06 PM
    Moderator
  • Here is what I'm talking about. Here are the tables in Word:

    Tables in Word

    The first table uses the default table row spacing while the second table uses an Exact Height of 0.17" for each table row.

    Then, these tables were copied to Microsoft Publisher 2010 and here is what it looks like (when saved to a PDF file):

    Tables in Publisher

    I don't like the large row height (in the top table) in Publisher so I've been specifying an Exact Row Height in Microsoft Word so the tables are a little tighter. But, as I said in my previous post, I have trouble when the table row has too much text that result in the row height needing to 2 rows high (e.g. .34") instead of just one.

    Thursday, April 24, 2014 2:42 AM
  • Actually, calculating the required row height addition for a given row is quite easy: simply get the vertical positions of the first and last characters in each cell on the row (or at least the one that is liable to have wrapped text. Subtract the first character's position from the last and anything more than 0 is the amount by which you need to increase the row height. For example:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim TblRw As Row, TblCll As Cell, sBaseHght As Single, sTmpHght As Single, sRwHght As Single
    sBaseHght = CentimetersToPoints(0.6)
    With ActiveDocument.Tables(1)
      For Each TblRw In .Rows
        sRwHght = 0
        With TblRw
          For Each TblCll In .Cells
            With TblCll.Range.Characters
              sTmpHght = .Last.Previous.Information(wdVerticalPositionRelativeToPage) - _
                .First.Information(wdVerticalPositionRelativeToPage)
            End With
            If sTmpHght > sRwHght Then sRwHght = sTmpHght
          Next
          .HeightRule = wdRowHeightExactly
          .Height = sRwHght + sBaseHght
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by ZooGuy1492 Thursday, April 24, 2014 1:38 PM
    Thursday, April 24, 2014 9:48 AM
  • Thanks Paul... That seemed to work.

    Thursday, April 24, 2014 1:40 PM