Solved

Splitting a table into columns using VBA in Word

Posted on 2006-07-16
7
385 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Word Formatting 3 63
How to search text in Word documents in Windows 10 3 67
MS Word document > cursor placement 5 35
Regarding Notepad++ 4 47
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…
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…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
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 …

860 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