Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I right-align a single column in a Word table?

-- RC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RC. You know, one of the phrases no one ever wants to hear is “This isn’t as bad as it looks.” After all, that’s what your business partner says when you discover he’s been using the company payroll to buy lottery tickets. “This isn’t as bad as it looks,” is the phrase a doctor uses when you catch a glimpse of the hypodermic needle he’s preparing to stick you with. (In that case, he’s right: it’s usually worse than it looks.) And yes, “This isn’t as bad as it looks” is the phrase that the Scripting Guys use before showing you a script that right-aligns a column in a Microsoft Word table.

The difference, of course, is that we Scripting Guys sincerely mean it. As you’re about to see, it takes only a few lines of code to right-align a table column in Word. The only problem is that it’s difficult to right-align a table column unless you actually have a table in your document. For better or worse, creating a table in Word - while not especially hard - does require a relatively large amount of code, especially if you want to create a table that resembles the sort of table a system administrator might create. So relax. The script will look big and it will look complicated, but the part you’re interested in is very easy.

In other words, this isn’t as bad as it looks:

Const wdAlignParagraphRight = 2 
Const NUMBER_OF_ROWS = 1
Const NUMBER_OF_COLUMNS = 3

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

Set objRange = objDoc.Range()
objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS
Set objTable = objDoc.Tables(1)

x=2

objTable.Cell(1, 1).Range.Text = "Process Name"
objTable.Cell(1, 2).Range.text = "Process ID"
objTable.Cell(1, 3).Range.text = "Handle Count"

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objItem in colItems
    If x > 1 Then
        objTable.Rows.Add()
    End If
    objTable.Cell(x, 1).Range.Text = objItem.Name
    objTable.Cell(x, 2).Range.text = objItem.ProcessID
    objTable.Cell(x, 3).Range.text = objItem.HandleCount
    x = x + 1
Next

objDoc.Tables(1).Columns(3).Select
Set objSelection = objWord.Selection

objSelection.ParagraphFormat.Alignment = wdAlignParagraphRight

What this script does is grab information about all the processes currently running on a computer; it then displays that process information in a Word table. That’s what almost all the preceding code is for: it creates a Word document, gets the process information, then creates a table and populates the rows and columns with process information. Once we get that far (in other words, once we have a table in our document) we need only three lines of code to right-align one of the columns.

Note. So are we going to explain all the code for creating a table in Microsoft Word? No, not today: that goes beyond what we can cover in a Hey, Scripting Guy! column. However, we do have an Office Space column that explains the process in considerable detail.

Hey, the Scripting Guys would never leave you hanging like that.

So what happens after we do have a table in our document? Well, the first step is to select one of the table columns. In our sample table, we display the process handle count in column 3; we did that because the handle count is a number and numbers are always good candidates for right-alignment. Here’s how we select column 3 in the table:

objDoc.Tables(1).Columns(3).Select

That should be pretty straightforward. The variable objDoc is an object reference to a Word document; we created that object reference way back at the beginning of the script:

Set objDoc = objWord.Documents.Add()

Tables(1) simply means we want to work with the first table in the Tables collection, a collection consisting of all the tables in our document. Likewise, Columns(3) means that we specifically want to work with the third column in table 1. And Select means, well, it means that we want to select column 3 in table 1. Make sense? We thought so.

After selecting the text we need to create an instance of the Selection object in order to manipulate that text. That’s what we do here:

Set objSelection = objWord.Selection

That means that all we have to do now is right-align all the paragraphs in the selected text:

objSelection.ParagraphFormat.Alignment = wdAlignParagraphRight

In case you’re wondering, wdAlignParagraphRight is a constant we defined at the beginning of the script (assigning it the value 2); we use this constant to tell Word that we want the selected text to be right-aligned. Needless to say, we could specify other alignment options, including:

wdAlignParagraphCenter (value 1) to center the text.

wdAlignParagraphLeft (value 0) to left-align the text.

wdAlignParagraphJustify (value 3) to justify the text.

And what happens when we run this script? This happens:

Hey, Scripting Guy!


See, we told you: it really wasn’t as bad as it looked, was it?