Automatic Formatting of Columns in Word Tables

E

Evan Stone

Hi,

I was wondering if there a convenient way to select a table in word, and
then tell it to right-justify all columns that contain numeric data (a la
Excel). Is this a feature that's available in Word (2000+) via automation?

I'm assuming that it isn't, but I figured I'd ask before I put a lot of work
into doing it manually if there was a way to do it automatically.

Thanks!

evan k. stone | software engineer
 
E

Evan Stone

Well... I decided to go the "inconvenient" route, and it seems to work okay.

I just basically rip through the columns in the table, select them,
determine what alignment they require (based on a field value in a dataset),
and then set each column accordingly.

There's usually just a handful of columns, so it's not a real lengthy
process:

// -------- begin code snippet (C#) --------
private void AlignWordTableColumns(
Word.Table wordTable,
DataSet ds,
MyDataField [] fieldList)
{

int colCount = fieldList.Length;

for (int i=1;i<=(colCount);i++)
{
// select the next column
wordTable.Columns.Item(i).Select();

// get the alignment setting for this column...
wordApplication.Selection.ParagraphFormat.Alignment =
GetDetailTableFieldAlignment(ds, fieldList[i-1].name);

// ye olde alignment funkfix. some things never change...
wordApplication.Selection.ParagraphFormat.RightIndent
= wordApplication.InchesToPoints(0.01F);

// Note: "VBA.False" is just a constant defined in a class
// elsewhere that I use to assist in C# automation
// coding. It's not a native C#/.NET/COM Interop thing.
wordApplication.Selection.ParagraphFormat.SpaceAfterAuto
= (int)VBA.False;
wordApplication.Selection.ParagraphFormat.SpaceBeforeAuto
= (int)VBA.False;

}

}
// -------- end code snippet (C#) --------
 
C

Cindy M -WordMVP-

Hi Evan,
Well... I decided to go the "inconvenient" route, and it seems to work okay.

I just basically rip through the columns in the table, select them,
determine what alignment they require (based on a field value in a dataset),
and then set each column accordingly.
this really is the most efficient manner :)

Cindy Meister
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top