+ Reply to Thread
Results 1 to 13 of 13

Reading a table from Word

  1. #1
    Registered User
    Join Date
    05-30-2010
    Location
    hk
    MS-Off Ver
    Excel 2003
    Posts
    17

    Reading a table from Word

    I have a table consists of 1 column and a few rows, let's say 5 in this case. (so 5*1 table)

    I now have a VBA to read the values from the HIGHLIGHTED table row by row into an array.

    My question is:

    How could I let the program knows the which cell is the last one which I required additional treatment?
    In this case, how can I instruct my VBA to do additional work on cell (1,5)?

    Any function that I can call in a word VBA?
    thanks.
    Last edited by christheta; 06-26-2012 at 01:58 AM.

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Reading a table from Word

    Hi christheta,

    If you want to edit the last cell in a table, it really doesn't concern the array - you just edit the cell's contents. Whether you do that before, or after, populating the array depends on whether you want the array to contain the original or edited data. Conversely, if you want to change the data in the array, it really doesn't matter where the data came from - you just need to know how to identify the array element.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    05-30-2010
    Location
    hk
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Reading a table from Word

    Sorry for not being able to make myself clear.
    I was actually trying to tell my program to recognize cell(1,5) is the last cell of the highlighted table thus I can flag the value obtained from this last cell.

    Any function that I can tell cell (1,5) is the last cell of the highlighted range? (note I want the table size to be changed from time to time, not 1,5 all the time)

  4. #4
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Reading a table from Word

    Cross-posted at: http://www.vbaexpress.com/forum/showthread.php?t=42699
    PLEASE READ RULE 8!!!
    For cross-posting etiquette, please also read: http://www.excelguru.ca/content.php?184

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Reading a table from Word

    If all you're interested in the the content of the last cell in the table, the array is of no consequence. All you need is something like:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-30-2010
    Location
    hk
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Reading a table from Word

    Oops, terribly sorry for not knowing the rule here.
    I posted over there as I thought I had made a mistake posting a Word VBA question in an EXCELforum. I now realised that is wrong. sorry for that.

    I have deleted the post in VBA express. And thanks for your time in explaining how it works here.

    And if you don't mind, would you still be kind enough to help me out here?

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Reading a table from Word

    Quote Originally Posted by christheta View Post
    I thought I had made a mistake posting a Word VBA question in an EXCELforum.
    But that's why EXCELforum has a Word programming area...
    And if you don't mind, would you still be kind enough to help me out here?
    Sure.

  8. #8
    Registered User
    Join Date
    06-26-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2003, Word 2003
    Posts
    2

    Re: Reading a table from Word

    Alternately you can set an cell OBJECT for the last cell of any table, and then can action it however you like.

    Dim aCell As Cell
    Set aCell = Selection.Tables(1).Range.Cells( _
    Selection.Tables(1).Range.Cells.Count)
    aCell.Range.Text = "yadda"
    aCell.Range.Font.Color = wdColorBrightGreen
    ' or whatever you want to do with the Cell

    Note that for the above, the cell object is set for the last cell of the table the Selection is in. If the Selection is NOT in the table, the code will fail. Obviously this should be properly error-trapped.

  9. #9
    Registered User
    Join Date
    05-30-2010
    Location
    hk
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Reading a table from Word

    Quote Originally Posted by fumei View Post
    Alternately you can set an cell OBJECT for the last cell of any table, and then can action it however you like.

    Dim aCell As Cell
    Set aCell = Selection.Tables(1).Range.Cells( _
    Selection.Tables(1).Range.Cells.Count)
    aCell.Range.Text = "yadda"
    aCell.Range.Font.Color = wdColorBrightGreen
    ' or whatever you want to do with the Cell

    Note that for the above, the cell object is set for the last cell of the table the Selection is in. If the Selection is NOT in the table, the code will fail. Obviously this should be properly error-trapped.
    Thanks a lot. It works well for manipulating the last cell when my selection includes the last cell of the table.

    But if I select the first column of the 4*4 table. the last cell referred by this object would then be cell(4,4) which is outside my selection. Any other clue?

  10. #10
    Registered User
    Join Date
    05-30-2010
    Location
    hk
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Reading a table from Word

    Please Login or Register  to view this content.
    I am now trying to read every cell into an array, and flag the last element of the array for my calculation.

    But it prompts me for an error 9, Subscript out of range

    Could you show me how to make it work?

    (p.s. Text2long converts text in word to long type)

  11. #11
    Registered User
    Join Date
    05-30-2010
    Location
    hk
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Reading a table from Word

    Thank you so much guys. You gave me a lot of insights.

  12. #12
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Reading a table from Word

    Quote Originally Posted by christheta View Post
    But if I select the first column of the 4*4 table. the last cell referred by this object would then be cell(4,4) which is outside my selection. Any other clue?
    In that case:
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    06-26-2012
    Location
    Vancouver, British Columbia
    MS-Off Ver
    Excel 2003, Word 2003
    Posts
    2

    Re: Reading a table from Word

    As macropod has stated, you do not need to use an array. uou can action thw last cell of the Selection ditectly.
    Again, working with a Cell object...

    Sub LastCellofSelection()
    Dim aCell As Cell
    Set aCell = Selection.Tables(1).Range.Cells(Selection.Range.Cells.Count)
    aCell.Range.Text = "yadda"
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1