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

    Range place row of text in table (Word2000)

    Is there a way to replace an entire row of text of a table without resorting to a cellular loop?
    In the sample code below, each commented effort fails; the last effort demonstrates that a split would work if I looped on each cell.
    <pre>Sub test()
    ' Needs a table of at least two rows, say 4 cells
    Dim rng As Range
    Set rng = ActiveDocument.Tables(1).Rows(1).Range
    rng.Select
    Dim strText As String
    strText = rng.Text
    Debug.Print strText
    Dim lng As Long
    For lng = Len(strText) To Len(strText) - 30 Step -1
    Debug.Print Asc(Mid$(strText, lng, 1))
    Next lng
    Set rng = ActiveDocument.Tables(1).Rows(2).Range
    rng.Select
    ' rng = strText
    ' rng.Text = strText
    ' rng.Rows(1).Range = strText
    ' rng.Rows(1).Range.Text = strText
    rng.Cells(1).Range = Split(strText, Chr$(13) & Chr$(7))(0)
    End Sub</pre>


    This example (at the foot) seems to indicate that a cellular loop is necessary.

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

    Re: Range place row of text in table (Word2000)

    Don't think so. You can copy and paste a row, but Word will insert a new row.

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

    Re: Range place row of text in table (Word2000)

    The text of the cells in a table is actually stored row-wise (separated by Chr(13) and Chr(7) characters as you have found), so you can retrieve the text of an entire row at once, for what it's worth. But the text of the cells in a column is not stored contiguously, so looping is the only way to get at it.

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

    Re: Range place row of text in table (Word2000)

    Thnaks, Hans. I thought I might have missed something. There's a related anomaly with Columns vs. Rows. I can get the text (with cell delimiters) of a row with .Rows(1).range.text, but with columns .Columns(1).range.text doesn't seem so good. I have resorted (for columns) to a loop through cells accumulating each cell's text.
    (later)
    There's another slightly-ever-so-weird thing with cells.
    Selecting a rectangular array of cells within a table (cells 20 through 39 in my example): To process each cell in the selection I must use "For Each cl In Selection.Cells". Using "For Each cl In Selection.Range.Cells" gives me a set of cells in each ROW described by the selection, as far as I can see (cells 19 through 42 in my example).

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Range place row of text in table (Word2000)

    Chris, I think we did the "selection vs. range in a table" thread a few years ago. That's just the way it is!

    As for the replacing of an entire row using VBA, it's an interesting question. Inasmuch as tabular data typically consists of discrete units, however, it seems sufficiently efficient to apply the changes in a cellwise fashion. (At least sufficiently efficient that I am not tempted to bang my head against this particular wall. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> )

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: Range place row of text in table (Word2000)

    I suppose one could split the table, delete the row to be replaced, insert the (delimited) text on the split line, convert that to a table, then re-align the columns (if necessary). So, yes, one could do as Chris wants, but it's hardly efficient.

    Cheers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

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

    Re: Range place row of text in table (Word2000)

    > but it's hardly efficient.
    In CPU terms, maybe. I was concerned with writing/maintenance efficiency.
    I couldn't find any examples of "Inserting a Row" or "Inserting a Column" without a loop.
    Confirmation that it's not possible is OK. I thought maybe I'd missed a concise method in VBA.
    Thanks.

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

    Re: Range place row of text in table (Word2000)

    If you select the cells you want to end up in the table as cells and not as a whole row, then I think you get the effect you need. There is probably a more elegant way to do that, but I have found that selecting the first cell and then running:
    Selection.MoveRight unit:=wdCharacter, Extend:=wdExtend, Count::=<number of cells in the row less 1>
    works (in Word 97 and 2002).
    Then you can copy the selection, place the cursor at the beginning of the first cell of the row you want to replace and paste. This replaces the contents cell by cell without changing the table formatting or adding a new row.
    I would be interesting is seeing better code to do this, code which works with one-column tables and whether the cells are empty or not, for example.

    Ed Colaianni
    Karlsruhe, Germany

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

    Re: Range place row of text in table (Word2000)

    ED, thanks for the response and apologies for the delay. I am beseiged on several fronts, one of which is my inability to master the vagaries of Selection vs. Range with cells, columns and tables. My post <post#=565,520>post 565,520</post#> struggles (incompletely) to determine the type of selection. The attached DOC is a work-in-progress.The business of selecting cells (Word2000 at any rate) seems corrupted.

    (In the attached doc)
    If I drag the mouse outside and to the left of the table, from cell 7 to cell 19, I see that I have selected rows, and the TestlngTableSelectionDetails tells me that I have selected rows.
    If I drag the mouse inside the table, from cell 24 to cell 7, I see that I have selected cells, and the TestlngTableSelectionDetails tells me that I have selected cells.
    If I drag the mouse outside and from above the table, from cell 2 to cell 4, I see that I have selected cells, and the TestlngTableSelectionDetails tells me that I have selected columns.
    If I drag the mouse inside the table, from cell 34 to cell 2, I see that I have selected cells, and the TestlngTableSelectionDetails tells me that I have selected columns.

    It seems significant that Word2000 does not give a visual indication of selection of columns by extending the highlighting below the column the way it extends highlighting to the right of selected rows of a table.
    This newsgroup entry discusses the problem, but I'm still struggling to put it all into practice.


    > one-column tables and whether the cells are empty or not, for example
    The code that tests selection.range is meant to serve a set of my macros to identify (and remove) empty cells (Len=0) and cells containibg only white space.

Posting Permissions

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