{"id":53763,"date":"2009-05-13T23:22:00","date_gmt":"2009-05-13T23:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/05\/13\/hey-scripting-guy-how-can-i-convert-to-windows-powershell-a-vbscript-script-that-creates-a-table-in-office-word\/"},"modified":"2009-05-13T23:22:00","modified_gmt":"2009-05-13T23:22:00","slug":"hey-scripting-guy-how-can-i-convert-to-windows-powershell-a-vbscript-script-that-creates-a-table-in-office-word","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-convert-to-windows-powershell-a-vbscript-script-that-creates-a-table-in-office-word\/","title":{"rendered":"Hey, Scripting Guy! How Can I Convert to Windows PowerShell a VBScript Script That Creates a Table in Office Word?"},"content":{"rendered":"<h2><img decoding=\"async\" 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\" \/> <\/h2>\n<p>Hey, Scripting Guy! I was looking through the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\" target=\"_blank\">Office Space Archive<\/a> and came across a nice article that talks about <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/jul05\/tips0726.mspx\" target=\"_blank\">how to create a table in Office Word<\/a>. The article is interesting, but I am more interested in how to do this by using Windows PowerShell. Will I be able to translate this script to Windows PowerShell? Or do you have one already written that uses Windows PowerShell to do this?<\/p>\n<p>&#8211; DC<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hi DC,<\/p>\n<p>You know, the great thing about Tech\u00b7Ed is the opportunity to meet and talk with people from around the world. We have enjoyed renewing our friendships with people we met years ago and making new friends. It is refreshing to be able to talk to so many people who share common interests. The good thing about having Tech\u00b7Ed in Los Angeles is there are world-class restaurants\u2014and don\u2019t forget walking on perpetual sunshine. Because we are about to go into brain overload from all the excellent sessions, we will go ahead and answer your question. We do not have a Windows PowerShell script that creates tables in an Office Word document. So you will have to bear with us for a minute while we translate the one that you referred to on the Office Space page. Speaking of bear with us, here is a <a href=\"http:\/\/www.sandiegozoo.org\/animalbytes\/t-sun_bear.html\" target=\"_blank\">Bornean sun bear<\/a> Ed saw at the <a href=\"http:\/\/en.wikipedia.org\/wiki\/San_Diego_Zoo\" target=\"_blank\">San Diego Zoo<\/a> who may have attended one Tech\u00b7Ed session too many.<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of a Bornean sun bear\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0513\/hsg-05-13-09-01.jpg\" width=\"500\" height=\"375\" \/> <\/p>\n<p>&nbsp;<\/p>\n<table id=\"EMD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">This week we are looking at how to migrate VBScript to Windows PowerShell. You should definitely check out the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/convert\/default.mspx\" target=\"_blank\">VBScript-to-Windows PowerShell Conversion Guide<\/a>. This is included as Appendix C in the Microsoft Press book, <a href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth10329.aspx\" target=\"_blank\">Microsoft Windows PowerShell Step by Step<\/a>. It is also in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/pschm.mspx\" target=\"_blank\">Windows PowerShell Graphical Help File<\/a>. Clearly, we are proud of that thing. You may also want to check out our <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" target=\"_blank\">Windows PowerShell Scripting Hub<\/a> where you will find links to the Windows PowerShell Owner&#8217;s Manual (very popular!) and other resources that will help you to convert VBScript to Windows PowerShell. One additional book that would be useful is the Microsoft Press book, <a href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth9541.aspx\" target=\"_blank\">Windows PowerShell Scripting Guide<\/a>. This book is useful if you are working with WMI, or if you are trying to go beyond simple line-by-line translations of one script to another.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>The <b>GetServiceCreateWordTable.vbs<\/b> from the Office Space <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/jul05\/tips0726.mspx\" target=\"_blank\">Creating Tables in Microsoft Word<\/a> article is shown here for ease of comparison:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">Const NUMBER_OF_ROWS = 1<br \/>Const NUMBER_OF_COLUMNS = 3<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)<br \/>objWord.Visible = True<br \/>Set objDoc = objWord.Documents.Add()<\/p>\n<p>Set objRange = objDoc.Range()<br \/>objDoc.Tables.Add objRange, NUMBER_OF_ROWS, NUMBER_OF_COLUMNS<br \/>Set objTable = objDoc.Tables(1)<\/p>\n<p>objTable.Cell(1, 1).Range.Text = &#8220;Service Name&#8221;<br \/>objTable.Cell(1, 2).Range.Text = &#8220;Display Name&#8221;<br \/>objTable.Cell(1, 3).Range.Text = &#8220;Service State&#8221;<\/p>\n<p>x = 2<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = _<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<br \/>Set colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_Service&#8221;)<\/p>\n<p>For Each objItem in colItems<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Rows.Add()<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Cell(x, 1).Range.Text = objItem.Name<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Cell(x, 2).Range.Text = objItem.DisplayName<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Cell(x, 3).Range.Text = objItem.State<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>x = x + 1<br \/>Next<\/p>\n<p>objTable.AutoFormat(9)<\/font><\/p>\n<p>The <b>GetServiceCreateWordTable.ps1<\/b> Windows PowerShell version is an exact translation of the <b>GetServiceCreateWordTable.vbs<\/b> script. The first thing that you have to do is to create two variables. The first variable is used to determine how many rows will be initially created when the table is created. The second variable determines the number of columns. In the <b>GetServiceCreateWordTable.ps1<\/b> script, the two variables that were created are constants. In Windows PowerShell if you want to create a constant, you use the <b>New-Variable<\/b> cmdlet as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:\\&gt; New-Variable -Name Number_Of_Rows -Value 1 -Option constant<\/font><\/span><\/p>\n<p>If you try to change the value of the <b>$Number_Of_Rows<\/b> constant, you will receive an error message as shown here:<\/p>\n<pre class=\"codeSample\"><p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:\\&gt; $Number_Of_Rows = 5<br \/>Cannot overwrite variable Number_Of_Rows because it is read-only or constant.<br \/>At line:1 char:16<br \/>+ $Number_Of_Rows<span>&nbsp; <\/span>&lt;&lt;&lt;&lt; = 5<\/font><\/span><\/p>\n\n\n\n<\/pre>\n<p>In our &#8220;exact translation,&#8221; this is the only thing we did not exactly translate. There is no reason to make the variables constant because we are not changing their values. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$Number_Of_Rows = 1<br \/>$Number_Of_Columns = 3<\/font><\/span><\/p>\n<p>Every script that works with Word Automation must create an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb244569.aspx\" target=\"_blank\">Word.Application object<\/a>. In Windows PowerShell, you use the <b>New-Object<\/b> cmdlet specify the <b>comobject<\/b> parameter and supply the name of the Program ID. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objWord = New-Object -comobject word.application<\/font><\/span><\/p>\n<p>After the <b>Word.Application<\/b> object is created, the <b>GetServiceCreateWordTable.vbs<\/b> script sets the visible property to <b>$true<\/b>, adds a document, and creates a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb245537.aspx\" target=\"_blank\">new range object<\/a>. For more information about this, look at <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may09\/hey0511.mspx\" target=\"_blank\">Monday\u2019s Hey, Scripting Guy article<\/a>. This section of code is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objWord.Visible = $true<br \/>$objDoc = $objWord.Documents.Add()<br \/>$objRange = $objDoc.Range()<\/font><\/span><\/p>\n<p>The next step is to add a table. To do this, you use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb213375.aspx\" target=\"_blank\">Add method<\/a> from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb259585.aspx\" target=\"_blank\">Tables collection<\/a>. The <b>Add<\/b> method is shown in <b>Table 1<\/b>. The <b>Add<\/b> method has three required parameters: <b>Range<\/b>, <b>NumRows<\/b>, and <b>NumColumns<\/b>. The <b>GetServiceCreateWordTable.vbs<\/b> script supplies these three parameters, but does not use any of the optional parameters. Each value for the parameters is held within variables. This makes reading the method call and changing the method call easy. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objDoc.Tables.Add($objRange,$Number_Of_Rows,$Number_Of_Columns)<\/font><\/span><\/p>\n<table id=\"ELH\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead>\n<tr>\n<td class=\"tableHeader\" colSpan=\"4\">Table 1 The Add method<\/td>\n<\/tr>\n<tr class=\"stdHeader\" vAlign=\"top\">\n<td id=\"colEOH\">Name<\/td>\n<td id=\"colESH\">Required\/Optional<\/td>\n<td id=\"colEWH\">Data Type<\/td>\n<td id=\"colE1H\">Description<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">Range<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Required<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Range object<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The range where you want the table to appear. The table replaces the range, if the range is not collapsed.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">NumRows<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Required<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Long<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The number of rows that you want to include in the table.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">NumColumns<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Required<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Long<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The number of columns that you want to include in the table.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">DefaultTableBehavior<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Sets a value that specifies whether Microsoft Word automatically resizes cells in tables to fit the cells\u2019 contents (AutoFit). Can be either of the following constants: <b>wdWord8TableBehavior<\/b> (AutoFit disabled) or <b>wdWord9TableBehavior<\/b> (AutoFit enabled). The default constant is <b>wdWord8TableBehavior<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">AutoFitBehavior<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Sets the AutoFit rules for how Word sizes tables. Can be one of the <b>WdAutoFitBehavior<\/b> constants.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>The <b>GetServiceCreateWordTable.vbs<\/b> script next retrieves the first table from the <b>Tables<\/b> collection. The VBScript line of code is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Set objTable = objDoc.Tables(1)<\/font><\/span><\/p>\n<p>In Windows PowerShell this is the code to retrieve the first table from the <b>Tables<\/b> collection:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objTable = $objDoc.Tables.item(1)<\/font><\/span><\/p>\n<p>VBScript requires you to use the <b>Set<\/b> statement when a method returns an object. The confusing section of the script is the VBScript code that calls the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb215662.aspx\" target=\"_blank\">Item method<\/a> from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb212166.aspx\" target=\"_blank\">Tables collection<\/a>. You might not know this because the <b>Item<\/b> method is the default method from the <b>Tables<\/b> collection, and in VBScript it is not required to be mentioned. The .NET Framework does not allow you to omit method names, and this is the reason why you use it in the Windows PowerShell code. It is a bad practice to leave off method names in VBScript because most readers will not realize that it is missing, and they will probably assume that they are using a <b>Tables<\/b> method or even querying a <b>Tables<\/b> property from a <b>document<\/b> object. When you run the <b>GetServiceCreateWordTable.ps1<\/b> script, it probably is not important if you know exactly what the script is doing. But when you have to change or troubleshoot the script, you must know exactly what the script is doing.<\/p>\n<p>The table column headings are produced by using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb217213.aspx\" target=\"_blank\">Cell method<\/a> from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb259571.aspx\" target=\"_blank\">Table object<\/a>. The <b>Cell<\/b> method returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb258624.aspx\" target=\"_blank\">Cell object<\/a>. The <b>range<\/b> property from the <b>cell<\/b> object returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb213003.aspx\" target=\"_blank\">Range object<\/a>. The range object has a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb179332.aspx\" target=\"_blank\">Text property<\/a> that enables you to set or retrieve text from the <b>Range<\/b> object. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$objTable.Cell(1,1).Range.Text = &#8220;Service Name&#8221;<br \/>$objTable.Cell(1,2).Range.Text = &#8220;Display Name&#8221;<br \/>$objTable.Cell(1,3).Range.Text = &#8220;Service State&#8221;<\/font><\/span><\/p>\n<p>The <b>GetServiceCreateWordTable.vbs<\/b> script assigns the number 2 to the <b>X<\/b> variable. This refers to the second row in the table and is the starting point for writing the WMI data to the newly created Office Word table. Then it performs the WMI query. This section of the script is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">x = 2<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = _<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<br \/>Set colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_Service&#8221;)<\/font><\/span><\/p>\n<p>In the <b>GetServiceCreateWordTable.ps1<\/b> script, this section of the script is simplified because of the use of the <b>Get-WmiObject<\/b> cmdlet. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$x = 2<br \/>$strComputer = &#8220;.&#8221;<\/p>\n<p>Get-WmiObject -query &#8220;Select * from Win32_Service&#8221; -computername $strComputer<\/font><\/span><\/p>\n<p>Because we are doing a script translation, we decided to use the legacy WQL syntax with the <b>Get-WmiObject<\/b>. This compatability mode of using Windows PowerShell is discussed in <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar09\/hey0304.mspx\" target=\"_blank\">How do I migrate my VBScript WMI queries to Windows Powershell<\/a>. If we were writing this script from scratch, we would query the <b>Win32_Service<\/b> class directly:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-WmiObject \u2013class Win32_Service \u2013computername $strComputer<\/font><\/span><\/p>\n<p>The <b>GetServiceCreateWordTable.vbs<\/b> script stores the WMI service information in the <b>colItems<\/b> variable and uses the <b>For\u2026Each\u2026Next<\/b> statement to walk through the collection. Inside the loop it adds a row to the table, retrieves the WMI property values, and writes the information to the <b>Text<\/b> property of the <b>Range<\/b> object in the current cell range. When it is finished writing to the current cells, it increments the value of the <b>x<\/b> variable and continues to the next <b>objItem<\/b> in the collection. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">For Each objItem in colItems<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Rows.Add()<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Cell(x, 1).Range.Text = objItem.Name<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Cell(x, 2).Range.Text = objItem.DisplayName<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>objTable.Cell(x, 3).Range.Text = objItem.State<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>x = x + 1<br \/>Next<\/font><\/span><\/p>\n<p>The <b>GetServiceCreateWordTable.ps1<\/b> script does not use the <b>ForEach<\/b> statement to walk through a collection of WMI objects. Instead, it uses the Windows PowerShell pipeline and streams the WMI data as it is received from WMI across the pipeline to the <b>ForEach-Object<\/b> cmdlet. Inside the script block of the <b>ForEach-Object<\/b> cmdlet the script adds a new row, writes to the <b>text<\/b> property of the <b>Range<\/b> object of the <b>Cell<\/b> object, and uses the <b>$_<\/b> automatic variable to refer to the current WMI object that is located on the pipeline. By not storing and forwarding the WMI data, the script is more efficient. We increment the value of the <b>$x<\/b> variable by using the <b>++<\/b> operator. This section of code is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Foreach-Object {<br \/><span>&nbsp;<\/span>$objTable.Rows.Add()<br \/><span>&nbsp;<\/span>$objTable.Cell($x,1).Range.Text = $_.name<br \/><span>&nbsp;<\/span>$objTable.Cell($x,2).Range.Text=$_.DisplayName<br \/><span>&nbsp;<\/span>$objTable.Cell($x,3).Range.Text=$_.State<br \/><span>&nbsp;<\/span>$x++<br \/>} #end Foreach-Object<\/font><\/span><\/p>\n<p>The last thing that must be done is to format that Word table. The <b>GetServiceCreateWordTable.ps1<\/b> script provides an enumeration value directly to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb217211.aspx\" target=\"_blank\">AutoFormat method<\/a> from the <b>Table<\/b> object. The <b>Format<\/b> parameter will accept either a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb238267.aspx\" target=\"_blank\">WdTableFormat<\/a> enumeration value or a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb213976.aspx\" target=\"_blank\">WdTableFormatApply<\/a> enumeration value. The number 9 corresponds to the <b>wdTableFormatColorful2<\/b> format. <i>Colorful<\/i> is a good name for table format number 9. You can see it for yourself here:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Image of a table created in Office Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/may\/hey0513\/hsg-05-13-09-02.jpg\" width=\"500\" height=\"358\" \/> <\/p>\n<p>&nbsp;<\/p>\n<p>The parameters of the <b>AutoFormat<\/b> method from the <b>Table<\/b> object are discussed in <b>Table 2<\/b>.<\/p>\n<table id=\"EQIAC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead>\n<tr>\n<td class=\"tableHeader\" colSpan=\"4\">Table 2 The parameters of the AutoFormat method from the Table object<\/td>\n<\/tr>\n<tr class=\"stdHeader\" vAlign=\"top\">\n<td id=\"colETIAC\">Name<\/td>\n<td id=\"colEXIAC\">Required\/Optional<\/td>\n<td id=\"colE2IAC\">Data Type<\/td>\n<td id=\"colE6IAC\">Description<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">Format<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">The format to apply. This parameter can be a <b>WdTableFormat<\/b> constant, a <b>WdTableFormatApply<\/b> constant, or a <b>TableStyle<\/b> object.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyBorder<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">True to apply the border properties of the specified format. The default value is <b>True<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyShading<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to apply the shading properties of the specified format. The default value is <b>True<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyFont<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to apply the font properties of the specified format. The default value is <b>True<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyColor<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to apply the color properties of the specified format. The default value is <b>True<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyHeadingRows<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to apply the heading-row properties of the specified format. The default value is <b>True<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyLastRow<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to apply the last-row properties of the specified format. The default value is <b>False<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyFirstColumn<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to apply the first-column properties of the specified format. The default value is <b>True<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">ApplyLastColumn<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to apply the last-column properties of the specified format. The default value is <b>False<\/b>.<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\">AutoFit<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Optional<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\">Variant<\/p>\n<\/td>\n<td>\n<p class=\"lastInCell\"><b>True<\/b> to decrease the width of the table columns as much as possible without changing the way text wraps in the cells. The default value is <b>True<\/b>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>The <b>GetServiceCreateWordTable.ps1<\/b> script is shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font face=\"Lucida Sans Typewriter\">$Number_Of_Rows = 1<br \/>$Number_Of_Columns = 3<\/p>\n<p>$objWord = New-Object -comobject word.application<br \/>$objWord.Visible = $true<br \/>$objDoc = $objWord.Documents.Add()<br \/>$objRange = $objDoc.Range()<br \/>$objDoc.Tables.Add($objRange,$Number_Of_Rows,$Number_Of_Columns) <br \/>$objTable = $objDoc.Tables.item(1)<\/p>\n<p>$objTable.Cell(1,1).Range.Text = &#8220;Service Name&#8221;<br \/>$objTable.Cell(1,2).Range.Text = &#8220;Display Name&#8221;<br \/>$objTable.Cell(1,3).Range.Text = &#8220;Service State&#8221;<\/p>\n<p>$x = 2<br \/>$strComputer = &#8220;.&#8221;<\/p>\n<p>Get-WmiObject -query &#8220;Select * from Win32_Service&#8221; -computername $strComputer |<br \/>Foreach-Object {<br \/><span>&nbsp;<\/span>$objTable.Rows.Add()<br \/><span>&nbsp;<\/span>$objTable.Cell($x,1).Range.Text = $_.name<br \/><span>&nbsp;<\/span>$objTable.Cell($x,2).Range.Text=$_.DisplayName<br \/><span>&nbsp;<\/span>$objTable.Cell($x,3).Range.Text=$_.State<br \/><span>&nbsp;<\/span>$x++<br \/>} #end Foreach-Object<\/p>\n<p>$objTable.AutoFormat(9)<\/font><\/p>\n<p>DC, that is about all we have time for today. Hope you have enjoyed all of the words that Ed wrote and Craig produced, edited, published, and quality checked. (Ed was over his word count by 33 percent!) Take care, and join us tomorrow.<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I was looking through the Office Space Archive and came across a nice article that talks about how to create a table in Office Word. The article is interesting, but I am more interested in how to do this by using Windows PowerShell. Will I be able to translate this script 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,4,5,155,45],"class_list":["post-53763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I was looking through the Office Space Archive and came across a nice article that talks about how to create a table in Office Word. The article is interesting, but I am more interested in how to do this by using Windows PowerShell. Will I be able to translate this script to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53763","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=53763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53763\/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=53763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}