
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. 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 = () Here’s how we select column 3 in the table: objDoc.Tables(1).Columns(3).Select 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.

So what happens after we do have a table in our document? Well, the first step is to select one of the table columns.

Hey, the Scripting Guys would never leave you hanging like that. However, we do have an Office Space column that explains the process in considerable detail.
#How to change the horizontal alignment in word 2016 code
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. 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. 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. 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. ObjTable.Cell(x, 3).Range.text = objItem.HandleCount ObjTable.Cell(x, 2).Range.text = objItem.ProcessID ObjTable.Cell(x, 1).Range.Text = objItem.Name Set colItems = objWMIService.ExecQuery(“Select * from Win32_Process”) Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) ObjTable.Cell(1, 3).Range.text = “Handle Count” ObjTable.Cell(1, 2).Range.text = “Process ID” ObjTable.Cell(1, 1).Range.Text = “Process Name” objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS Set objWord = CreateObject(“Word.Application”) In other words, this isn’t as bad as it looks: Const wdAlignParagraphRight = 2 The script will look big and it will look complicated, but the part you’re interested in is very easy. 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.

The only problem is that it’s difficult to right-align a table column unless you actually have a table in your document.

As you’re about to see, it takes only a few lines of code to right-align a table column in Word. The difference, of course, is that we Scripting Guys sincerely mean it. (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. “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. 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. Hey, Scripting Guy! How can I right-align a single column in a Word table?
