WayneBurrows said:
That is brilliant thanks.
If I use the macro to insert the table where the selected text is with
a command like
ActiveDocument.AttachedTemplate.AutoTextEntries("TableOne").Insert
Where:=Selection.Range
How do I then set the oTable variable to this inserted table so that I
can then insert the text.
I take it that you want to replace the selected text (the comma delimited
items) with the autotext table?
In that case, the table is the first table in the selection.
Dim oRng As Range
Dim oEnd As Range
Dim oPara1, oPara2 As Variant
Dim oTable As Table
'Locate the end of the document
Set oEnd = ActiveDocument.Range
oEnd.Start = oEnd.End
'Select the two rows of comma delimited text
Set oRng = Selection.Range
'Check that the data is selected
If Len(oRng) = 0 Then
MsgBox "Select the data first!", vbCritical, "Error"
Exit Sub
End If
'Assign each of the two rows to a variable
oPara1 = Split(oRng.Paragraphs(1).Range.Text, ",")
oPara2 = Split(oRng.Paragraphs(2).Range.Text, ",")
'Add the autotext table in place of the data
ActiveDocument.AttachedTemplate.AutoTextEntries("TableOne") _
.Insert Where:=oRng
Set oTable = oRng.Tables(1)
'Write the entries from the first row to the appropriate cells
For i = 0 To UBound(oPara1)
oPara1(i) = Replace(oPara1(i), Chr(13), "")
oTable.Cell(1 + i, 2).Range.Text = Trim(oPara1(i))
Next i
'Write the entries from the second row to the appropriate cells
For j = 0 To UBound(oPara2)
oPara2(j) = Replace(oPara2(j), Chr(13), "")
oTable.Cell(5 + j, 3).Range.Text = Trim(oPara2(j))
Next j
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>