{"id":54603,"date":"2009-01-15T11:56:00","date_gmt":"2009-01-15T11:56:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/01\/15\/hey-scripting-guy-how-can-i-modify-the-footers-of-an-office-excel-spreadsheet\/"},"modified":"2009-01-15T11:56:00","modified_gmt":"2009-01-15T11:56:00","slug":"hey-scripting-guy-how-can-i-modify-the-footers-of-an-office-excel-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-modify-the-footers-of-an-office-excel-spreadsheet\/","title":{"rendered":"Hey, Scripting Guy! How Can I Modify the Footers of an Office Excel Spreadsheet?"},"content":{"rendered":"<h2><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\"> <\/h2>\n<p>Hey, Scripting Guy! I have a simple question for you: How can I modify the footers in an Office Excel spreadsheet?<\/p>\n<p>&#8211; MN<\/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\"><\/p>\n<p>Hi MN,<\/p>\n<p>I have a simple answer for you as well. You assign a value to the appropriate footer property of the <b>pagesetup<\/b> object. If you would prefer a longer answer, read on.<\/p>\n<table class=\"dataTable\" id=\"E2C\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">The Office Excel automation model is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb255823.aspx\">documented here on MSDN<\/a>. Here are previous <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/office.mspx\">&#8220;Hey, Scripting Guy!&#8221; articles<\/a> that illustrate using VBScript to automate Microsoft Excel. The <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\">Office space archive<\/a> also has numerous examples of using VBScript with Office Excel. You can also find lots of examples of automating Office Excel in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/default.mspx?mfr=true\">Script Center Repository<\/a>. Perhaps I also saved the best for last. The Community-Submitted Scripts Center has a <i>ton<\/i> of scripts that automate Office Excel. Here is the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/csc\/scripts\/office\/excel\/index.mspx\">direct link to that gold mine of information<\/a>. Look in the Microsoft Office section. If you are new to using Microsoft PowerShell, you can get a jump-start on it with the resources in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/msh\/default.mspx?mfr=true\">Windows PowerShell hub<\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Today, we have a script called <b>CreateExcelFooter.ps1<\/b> that creates an instance of the Office Excel application object, makes it visible, adds a workbook, retrieves the worksheet, and then adds three different footers to it. Just for fun, we also show the spreadsheet in print preview fashion. The script is seen&nbsp;here:<\/p>\n<pre class=\"codeSample\">$excel = new-object -comobject excel.application\n$excel.visible = $true\n$workbook = $excel.workbooks.add()\n$worksheet = $workbook.worksheets.item(1)\n$worksheet.pageSetup.LeftFooter = \"Date: &amp;D\"\n$worksheet.pageSetup.CenterFooter = \"Time: &amp;T\"\n$worksheet.pageSetup.RightFooter = \"Page &amp;P of &amp;N\"\n$worksheet.cells.item(1,1) = \"a\"\n$workbook.printPreview()\n<\/pre>\n<p>The first thing we need to do is to create an instance of the <b>Excel Application<\/b> object. We do this by using the <b>New-Object<\/b> cmdlet and specifying the <b>comobject<\/b> parameter. We store the application object that is returned in the <b>$excel<\/b> variable. This line of code is seen here: <\/p>\n<pre class=\"codeSample\">$excel = new-object -comobject excel.application<\/pre>\n<p>Next we want to be able to see our Office Excel application, so we make the <b>visible<\/b> property true. In cases in which we want to automate a large number of items and we have our script working properly, I will generally not make Office Excel visible. This allows the script to run and to not tie up things on the desktop. This line of code is seen here: <\/p>\n<pre class=\"codeSample\">$excel.visible = $true<\/pre>\n<p>After we have the Office Excel application visible, we decide to add a workbook. Workbooks are a key component of Office Excel, and we nearly always need to add a workbook to the script. To do this, we use the <b>workbooks<\/b> property of the <b>application<\/b> object to return a <b>workbooks<\/b> collection, and we use the <b>add<\/b> method. We use the <b>$workbook<\/b> variable to hold the <b>workbook<\/b> object that is created: <\/p>\n<pre class=\"codeSample\">$workbook = $excel.workbooks.add()<\/pre>\n<p>We now use the <b>worksheets<\/b> property of the <b>workbook<\/b> object to retrieve the first worksheet. We use the <b>item<\/b> method of the <b>worksheets<\/b> object to return the first worksheet. This is seen here:<\/p>\n<pre class=\"codeSample\">$worksheet = $workbook.worksheets.item(1)<\/pre>\n<p>When we have the <b>worksheet<\/b> object, we can use the <b>pagesetup<\/b> property to return the <b>pagesetup<\/b> object, and then we can access the <b>leftfooter<\/b> property. To change the left footer, you just assign an acceptable value to the property. We use <b>&amp;D<\/b>, which is a formatting code that tells Office Excel to add the date (this is <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb225426.aspx\" target=\"_blank\">documented here<\/a>): <\/p>\n<pre class=\"codeSample\">$worksheet.pageSetup.LeftFooter = \"Date: &amp;D\"<\/pre>\n<p>We can do the same thing to the center footer and to the right footer. For the center footer, we use the <b>&amp;T<\/b> formatting code to display the current time. For the right footer, we use the <b>&amp;P<\/b> code to display the current page number, and the <b>&amp;N<\/b> to display the number of pages in the worksheet. You can actually use any of the coded values seen in Table 1 in either the header or in the footer:<\/p>\n<p><b>Table 1 Excel format codes used for headers and footers<\/b><\/p>\n<table class=\"dataTable\" id=\"ECH\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead>\n<tr class=\"stdHeader\" vAlign=\"top\">\n<td class=\"\" id=\"colEEH\">Format code<\/td>\n<td class=\"\" id=\"colEIH\">Meaning<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;D<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the current date<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;T<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the current time<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;F<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the name of the document<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;A<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the name of the workbook tab<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;P<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the page number<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;P+<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the page number plus the specified number<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;P-<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the page number minus the specified number<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;&amp;<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints a single ampersand<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;N<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the total number of pages in the document<\/p>\n<\/td>\n<\/tr>\n<tr class=\"evenRecord\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;Z<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Prints the file path<\/p>\n<\/td>\n<\/tr>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\">&amp;G<\/p>\n<\/td>\n<td class=\"\">\n<p class=\"lastInCell\">Inserts an image<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>This is shown&nbsp;here:<\/p>\n<pre class=\"codeSample\">$worksheet.pageSetup.CenterFooter = \"Time: &amp;T\"\n$worksheet.pageSetup.RightFooter = \"Page &amp;P of &amp;N\"\n<\/pre>\n<p>In Office Excel 2007 you cannot print preview a spreadsheet that does not have any data in it. So we need to at least add a single character in a single cell:<\/p>\n<pre class=\"codeSample\">$worksheet.cells.item(1,1) = \"a\"<\/pre>\n<p>Because I do not like to mouse around if I do not have to and because the best way to see the fine spreadsheet we have created is in print preview mode, we decided to go ahead and add this to the script:<\/p>\n<pre class=\"codeSample\">$workbook.printPreview()<\/pre>\n<p>When we run the script, we see this:<\/p>\n<p><img decoding=\"async\" height=\"400\" alt=\"Image of the worksheet in print preview mode\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/january\/hey0115\/hsg_excel4_01.jpg\" width=\"283\" border=\"0\"><\/p>\n<p>&nbsp;<\/p>\n<p>Well that is about all there is to working with Office Excel footers. MN, I hope you enjoyed the article, and find the information useful. See you tomorrow for Quick-Hits Friday.<\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><span class=\"Apple-style-span\"><b><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/b><\/span><\/font><\/p>\n<p><font class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><b><\/b><\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a simple question for you: How can I modify the footers in an Office Excel spreadsheet? &#8211; MN Hi MN, I have a simple answer for you as well. You assign a value to the appropriate footer property of the pagesetup object. If you would prefer a longer answer, read [&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":[710,48,49,3,45],"class_list":["post-54603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a simple question for you: How can I modify the footers in an Office Excel spreadsheet? &#8211; MN Hi MN, I have a simple answer for you as well. You assign a value to the appropriate footer property of the pagesetup object. If you would prefer a longer answer, read [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54603","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=54603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54603\/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=54603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}