Results 1 to 11 of 11
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Determining Row Height (Word 2003)

    I would like to be able to use VBA to determine the height of a table row that does not have a specified height. In other words, if I have a row for which the Specify height check box is clear (not checked).

    If I try:

    MsgBox Selection.Rows(1).Height

    for a row with no specified height (the height increases as I had content and decreases as I remove content), then I get the height:

    9999999

    In most cases I want to DISallow rows breaking across pages, but for very large rows, rows that would not show all the data especially, I want to allow it. I know how to turn it on and off, but I just don't know how to tell how big the row is.

    Please help!!

    Thanks!!
    Troy

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Determining Row Height (Word 2003)

    There must be a way, but I have no idea what it is. In fact, everything I tried was very frustrating:
    <UL><LI>Calculate the vertical top of the range of the cell contents and compare with the next row (or text following table). Not accurate due to "space before" settings.
    <LI>Use Range.ComputeStatistics to count lines inside table cells. Always seems to return 0 (???).[/list]Must eat dinner now, sorry!

  3. #3
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Determining Row Height (Word 2003)

    I also tried a few similar approaches with no result.

    StuartR

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Wellington, New Zealand
    Posts
    143
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Determining Row Height (Word 2003)

    To find the height of the tallest row in a table (or part of one) it should be possible to use the DistributeHeight command then read the resulting height - then undo the DistributeHeight command, presumably. Doing this, two rows at a time, may give you the height of each one (though I haven't tested it).

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Determining Row Height (Word 2003)

    Good idea. See the attached for testing.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Determining Row Height (Word 2003)

    Very cute, nice solution.

    StuartR

  7. #7
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining Row Height (Word 2003)

    <P ID="edit" class=small>(Edited by TroyWells on 22-Sep-06 00:42. )</P>Thanks for this great example!! However, I had the following errors:

    1. When I tried to paste the text into a module a got the following error:

    "Compile error: Syntax error"

    The problem seems to be with the first line:

    <pre>Attribute VB_Name = "modLounge20060919"</pre>



    What does this line do?

    2. If I commented out that line, the function works fine until it gets to a row that is too big to be seen. At that point, it gives the following error:

    Run-time error '4198': Command failed

    It is failing at the following line:

    <pre>rngTemp.Rows.DistributeHeight</pre>



    If I delete enough text out of that row so that it is entirely visible, it works fine.

    Obviously, this defeats the purpose of the function since it is needed to measure the height of rows that are larger than the page.

    Any ideas?

    Thanks!!
    Troy
    -------------------------------EDIT
    P.S. ACTUALLY, THIS SOLUTION WILL WORK FOR ME, but probably not for others who need to really know the height of this row.

    For me, all I need to do is create an "on error" statement where if the error number is 4198, I change the selected row to allow it to break across pages.

    What do you think?

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Determining Row Height (Word 2003)

    Troy, it's an exported module with a .txt extension. I should have added these directions: right-click the link and save the file to your computer, renaming the extension to .bas and then import into your template for using the VBE's File>Import command.

    > Obviously, this defeats the purpose of the function since it is needed to measure the height of rows that
    > are larger than the page

    "Obviously" that scenario never occurred to me, and I certainly didn't test it.

    > all I need to do is create an "on error" statement where if the error number is 4198, I change the selected
    > row to allow it to break across pages.

    Okay. I don't understand your document, but I'll take your word for it. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  9. #9
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining Row Height (Word 2003)

    Sorry for the confusion. I am generating Word documents from RoboHTML and then trying to clean them up with VBA as much as possibly.

    As I said in my orignal post, in most cases I want to DISallow rows breaking across pages, but for very large rows, rows that would not show all the data especially, I want to allow it. I know how to turn it on and off in VBA, but I just don't know how to tell how big the row is to tell whether it is too big t shw everything.

    Now, thanks to this error, of all things, I am able to detect rows that are too big. I have attached the text of my finished module. Most of it is the same as what you gave. I just added my error routine and a line to delete the added temp row.

    Thanks again for your help!!
    Troy

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Determining Row Height (Word 2003)

    Okay, with greater clarity comes other inspiration. You really need to know whether the bottom line of text in any cell in the row is below the bottom margin. This is much easier to ascertain. See the attached. It's a module renamed with a .txt extension and can be imported into the VBE as described in an above post. Hopefully it's faster, too, although I certainly haven't tried to benchmark it.

  11. #11
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Determining Row Height (Word 2003)

    Thanks to your fine example, I was able to come up with a script that is much faster (for a 300 page document with many tables it took 12 seconds compared to 6:45).

    Please don't take that as a criticism. Yours is much more elegant, and I learned much from it. I just didn't need it all.

    The following is my script:

    <pre>Sub GetHeightInInchesForTooBigRows()
    Dim intCounter As Integer
    Dim aTable As Table
    Dim I1 As Integer

    'This error statement handles rows with merged cells.
    On Error GoTo CombinedCellError
    GoTo AfterCombinedCellError
    CombinedCellError:
    If Err.Number = 5941 Then
    aTable.Rows(I1).Select
    aTable.Cell(I1, Selection.Columns.Count).Select
    Resume Next
    End If
    AfterCombinedCellError:
    Selection.HomeKey Unit:=wdStory

    For Each aTable In ActiveDocument.Tables
    For I1 = 1 To aTable.Rows.Count
    aTable.Cell(I1, aTable.Columns.Count).Select
    Selection.MoveRight
    Selection.MoveLeft
    If Selection.Information(wdVerticalPositionRelativeTo Page) > 715 Then
    Selection.Rows(1).AllowBreakAcrossPages = True
    End If
    Next I1
    Next aTable
    End Sub</pre>


    Thanks for your help!!
    Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •