Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Karlsruhe, Germany
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Concatenating table cell contents (Word 97-2003)

    Hello,
    I need some robust code to replace the contents of a cell with the concatenation of the contents of two cells in the same table. Should work for all legal cell content types and even if some or all of the cells are empty. It would be nice if no paragraph marks were added/deleted, but for this application that really doesn't matter. The problems I am having are the following:
    --Can't seem to select the contents of a cell as such. Paste often, but not always, pastes a whole new cell. Paste special as text cannot handle some types of content.
    --Can't position the cursor reliably at the end of the cell for pasting the second set of contents into the target cell
    --Merging cells seems to work, but causes problems later on for other reasons

    Thank you for any help

    Ed Colaianni
    Karlsruhe, Germany

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

    Re: Concatenating table cell contents (Word 97-2003)

    Does this code do what you want? It will combine the contents of the first two cells in row 1 of the first table in the document into the third cell. Of course, you'll have replace Tables(1) with a reference to the correct table, and Cell(1, 1) etc. with references to the correct cells.

    Dim rng As Range
    With ActiveDocument.Tables(1)
    .Cell(1, 3).Range.Delete
    Set rng = .Cell(1, 1).Range
    rng.MoveEnd Count:=-1
    If rng.End > rng.Start Then
    rng.Copy
    .Cell(1, 3).Range.Paste
    ' Optional: insert paragraph mark
    .Cell(1, 3).Range.InsertParagraphAfter
    End If
    Set rng = .Cell(1, 2).Range
    rng.MoveEnd Count:=-1
    If rng.End > rng.Start Then
    rng.Copy
    Set rng = .Cell(1, 3).Range
    rng.MoveEnd Count:=-1
    rng.Collapse Direction:=wdCollapseEnd
    rng.Paste
    End If
    End With

  3. #3
    Lounger
    Join Date
    Jan 2001
    Location
    Karlsruhe, Germany
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Concatenating table cell contents (Word 97-2003)

    Thank you Hans for your rapid reply.
    The Delete method doesnt seem to work with the cell-range if the cell is empty, or is there something else wrong with:

    Selection.Tables(1).Rows(picrow).Cells(1).Range.De lete

    What does work is
    Selection.Tables(1).Rows(picrow).Cells(1).Range.Te xt = ""
    Is there any reason not to use that?

    Then the idea of using
    MoveEnd.Count=-1 bothers me. Is the idea to move back behind the end-of-cell marker that is part of the range? This seems to me to be very implementation dependent and perhaps subject to change in the future. Maybe they will add another character, or who knows what. Is there no clean way to select the contents of a cell? Wordbasic.nextcell, prevcell did that, I think, but neither worked for both the first and last cell of a table.

    I have gone back to cells.merge, selecting and copying the resultant cell range, then undoing the merge, emptying the cells and pasting. Any reason why that might cause problems?

    Regards,
    Ed

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

    Re: Concatenating table cell contents (Word 97-2003)

    Selection.Tables(1).Rows(picrow).Cells(1).Range.De lete doesn't cause an error if the cell is empty when I try it, but if setting .Text = "" works better for you, use it by all means.

    The purpose of MoveEnd Count:=-1 was indeed to exclude the cell marker from the range being copied and pasted. This has worked in all recent versions of Word. If Microsoft is going to change the way tables work in future, a lot of existing code will stop working.

    I had thought about merging, copying and undoing the merge. This should work well, but I haven't tested it exhaustively.

  5. #5
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Concatenating table cell contents (Word 97-2003)

    >The Delete method doesnt seem to work with the cell-range if the cell is empty

    The last two lines of this block of code work for me.
    (I'm making copies of a master table at the head of a document, and clearing out column 2, which in the master table contains a description of what should be typed into the cells)
    <pre> Selection.Style = ActiveDocument.Styles("Normal")
    ActiveDocument.Tables(1).Range.Copy
    Selection.Paste
    Selection.MoveLeft Unit:=wdCharacter, Count:=2
    <font color=blue>Selection.Tables(1).Columns(2).Select
    Selection.Delete Unit:=wdCharacter, Count:=1</font color=blue></pre>


Posting Permissions

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