Results 1 to 2 of 2
2006-02-15, 22:10 #1
- Join Date
- Mar 2003
- Elkins Park, Pennsylvania, USA
- Thanked 0 Times in 0 Posts
Formatting Tables via VBA (Word XP)
I'm going insane trying to figure this out.
I have documents that contain tables that I must format. I need to give them a uniform appearance: alignment, width, borders, etc. The tables have been created by pasting together other tables from other sources. (Sometimes they combine into a single table, sometimes they remain two discreet tables... but I'll bang my head against that issue in due time.) What on earth must I do so that I don't have a table with ragged right and left borders? I want nothing more than to have the left and right edges aligned, but I can't even do it manually. In the attached sample document, the right edge remains visibly ragged whether I manipulate the settings manually or through code. And some combined tables even seem to keep a ragged left margin.
I have written code (contained in the code module included in the attached document), but it doesn't always work predictably. For example, if there is non-table-text (e.g. a caption) that is not in a table row, I want to convert the text into a table via VBA (because I don't want the table split by text.) But if I don't keep the new table separated as its own table before combining it with the surrounding tables, then it will almost never align with the tables above and below it. In fact, if I convert to a table all of the text in between two existing tables, it seems to virtually ignore the FormatTableWidth sub I wrote. To see what I mean, you can use the attached sample document. It includes the sub that converts the caption, and the sub that should set the width, alignment, etc of the selected table. (I put the two commands on a toolbar for testing.)
I simply cannot get predictable behavior, and I don't know if it's the tables or the code itself. But I DO know I'm going crazy trying to solve this!<font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike
2006-02-15, 22:21 #2
- Join Date
- Mar 2002
- Thanked 29 Times in 29 Posts
Re: Formatting Tables via VBA (Word XP)
It's probably best to convert all tables to tab-delimited text, then use code to create a table from the tab-delimited text. It's probably not too hard to determine the number of columns per row, and to calculate column widths for an even distribution. But it'll be very difficult to determine criteria for "optimal" column widths.