{"id":67853,"date":"2006-03-01T09:49:00","date_gmt":"2006-03-01T09:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/01\/how-can-i-right-align-a-single-column-in-a-word-table\/"},"modified":"2006-03-01T09:49:00","modified_gmt":"2006-03-01T09:49:00","slug":"how-can-i-right-align-a-single-column-in-a-word-table","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-right-align-a-single-column-in-a-word-table\/","title":{"rendered":"How Can I Right-Align a Single Column in a Word Table?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I right-align a single column in a Word table?<BR><BR>&#8212; RC<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RC. You know, one of the phrases no one ever wants to hear is \u201cThis isn\u2019t as bad as it looks.\u201d After all, that\u2019s what your business partner says when you discover he\u2019s been using the company payroll to buy lottery tickets. \u201cThis isn\u2019t as bad as it looks,\u201d is the phrase a doctor uses when you catch a glimpse of the hypodermic needle he\u2019s preparing to stick you with. (In that case, he\u2019s right: it\u2019s usually <I>worse<\/I> than it looks.) And yes, \u201cThis isn\u2019t as bad as it looks\u201d is the phrase that the Scripting Guys use before showing you a script that right-aligns a column in a Microsoft Word table.<\/P>\n<P>The difference, of course, is that we Scripting Guys sincerely mean it. As you\u2019re 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\u2019s 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 &#8211; while not especially hard &#8211; 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\u2019re interested in is very easy.<\/P>\n<P>In other words, this isn\u2019t as bad as it looks:<\/P><PRE class=\"codeSample\">Const wdAlignParagraphRight = 2 \nConst NUMBER_OF_ROWS = 1\nConst NUMBER_OF_COLUMNS = 3<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()<\/p>\n<p>Set objRange = objDoc.Range()\nobjDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS\nSet objTable = objDoc.Tables(1)<\/p>\n<p>x=2<\/p>\n<p>objTable.Cell(1, 1).Range.Text = &#8220;Process Name&#8221;\nobjTable.Cell(1, 2).Range.text = &#8220;Process ID&#8221;\nobjTable.Cell(1, 3).Range.text = &#8220;Handle Count&#8221;<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_Process&#8221;)\nFor Each objItem in colItems\n    If x &gt; 1 Then\n        objTable.Rows.Add()\n    End If\n    objTable.Cell(x, 1).Range.Text = objItem.Name\n    objTable.Cell(x, 2).Range.text = objItem.ProcessID\n    objTable.Cell(x, 3).Range.text = objItem.HandleCount\n    x = x + 1\nNext<\/p>\n<p>objDoc.Tables(1).Columns(3).Select\nSet objSelection = objWord.Selection<\/p>\n<p>objSelection.ParagraphFormat.Alignment = wdAlignParagraphRight\n<\/PRE>\n<P>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\u2019s 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.<\/P>\n<TABLE id=\"EAD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. 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 <I>Hey, Scripting Guy! <\/I>column. However, we do have an <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/jul05\/tips0726.mspx\"><B>Office Space column<\/B><\/A> that explains the process in considerable detail.<\/P>\n<P>Hey, the Scripting Guys would never leave you hanging like that.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what happens after we <I>do<\/I> 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\u2019s how we select column 3 in the table:<\/P><PRE class=\"codeSample\">objDoc.Tables(1).Columns(3).Select\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Add()\n<\/PRE>\n<P><B>Tables(1)<\/B> simply means we want to work with the first table in the <B>Tables<\/B> collection, a collection consisting of all the tables in our document. Likewise, <B>Columns(3)<\/B> means that we specifically want to work with the third column in table 1. And <B>Select<\/B> means, well, it means that we want to select column 3 in table 1. Make sense? We thought so.<\/P>\n<P>After selecting the text we need to create an instance of the <B>Selection<\/B> object in order to manipulate that text. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">Set objSelection = objWord.Selection\n<\/PRE>\n<P>That means that all we have to do now is right-align all the paragraphs in the selected text:<\/P><PRE class=\"codeSample\">objSelection.ParagraphFormat.Alignment = wdAlignParagraphRight\n<\/PRE>\n<P>In case you\u2019re 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:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>wdAlignParagraphCenter (value 1) to center the text.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>wdAlignParagraphLeft (value 0) to left-align the text.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>wdAlignParagraphJustify (value 3) to justify the text.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>And what happens when we run this script? This happens:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/rightalign.jpg\" width=\"391\" height=\"418\"> \n<P><BR>See, we told you: it really <I>wasn\u2019t<\/I> as bad as it looked, was it?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I right-align a single column in a Word table?&#8212; RC Hey, RC. You know, one of the phrases no one ever wants to hear is \u201cThis isn\u2019t as bad as it looks.\u201d After all, that\u2019s what your business partner says when you discover he\u2019s been using the company payroll to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5],"class_list":["post-67853","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I right-align a single column in a Word table?&#8212; RC Hey, RC. You know, one of the phrases no one ever wants to hear is \u201cThis isn\u2019t as bad as it looks.\u201d After all, that\u2019s what your business partner says when you discover he\u2019s been using the company payroll to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67853"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67853\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}