Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't Defeat Evil Tables (Word 2003 VBA)

    I sorely need a fresh pair of eyes to figure another approach to working with all the cells in a table.

    The problem is very simple: I need to loop through and "inspect" each cell in a table. The problem I'm having is that a "For-Each" statement goes into an infinite loop when it hits a pair of rows where the first cell(s) have been merged vertically (it keeps looping back through the same row); and for similar reasons, I can't even use Rows.Count or Columns.Count methods because there are sometimes vertically or horizontally merged cells.

    If I use the selection object, I can sort of use the MoveRight method, but I can't figure out where to stop (so that it doesn't add more rows) because I can't even determine how many rows the table may have!

    I hope someone else has been faced with this, because I'm at wit's end! How can I "move" through every cell without adding new rows?

    Thanks.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can't Defeat Evil Tables (Word 2003 VBA)

    You can use code like this:

    Dim oCell As Cell
    ActiveDocument.Tables(1).Select
    For Each oCell In Selection.Cells
    ' Code to inspect cell goes here
    ...
    Next oCell

    You may not be able to get the row or column number of oCell if there are merged cells. (One more reason not to use merged cells in tables)

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't Defeat Evil Tables (Word 2003 VBA)

    If you've already solved this problem then feel free to ignore me...

    BUT, I once had to iterate thru Word tables in Code (Word 2000) and was getting strange results with some merged cells as well. My solution was to ensure that Word was in PrintLayout mode before iterating thru Word tables in code. Very weird, but it worked...

  4. #4
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't Defeat Evil Tables (Word 2003 VBA)

    Sweet! Thanks for the suggestion. Did you use the selection object? Or were you looping through the Cells collection?
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't Defeat Evil Tables (Word 2003 VBA)

    I used Selection to walk thru the cells. Attached is the code - notice the beginning of the code checks and sets the display property to avoid the infinite loop/weird stepping problems with word tables. My code goes thru a table and merges those cells where the content is the same, hence the name. In order to get it to work, I had to ensure Word was displaying in NormalView. This makes limited sense to me, so regard it as a "gotcha" in the Word VBA Object world...

    HTH
    Attached Files Attached Files

Posting Permissions

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