{"id":55483,"date":"2008-05-28T01:49:00","date_gmt":"2008-05-28T01:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/05\/28\/hey-scripting-guy-how-can-i-tile-workbook-windows-in-microsoft-excel\/"},"modified":"2008-05-28T01:49:00","modified_gmt":"2008-05-28T01:49:00","slug":"hey-scripting-guy-how-can-i-tile-workbook-windows-in-microsoft-excel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-tile-workbook-windows-in-microsoft-excel\/","title":{"rendered":"Hey, Scripting Guy! How Can I Tile Workbook Windows in Microsoft Excel?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I have a Windows PowerShell script that starts Excel and loads three comma-separated values files into separate workbooks. What I was wondering is whether there\u2019s a way to have PowerShell tile these workbooks, either vertically or horizontally. I poked through various properties and searched the Web for ideas, but I couldn\u2019t find anything. Any help would be greatly appreciated.<br \/>&#8212; RE<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, RE. In case you\u2019re also wondering why there was no <i>Hey, Scripting Guy!<\/i> column yesterday that wasn\u2019t because the Scripting Guy who writes this column is lazy. (He <i>is<\/i> lazy, but that isn\u2019t why there was no column yesterday.) Instead, yesterday was Memorial Day in the US, one of those rare national holidays that even Microsoft employees get to take off. If there\u2019s one thing you can say about the Scripting Guy who writes this column it\u2019s this: if he doesn\u2019t need to come into work and write this column, well, he\u2019s not going to come into work and write this column.<\/p>\n<table class=\"dataTable\" id=\"EBD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Well, OK, there <i>was<\/i> that one time that he came into work on January 2<sup>nd<\/sup>, which was technically a holiday \u2013 and a day off \u2013 because January 1<sup>st<\/sup> fell on a Sunday. But that wasn\u2019t because he was eager to come into work; that was because he\u2019s an idiot. (Especially when you consider the fact that he was here for 2 or 3 hours before it suddenly dawned him that the rest of the campus was completely deserted.)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>For those of you unfamiliar with the holiday, Memorial Day was originally called Decoration Day, and was first held as a way to honor the memory of soldiers who died during the Civil War. (We tried to verify this with Peter Costantini, the oldest living Scripting Guy, but we never got an answer. Instead, Peter insisted on telling us \u2013 over and over again \u2013 how he tried to enlist in the Union army and fight in the Civil War, but was rejected because he was too old.) After World War II the meaning of Decoration Day was extended to make it a day in which Americans would pay homage to <i>all<\/i> the men and women who gave their lives for their country, regardless of which war they fought in. <\/p>\n<p>For the first 100 years or so Memorial Day was always observed on May 30<sup>th<\/sup>; in 1971, however, the date was switched to the last Monday in May, the better to turn Memorial Day into a three-day weekend. A number of people were \u2013 and still are \u2013 upset with the decision to turn Memorial Day into a three-day weekend, believing that it detracts from the original meaning of the holiday: to honor the men and women who lost their lives in military service to the country. But hey, it\u2019s Memorial Day: a day for picnics, car racing, and great Memorial Day Blowout Sales! Who has time for anything else? <\/p>\n<p>And yes, we <i>are<\/i> being facetious.<\/p>\n<p>Not that it matters, mind you; after all, Memorial Day was yesterday, which means that today must be today. And if today <i>is<\/i> today, that means that we need to come up with a PowerShell script that can arrange workbook windows in Excel. You know, a script like this one:<\/p>\n<pre class=\"codeSample\">$xlArrangeStyleHorizontal = -4128\n\n$objExcel = New-Object -com Excel.Application\n$objExcel.Visible = $True\n\n$objWorkbook1 = $objExcel.Workbooks.Add()\n$objWorkbook2 = $objExcel.Workbooks.Add()\n$objWorkbook3 = $objExcel.Workbooks.Add()\n\n[void] $objExcel.Windows.Arrange($xlArrangeStyleHorizontal)\n<\/pre>\n<p>So how does this script work? We\u2019re glad you asked that question. The script starts off by assigning the value -4128 to a variable named $xlArrangeStyleHorizontal; we\u2019ll use this variable to tell the script that we want to tile the workbook window horizontally. Don\u2019t want to tile the windows horizontally? In that case, you can use any of the following values:<\/p>\n<table class=\"dataTable\" id=\"ELE\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Constant<\/b><\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\"><b>Value<\/b><\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">xlArrangeStyleCascade<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">7<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">xlArrangeStyleHorizontal<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">-4128<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">xlArrangeStyleTiled<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">1<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">xlArrangeStyleVertical<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">-4166<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<table class=\"dataTable\" id=\"EPF\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. Why in the world did the Excel team pick the values 1, 7, -4128, and -4166? To tell you the truth, we don\u2019t know; we learned a long time ago not to ask questions like that.<\/p>\n<p>After all, the answer usually just gives us a headache.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>After assigning a value to the variable $xlArrangeStyleHorizontal we next use the <b>New-Object<\/b> cmdlet to create an instance of the <b>Excel.Application<\/b> object:<\/p>\n<pre class=\"codeSample\">$objExcel = New-Object -com Excel.Application\n<\/pre>\n<p>By the way, don\u2019t forget to include the <b>\u2013com<\/b> parameter; that tells PowerShell that we want to create a COM object. If you leave this parameter out New-Object will try to instantiate an instance of a .NET Framework class named Excel.Application, which is going to be a bit of a problem seeing as how there <i>isn\u2019t<\/i> a .NET Framework class named Excel.Application.<\/p>\n<p>As soon as we have our new instance of Excel we set the <b>Visible<\/b> property to True ($True); that simply makes our instance of Excel visible onscreen. We then use the following lines of code to create three new workbooks, one named $objWorkbook1, one named $objWorkBook2, and the last one named $objWorkbook3:<\/p>\n<pre class=\"codeSample\">$objWorkbook1 = $objExcel.Workbooks.Add()\n$objWorkbook2 = $objExcel.Workbooks.Add()\n$objWorkbook3 = $objExcel.Workbooks.Add()\n<\/pre>\n<table class=\"dataTable\" id=\"ESG\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. Why in the world did the Scripting Guys pick the names $objWorkbook1, $objWorkbook2, and $objWorkbook3? That one we can answer: because we were too lazy to try and come up with names any more clever than that.<\/p>\n<p>Besides, the names <a href=\"http:\/\/www.threestooges.com\/bios\/\" target=\"_blank\"><b>Larry, Moe, and Curly<\/b><\/a> were already taken.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Believe it or not, at this point we\u2019re almost done. In fact, to tile our workbooks horizontally all we have to do is run the following line of code:<\/p>\n<pre class=\"codeSample\">[void] $objExcel.Windows.Arrange($xlArrangeStyleHorizontal)\n<\/pre>\n<p>All we\u2019re doing here is calling the <b>Arrange<\/b> method (a method that belongs to Excel\u2019s Windows collection object). As you can see, we pass the Arrange method a single parameter: $xlArrangeStyleHorizontal, the variable which \u2013 as we noted earlier \u2013 tells the script that we want to arrange the windows horizontally. That\u2019s all we have to do.<\/p>\n<p>Oh, good question: why <i>did<\/i> we preface this command with <b>[void]<\/b>? Well, when we call the Arrange method the value <i>True<\/i> gets echoed back to the PowerShell console. That\u2019s not a big deal but, then again, it\u2019s easy enough to prevent that message from appearing onscreen. And how do we suppress a message like this? You got it: we just preface the command with [void].<\/p>\n<p>By the way, our running instance of Excel is going to look like a lot like this, with the three workbooks tiled on top of each other:<\/p>\n<p><img decoding=\"async\" height=\"329\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/exceltiles.jpg\" width=\"450\" border=\"0\" \/> <\/p>\n<p>Pretty cool, huh?<\/p>\n<p>We should probably note that this works because we used a single instance of Excel to create all three of our workbooks. If you\u2019d prefer to have each workbook open in its own window (independent of all the other workbooks) then you need to use a separate instance of Excel for each workbook. In other words, you need to use a script like this:<\/p>\n<pre class=\"codeSample\">$objExcel = New-Object -com Excel.Application\n$objExcel.Visible = $True\n$objWorkbook1 = $objExcel.Workbooks.Add()\n\n$objExcel2 = New-Object -com Excel.Application\n$objExcel2.Visible = $True\n$objWorkbook2 = $objExcel2.Workbooks.Add()\n\n$objExcel3 = New-Object -com Excel.Application\n$objExcel3.Visible = $True\n$objWorkbook3 = $objExcel3.Workbooks.Add()\n<\/pre>\n<p>And don\u2019t worry, VBScripters: we didn\u2019t forget you. Here\u2019s a VBScript version of our original script, the one that creates three workbooks and then arranges the three horizontally:<\/p>\n<pre class=\"codeSample\">Const xlArrangeStyleHorizontal = -4128\n\nSet objExcel = CreateObject(\"Excel.Application\")\nobjExcel.Visible = True\n\nSet objWorkbook1 = objExcel.Workbooks.Add()\nSet objWorkbook2 = objExcel.Workbooks.Add()\nSet objWorkbook3 = objExcel.Workbooks.Add()\n\nobjExcel.Windows.Arrange(xlArrangeStyleHorizontal)\n<\/pre>\n<p>That should do it, RE. So is the Scripting Guy who writes this column glad to be back to work after a three-day break? Let\u2019s put it this way: he <i>did<\/i> spend the morning busily working on a script, but it was a script that reports back the number of days between the current date and July 4<sup>th<\/sup>, the next official Microsoft day off:<\/p>\n<pre class=\"codeSample\">dtmEndingDate = Date\nintDays = DateDiff(\"d\", dtmEndingDate, #7\/4\/2008#)\nWscript.Echo intDays\n<\/pre>\n<p>Wow, 38 days to go. Oh well, we\u2019ll see you all tomorrow. And the day after that. And the day after that. And \u2026.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a Windows PowerShell script that starts Excel and loads three comma-separated values files into separate workbooks. What I was wondering is whether there\u2019s a way to have PowerShell tile these workbooks, either vertically or horizontally. I poked through various properties and searched the Web for ideas, but I couldn\u2019t find [&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":[48,49,3,45],"class_list":["post-55483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a Windows PowerShell script that starts Excel and loads three comma-separated values files into separate workbooks. What I was wondering is whether there\u2019s a way to have PowerShell tile these workbooks, either vertically or horizontally. I poked through various properties and searched the Web for ideas, but I couldn\u2019t find [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55483","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=55483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55483\/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=55483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}