Convert text to table using VBA

Dear Experts:

I got two questions:

QUESTION 1:

I would like to convert text to a table. The text to be converted ...
 ... starts from paragraph 2 and ends at the very end of the document.
 ... The separator is to be 'COMMAS'.

I am using below code. It works fine but I'd rather use a range object instead of the selection object.

QUESTION 2:
The macro "InsertOneRowAboveTheFirstOne" inserts a row above the first one. I would like to enter text in each cell of the newly created table row. How is this achieved using VBA?

Help is much appreciated. Thank you very much in advance.

I have attached a sample file for your convenience.

Regards, Andreas  SAMPLE-FILE-TABLE-MACRO.docx
Sub ConvertToTableSeparatedByCommas

Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdParagraph
Selection.EndKey Unit:=wdStory, Extend:=wdExtend

    WordBasic.TextToTable NumColumns:=3

End Sub


Sub InsertOneRowAboveTheFirstOne
Dim tbl as table
Dim rng as range
Dim rows as rows
Set tbl = ActiveDocument.Tables(1)
   Set rng = tbl.rows(1).Range
   Set rows = rng.rows
   rows.Select
   Selection.InsertRowsAbove 1
End Sub

Open in new window

Andreas HermleTeam leaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
Morning Andreas.
This is Q1
Sub ConvertToTableSeparatedByCommas_Range()
    Dim rng As Range
    
    Set rng = ActiveDocument.Range
    rng.Start = ActiveDocument.Paragraphs(2).Range.Start
    rng.ConvertToTable ","
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredCommented:
This shows a way of doing Q2
Sub InsertOneRowAboveTheFirstOne()
    Dim tbl As Table
    Dim rng As Range
    Dim rw As Row
    Dim cl As Cell
    
    Set tbl = ActiveDocument.Tables(1)
    Set rw = tbl.rows.Add(tbl.rows(1))
    For Each cl In rw.Cells
        cl.Range.Text = "Cell " & cl.RowIndex & ", " & cl.ColumnIndex
    Next cl
End Sub

Open in new window

Andreas HermleTeam leaderAuthor Commented:
Dear Graham,

thank you very much for your swift support. Q1 is fully answered. Thank you very much.

As for Q2, I guess,  I should have been more precise. The text in the newly created cells in row 1 should be different in all the three cells, i.e.

Cell 1 of first row: Page
Cell 2 of first row: Code
Cell 3 of first row: Submitted

Thank you very much in advance for your great help.

Regards, Andreas
GrahamSkanRetiredCommented:
Here is the code tweaked to do that.
Sub InsertOneRowAboveTheFirstOne()
    Dim tbl As Table
    Dim rng As Range
    Dim rw As Row
    
    Set tbl = ActiveDocument.Tables(1)
    Set rw = tbl.rows.Add(tbl.rows(1))
    rw.Cells(1).Range.Text = "Page"
    rw.Cells(2).Range.Text = "Code"
    rw.Cells(3).Range.Text = "Submitted"
End Sub

Open in new window

Andreas HermleTeam leaderAuthor Commented:
Hi Graham,

great, that's it. Thank you very much for your great support. Regards, Andreas
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.