Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Splitting a table into columns using VBA in Word

Posted on 2006-07-16
7
Medium Priority
?
391 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:pelbooks
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17122458
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
0
 
LVL 2

Author Comment

by:pelbooks
ID: 17122730
Dear Graham Skan
how can I add my table as an object variable? Thank you.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17123064
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)
0
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17123070
Sorry. Missed the Set.

dim tbl as Table
Set tbl = doc.tables(1)
0
 
LVL 2

Author Comment

by:pelbooks
ID: 17128494
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.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 17128937
You could get the table width by adding the column widths, then average that space out between the columns:

Sub AddColumn()
    Dim tbl As Table
    Dim tblwidth As Single
    Dim col As Column
   
    Set tbl = ActiveDocument.Tables(1)
   
    For Each col In tbl.Columns
        tblwidth = tblwidth + col.width
    Next col
   
    tbl.Columns.Add

    For Each col In tbl.Columns
        col.width = tblwidth / tbl.Columns.Count
    Next col
End Sub
0
 
LVL 2

Author Comment

by:pelbooks
ID: 17129034
great thanks a lot :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question