Log in

View Full Version : [SOLVED:] Copy Table Cell - Insert Text Before Each Table



dj44
03-03-2017, 03:42 PM
good evening folks:)


I am trying to copy the cell id before each table.

The cell - first one I put some text in it like an ID.

Now I am trying to paste that before each table - so I know where each table should be, when it gets moved or lost




Sub InsertAfterEachTable()


Dim oTable As Table
Dim i As Long
Dim SourceCell As cell
Dim DestCell As cell, Temp As cell



With ActiveDocument
For i = .Tables.Count To 1 Step -1
Set oTable = .Tables(i)
Set SourceCell = ThisDocument.Tables(i).cell(1, 1)
SourceCell.Range.Copy
SourceCell.Range.Paste

' something not right here

Next i
End With
lbl_Exit:
Exit Sub
End Sub


No Cell Id was pasted, and Im not sure why.

gmayor
03-03-2017, 11:30 PM
Your code simply pastes the copied text back to the same cell. To paste it after the table


Sub InsertAfterEachTable()
Dim oTable As Table
Dim i As Long
Dim oCell As Range
Dim oRng As Range
With ActiveDocument
For i = 1 To .Tables.Count
Set oTable = .Tables(i)
Set oRng = oTable.Range
oRng.Collapse 0
oRng.InsertParagraphAfter
oRng.End = oRng.End + 1
oRng.Collapse 1
Set oCell = ActiveDocument.Tables(i).Cell(1, 1).Range
oCell.End = oCell.End - 1
oRng.FormattedText = oCell.FormattedText
Next i
End With
lbl_Exit:
Exit Sub
End Sub

dj44
03-04-2017, 08:33 AM
Thank you Graham,

that worked a treat!

My tables are always stressing me out - so i need to some times paste them to another document and deal with them, then paste them back
but i always forget where they came from.:doh:

I used the insert after, but that only pasted the text to the end of the document
So I couldnt work out how to make the cells paste it after each one

But this is good

Have a good weekend folks all
:)