none
Problem using wdRowHeightAtLeast method with multiple tables in Microsoft Word 2010 document (trying to change row height) RRS feed

  • Question

  • I'm having a problem using the wdRowHeightAtLeast method/property in multiple tables in the same Microsoft Word 2010 document.  In the first Table in my document, wdRowHeightAtLeast works with no problems.  This block of code is working correctly:

    'This code is for my YES button. It expands the rows back to their original height

    'using wdRowHeightAtLeast. The hidden rows keep expanding until a row with only one

    'cell is encountered.

    Private Sub OptionButton1_Click() Dim i As Integer i = Selection.Information(wdEndOfRangeRowNumber) i = i + 1 Do While ThisDocument.Tables(1).Rows(i).Cells.Count > 1 Selection.Tables(1).Rows(i).HeightRule = wdRowHeightAtLeast ThisDocument.Tables(1).Rows(i).SetHeight RowHeight:=InchesToPoints(0.4), _ HeightRule:=wdRowHeightAtLeast i = i + 1 Loop End Sub 'This code is for my NO button. It shrinks my rows to .01 inches to make the rows look

    'like they are hidden. The Hidden feature apparently doesn't work for Visual Basic in

    'Microsoft Word documents, that's why I'm shrinking the rows to .01 inches instead.

    'The rows keep shrinking until a row with only one cell is encountered.

    Private Sub OptionButton2_Click() Dim i As Integer i = Selection.Information(wdEndOfRangeRowNumber) i = i + 1 Do While ThisDocument.Tables(1).Rows(i).Cells.Count > 1 i = i + 1 Loop End Sub

    I'm using the EXACT same code for the second Table in my Word document.  The only difference is that I'm using "Tables(2)" everywhere instead of "Tables(1)".  However, I keep getting an error message for this line of code:

    Selection.Tables(2).Rows(i).HeightRule = wdRowHeightAtLeast

    The error message states:  "Run time error '5941':  The requested member of the collection does not exist."

    Has anyone out there encountered this problem before using "wdRowHeightAtLeast"?  Is there something I have to do to "reset" wdRowHeightAtLeast for Tables(2)?

    Thank you in advance,

    Jim

     


    James Hutchinson

    Monday, August 4, 2014 1:37 AM

Answers

  • Dave,

    Actually, I'm afraid I spoke too soon about the code that you provided.  It doesn't work the way I want it to.  Using your code, when I click the YES button, it expands every row in my Table.  I do not want that to happen.  And since I was using similar code for the NO button, I think it was shrinking every row in my Table.  I think this is what was causing the graphics display inside my document to get screwed-up.

    My code that I provided at the very beginning of this thread actually does work correctly.  However, I forgot to provide the following line of code in my code excerpt at the beginning of this thread:

    For my NO button code, I forgot to include this right before "i = i + 1" in the DO...WHILE loop:

    "ThisDocument.Tables(1).Rows(i).SetHeight RowHeight:=InchesToPoints(0.01), _
      HeightRule
    :=wdRowHeightAtExactly"

    I will try to insert this line in my first post at the top.

    The problem regarding my additional tables that I spoke about at the beginning is that I was referencing the wrong table numbers after Tables(1).  I should have been referencing Tables(3), not Tables(2).  I forgot that my Tables(2) was a small, insignificant table containing nothing but title information and no VB code, so I didn't think about it as a separate table.  I feel pretty dumb about it, but that was the root of my problems. 

    Jim


    James Hutchinson


    • Marked as answer by jhutch03 Sunday, August 10, 2014 3:24 PM
    • Edited by jhutch03 Sunday, August 10, 2014 3:27 PM typo
    Saturday, August 9, 2014 6:11 AM

