Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Mar 2002
    Thanked 0 Times in 0 Posts

    Word Table Manipulation (Word 97)

    I apologize if this seems like such a newbie question or if it has already been covered in detail elsewhere. I searched both the Word forum and the VBA forum, but was not able to find much on this subject.

    Basically, I am re-sorting a table. The original table starts out sorted alphabetically on one of the fields, and then I need to re-sort the table using physical locations, rather than any alpha or numeric sorting characteristics. The specifics of what the new sort does is not really material; suffice it to say that it doesn't fit within the constraints of Word's sorting mechanism.

    My algorithm involves making several passes through the table and copying each row to a new table. The new table should probably be in a new document so the old table is not touched.

    Here is a little bit of code from my program. The net result is the rows from AlphaTable should be copied in their new order into SortTable.

    ' Set our AlphaTable to the first table in the document
    Set AlphaTable = ActiveDocument.Tables(1)

    ' Add a table into which the original table will be sorted.
    Set MyRange = ActiveDocument.Content
    MyRange.Collapse Direction:=wdCollapseEnd
    Set SortTable = ActiveDocument.Tables.Add(Range:=MyRange, NumRows:=AlphaTable.Rows.Count, _

    Then, as I walk through the AlphaTable, each row is addressed by

    MsgBox (AlphaTable.Rows(AlphaIndex))

    My questions deal with the mechanics of Word table manipulation.

    1. What is the easiest way to copy a row from one table to another? I cannot seem to get anything to work, but I am the first to admit I don't even know what rabbit trails to go down to start looking.

    Thank you all in advance for your help.

    bob rench

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Silicon Valley, USA
    Thanked 94 Times in 90 Posts

    Re: Word Table Manipulation (Word 97)

    To maintain the look and feel of your original table, without having to manually "set up" the new table, I am partial to copy and paste, which is easiest with the Selection object. Here's a stab at it:
    <pre>Sub TableRowCopy()
    Dim doc1 As Document, doc2 As Document, row1 As Row
    Set doc1 = ActiveDocument
    Set doc2 = Documents.Add
    With doc1.Tables(1)
    For Each row1 In .Rows
    With Selection
    .Collapse wdCollapseEnd
    End With
    End With
    End Sub</pre>

    The screen does flip back and forth rapidly, but hopefully this is not too irritating for your application. On my PC (800MHz PIII) it's fast!

  3. #3
    Super Moderator
    Join Date
    Dec 2000
    New York, NY
    Thanked 29 Times in 27 Posts

    Re: Word Table Manipulation (Word 97)

    <P ID="edit" class=small>Edited by Gary Frieder on 28-Mar-02 04:53.</P>Here's a rangeified version:

    <pre>Sub TableRowCopyViaRange()

    Dim doc1 As Document, doc2 As Document
    Dim row1 As Row, RowRng As Range
    Dim doc2Rng As Range

    Set doc1 = ActiveDocument
    Set doc2 = Documents.Add
    Set doc2Rng = doc2.Range

    With doc1.Tables(1)
    For Each row1 In .Rows
    Set RowRng = row1.Range
    doc2Rng.Start = doc2Rng.End
    Next 'row1
    End With

    Set doc1 = Nothing
    Set doc2 = Nothing
    Set RowRng = Nothing
    Set doc2Rng = Nothing

    End Sub</pre>


    Edit: changed 'Cut' to 'Copy'!

Posting Permissions

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