Link to home
Start Free TrialLog in
Avatar of pelbooks
pelbooks

asked on

Splitting a table into columns using VBA in Word

Hello everyone.
I have this table in a doc file and I get some data in a recordset from an access file using vba. I want to display these data in the table but I want to add columns inside this table first and then put the data in the cells. Every time I want a different number of columns (depending on the data I got). How can I add columns and rows in a table? Thank you very much.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

If you already have your table in an object variable, then you can use these Subs:

Sub AddRow(tbl As Table)
Dim rowObject As Row
    Set rowObject = tbl.Rows.Add
End Sub

Sub AddColumn(tbl As Table)
    Dim colObject As Column
    Set colObject = tbl.Columns.Add
End Sub
Avatar of pelbooks
pelbooks

ASKER

Dear Graham Skan
how can I add my table as an object variable? Thank you.
You will have to find it. Tables are indexed by their order in the document.

If it is the first, then you could do this

dim tbl as Table
tbl = doc.tables(1)
Sorry. Missed the Set.

dim tbl as Table
Set tbl = doc.tables(1)
Hello again!
This works indeed but the columns go outside the predefined maximum width! Can't I create the columns without going over the maximum width? Thank you.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
great thanks a lot :)