Solved

Splitting a table into columns using VBA in Word

Posted on 2006-07-16
7
382 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
Comment Utility
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
Comment Utility
Dear Graham Skan
how can I add my table as an object variable? Thank you.
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Sorry. Missed the Set.

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

Author Comment

by:pelbooks
Comment Utility
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 125 total points
Comment Utility
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
Comment Utility
great thanks a lot :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now