Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Jan 2004
    Location
    Prague, Czech Republic
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Manipulating Tables in Word (Word2k)

    Hello,
    what is the code that accesses a cells in a table in Word?

    For instance, I have a table 10 x 4 and I would like to copy first two columns, but only e.g. 2nd, 3rd, 5th and 9th row into a new table 5x2. (headings in row 1).
    I tried to apply Range(Cells) but no success so far.

    Thank you very much

    Martin
    Regards,
    Martin

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

    Re: Manipulating Tables in Word (Word2k)

    Something like this:

    ' Module-level variables
    Private tblSource As Table
    Private tblTarget As Table
    Private lngTargetRow As Long

    ' ______________

    ' This is the macro to run
    Sub CopySomeCells()
    Selection.EndKey Unit:=wdStory

    ' Set table references
    Set tblSource = ActiveDocument.Tables(1)
    Set tblTarget = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=5, NumColumns:=2)

    ' Initialize target row
    lngTargetRow = 0

    ' Copy specified rows
    CopyRow 2
    CopyRow 3
    CopyRow 5
    CopyRow 9

    ' Release object memory
    Set tblTarget = Nothing
    Set tblSource = Nothing
    End Sub

    ' ______________

    ' Auxiliary procedure to copy cells from one row
    Sub CopyRow(lngRow As Long)
    Dim lngCol As Long

    ' Go to next row in target table
    lngTargetRow = lngTargetRow + 1

    ' Copy cells from columns 1 and 2
    For lngCol = 1 To 2
    tblSource.Cell(lngRow, lngCol).Range.Copy
    tblTarget.Cell(lngTargetRow, lngCol).Range.Paste
    Next lngCol
    End Sub

  3. #3
    Star Lounger
    Join Date
    Sep 2001
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Manipulating Tables in Word (Word2k)

    For example you might use Activedocument.Tables(1).Rows(3).Range to access the 3rd row. (Of course you might reference the table by other ways than using Activedocument as well.)

    Regards,
    hedgehog

  4. #4
    Lounger
    Join Date
    Jan 2004
    Location
    Prague, Czech Republic
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Manipulating Tables in Word (Word2k)

    Many thanks to both of you,
    Hans, this works a treat!
    Regards,
    Martin

Posting Permissions

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