All replies

  • The problem with your code is its use of i = Selection.Information(wdEndOfRangeRowNumber). Unless you're moving the selection to the second table, it will continue to refer to whatever row was selected in the first table and, if that is more than the row count for the second table, it'll throw an error. Even if it the selected row in the first table is no more than the row count in the second table, you're liable to find the code starting at the wrong line. In any event there is no need to select anything or use a loop. Try:

    Private Sub OptionButton1_Click()
    Dim Rng As Range
    Set Rng = ThisDocument.Tables(1).Range
    With Rng
      If .Rows.Count = 1 Then Exit Sub
      .MoveStart wdRow, 1
      .Rows.SetHeight RowHeight:=InchesToPoints(0.4), HeightRule:=wdRowHeightAtLeast
    End With
    Set Rng = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, August 4, 2014 2:46 AM
  • Paul,

    The code works beautifully.  Thank you very much! 

    The only thing I had to change was that it should be "If .Cells.Count = 1", not "If .Rows.Count = 1".

    Could you also tell what If condition I can use to test for the end of a Table?  That is the other scenario/if condition the Tables in my Word document may encounter, besides "Cells.Count = 1".

    Thanks,
    Jim


    James Hutchinson

    Tuesday, August 5, 2014 2:03 PM
  • Perhaps you could explain what it is you're trying to achieve and why you believe it was necessary to modify the code? The code I provided already 'finds' the end of the table, via the Rng variable.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, August 5, 2014 9:55 PM
  • That makes sense.  Duh, sorry.  I'll verify tonight that it works correctly at the end of the Tables and I'll let you know if I encounter a problem. 


    James Hutchinson

    Tuesday, August 5, 2014 10:47 PM
  • Paul,

    So the YES button code works like a charm.  However, I'm having trouble with the NO button code (collapsing of the height of the rows).  This is the code I'm using, which is very similar to the code that you provided me a couple of days ago:

    Private Sub OptionButton1_Click()
    Dim Rng As Range
    Set Rng = ThisDocument.Tables(1).Range
    With Rng
      If .Rows.Count = 1 Then Exit Sub
      .MoveStart wdRow, 1
      .Rows.SetHeight RowHeight:=InchesToPoints(0.01), HeightRule:=wdRowHeightAtExactly
    End With
    Set Rng = Nothing
    End Sub
    --------------------------------------------------------------------------------

    I do not receive an error message when I run this code.  Instead of an error, the graphics inside my Word document become all screwy when I click the NO button and the graphics stay screwed-up unless I close and re-open the Word document.   I don't really know another way to explain the error.

    I tried changing the height of the row from .01 to .1, but I still had the same problem.

    Thanks,
    Jim


    James Hutchinson

    Wednesday, August 6, 2014 2:46 PM
  • Since the macro has nothing to do with any graphics, I have no idea what you mean by "the graphics inside my Word document become all screwy" - and that's hardly a problem description you could expect anyone to work with.

    The only thing I can think of that would affect any graphics is if they are anchored to cells in the table, or to content occurring after it. In such cases, the graphics are liable to move higher on the page and that may cause overlapping and other, related, layout issues. But, since closing and re-opening the document seems to resolve the issue, I'd say it's more likely to be a problem with your display driver. Accordingly, I'd suggest checking the maker's website for an updated driver.


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Edited by macropodMVP Saturday, August 9, 2014 12:24 PM
    Thursday, August 7, 2014 1:28 AM
  • Paul,

    If I could describe it, I would.  When I click the NO button, the document turns mostly black and it looks like there may be dozens of tiny rows appearing, it's hard to say.  Since the NO button is shinking the rows to .01 inches, that is somewhat understandable.  However, the code should stop shrinking the rows when it reaches a row with a cell count of 1.  Only 3 or 4 rows should get shrunk to .01 inches.

    Because of the jumbled screen, it's hard to tell what is really happening.

    Is there any way I can send you a copy of the document and you could see for yourself what I'm talking about?

    I will try using the document from a couple of different computers and see if I still run into the same issue.

    Thanks,

    Jim


    James Hutchinson

    Thursday, August 7, 2014 2:36 PM
  • As I've already said, this is liable to be a display driver issue. Your additional info reinforces that impression.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, August 7, 2014 11:12 PM
  • Dave,

    Actually, I'm afraid I spoke too soon about the code that you provided.  It doesn't work the way I want it to.  Using your code, when I click the YES button, it expands every row in my Table.  I do not want that to happen.  And since I was using similar code for the NO button, I think it was shrinking every row in my Table.  I think this is what was causing the graphics display inside my document to get screwed-up.

    My code that I provided at the very beginning of this thread actually does work correctly.  However, I forgot to provide the following line of code in my code excerpt at the beginning of this thread:

    For my NO button code, I forgot to include this right before "i = i + 1" in the DO...WHILE loop:

    "ThisDocument.Tables(1).Rows(i).SetHeight RowHeight:=InchesToPoints(0.01), _
      HeightRule
    :=wdRowHeightAtExactly"

    I will try to insert this line in my first post at the top.

    The problem regarding my additional tables that I spoke about at the beginning is that I was referencing the wrong table numbers after Tables(1).  I should have been referencing Tables(3), not Tables(2).  I forgot that my Tables(2) was a small, insignificant table containing nothing but title information and no VB code, so I didn't think about it as a separate table.  I feel pretty dumb about it, but that was the root of my problems. 

    Jim


    James Hutchinson


    • Marked as answer by jhutch03 Sunday, August 10, 2014 3:24 PM
    • Edited by jhutch03 Sunday, August 10, 2014 3:27 PM typo
    Saturday, August 9, 2014 6:11 AM
  • Hi Jim

    So, is your problem solved? If yes, please mark one or more replies in this discussion as an answer.

    If not, please re-state your problem as it now stands as things have become rather confusing...


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, August 9, 2014 5:14 PM
    Moderator
  • I just marked my previous post as the answer.

    Jim


    James Hutchinson

    Sunday, August 10, 2014 3:28 PM
  • I just marked my previous post as the answer


    Why? How did my replies of 4, 5 & 7 August not answer each of your questions but your previous post did?

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, August 10, 2014 9:18 PM