Solved

Splitting a table into columns using VBA in Word

Posted on 2006-07-16
7
383 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 125 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

948 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

23 Experts available now in Live!

Get 1:1 Help Now