{"id":7771,"date":"2015-02-18T00:01:00","date_gmt":"2015-02-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/02\/18\/use-powershell-to-add-headers-to-word-documents\/"},"modified":"2019-02-18T10:30:35","modified_gmt":"2019-02-18T17:30:35","slug":"use-powershell-to-add-headers-to-word-documents","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-add-headers-to-word-documents\/","title":{"rendered":"Use PowerShell to Add Headers to Word Documents"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add headers to Microsoft Word documents.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It snowed in Charlotte yesterday. It was not really a surprise because the weather service did a good job of warning that it would happen. Although the Scripting Wife and I were a bit worried that the power would go off, it turned out to be a huge non-event&mdash;at least for us. For the thousands of people who lost their power&#8230;well, that was a different story. It seems that these sorts of things always pile up&mdash;we lose the power, it is cold, there is ice and snow&mdash;it all happens at the same time. Why can&rsquo;t we have a nice snowstorm during the summer when it is over 100 degrees with 98 percent humidity? Oh well.<\/p>\n<p>One of the things that seems to pile on is using Windows PowerShell to automate Microsoft Word. What am I talking about? Well, about the time I am finished with one thing, I remember that there is something else I need to do. For example, if I had added my script in the static header of the Word document template that I created yesterday, I could avoid having to write that script today. The cool thing is that when I know how to automate adding a header, I can customize the header, such as to include the title of the article or some other information that would be helpful.<\/p>\n<h2>Begin with the Word.Application object<\/h2>\n<p>The place to begin is with the Word.Application object. I have to create that object as my entry point for automating Word. It is a COM object, so I also need to specify that. When I am writing a single script, I like to make the Word object visible. After I get everything working, I do not need to make the object visible, and I do that by setting the <b>Visible<\/b> property to <b>$false<\/b>. Here are those two lines of code:<\/p>\n<p style=\"margin-left:30px\">$word = New-Object -comobject word.application<\/p>\n<p style=\"margin-left:30px\">$word.visible = $true<\/p>\n<p>I like to use the standard enumerations because it makes the script more readable. It also facilitates looking stuff up on MSDN. To do this, I create two types, which I will use directly in the script:<\/p>\n<p style=\"margin-left:30px\">$HeaderFooterIndex = &quot;microsoft.office.interop.word.WdHeaderFooterIndex&quot; -as [type]<\/p>\n<p style=\"margin-left:30px\">$alignmentTab = &quot;microsoft.office.interop.word.WdAlignmentTabAlignment&quot; -as [type]<\/p>\n<p>To edit a Word document, I need to open it. I am working with my Template document right now, so I specify my path to the template, and I then call the <b>Open<\/b> method. A document object returns and I store it in a variable named <b>$doc<\/b>. This is shown here:<\/p>\n<p style=\"margin-left:30px\">$filePath = &quot;C:\\lit\\PaperTemplate.docx&quot;<\/p>\n<p style=\"margin-left:30px\">$doc = $word.documents.open($filePath)<\/p>\n<p>I now need to retrieve a <b>Section<\/b> object. Keep in mind that the sections begin numbering with 1 as opposed to 0. To gain access to the <b>Section<\/b> object, I use the <b>Item<\/b> method to retrieve a specific section from the <b>Sections<\/b> collection. When I call the <b>Sections<\/b> property from the <b>Document<\/b> object, it returns a Sections collection. This collection includes the <b>Item<\/b> method. The code is shown here:<\/p>\n<p style=\"margin-left:30px\">$section = $doc.sections.item(1)<\/p>\n<p>Now I want to edit my first page header. To do this, I use the <b>Headers<\/b> property from the <b>Section<\/b> object to return a <b>Headers<\/b> collection. I then use the <b>Item<\/b> method to gain access to a specific header. I want the header for the first page, so I use the <b>wdHeaderFooterFirstPage<\/b> enumeration. This is a static property from the type I loaded earlier in my script.<\/p>\n<p>In the Windows PowerShell ISE, I can use IntelliSense to retrieve the possible values, and therefore, to avoid having to bounce back and forth on MSDN looking stuff up. Here is that code:<\/p>\n<p style=\"margin-left:30px\">$header = $section.headers.item($HeaderFooterIndex::wdHeaderFooterFirstPage)<\/p>\n<p>Now, I need my header to be right-justified on my paper. So to do this, I need to use the <b>wdRight<\/b> enumeration from the <b>WdAlignmenttabAlignment<\/b> type that I added earlier. I use this value to specify that I want to insert an alignment tab. There is a method from the <b>Range<\/b> object that permits me to do this. All I do is call the method, and tell it what kind of alignment tab I want to insert. This is shown here:<\/p>\n<p style=\"margin-left:30px\">$header.range.InsertAlignmentTab($alignmentTab::wdRight)<\/p>\n<p>After I have inserted my alignment tab, I can finally insert my header text. To do this, I need to use the <b>InsertAfter<\/b> method from the <b>Range<\/b> object. As shown here, I simply give it a string that represents the text I want to insert:<\/p>\n<p style=\"margin-left:30px\">$header.range.InsertAfter(&quot;First Page Header&quot;)<\/p>\n<p>Now I need to save my changes, close the Word document, and call it quits:<\/p>\n<p style=\"margin-left:30px\">$doc.save()<\/p>\n<p style=\"margin-left:30px\">$doc.close()<\/p>\n<p style=\"margin-left:30px\">$word.quit()<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">$filePath = &quot;C:\\lit\\PaperTemplate.docx&quot;<\/p>\n<p style=\"margin-left:30px\">$HeaderFooterIndex = &quot;microsoft.office.interop.word.WdHeaderFooterIndex&quot; -as [type]<\/p>\n<p style=\"margin-left:30px\">$alignmentTab = &quot;microsoft.office.interop.word.WdAlignmentTabAlignment&quot; -as [type]<\/p>\n<p style=\"margin-left:30px\">$word = New-Object -comobject word.application<\/p>\n<p style=\"margin-left:30px\">$word.visible = $true<\/p>\n<p style=\"margin-left:30px\">$doc = $word.documents.open($filePath)<\/p>\n<p style=\"margin-left:30px\">$section = $doc.sections.item(1)<\/p>\n<p style=\"margin-left:30px\">$header = $section.headers.item($HeaderFooterIndex::wdHeaderFooterFirstPage)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">$header.range.InsertAlignmentTab($alignmentTab::wdRight)<\/p>\n<p style=\"margin-left:30px\">$header.range.InsertAfter(&quot;First Page Header&quot;)<\/p>\n<p style=\"margin-left:30px\">$doc.save()<\/p>\n<p style=\"margin-left:30px\">$doc.close()<\/p>\n<p style=\"margin-left:30px\">$word.quit()<\/p>\n<p>When I run the script, the Word document pops up, and then it closes. I open the file, and I see that the header was added. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-18-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-18-15-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>That is all there is to using Windows PowerShell to add a header to a document. Word Week continues tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add headers to Microsoft Word documents. Microsoft Scripting Guy, Ed Wilson, is here. It snowed in Charlotte yesterday. It was not really a surprise because the weather service did a good job of warning that it would happen. Although the Scripting Wife and [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[49,3,45,360],"class_list":["post-7771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-office","tag-scripting-guy","tag-windows-powershell","tag-word"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add headers to Microsoft Word documents. Microsoft Scripting Guy, Ed Wilson, is here. It snowed in Charlotte yesterday. It was not really a surprise because the weather service did a good job of warning that it would happen. Although the Scripting Wife and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7771","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7771"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7771\/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=7771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}