{"id":7751,"date":"2015-02-19T00:01:00","date_gmt":"2015-02-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/02\/19\/add-custom-headers-to-folder-full-of-word-documents\/"},"modified":"2019-02-18T10:30:34","modified_gmt":"2019-02-18T17:30:34","slug":"add-custom-headers-to-folder-full-of-word-documents","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/add-custom-headers-to-folder-full-of-word-documents\/","title":{"rendered":"Add Custom Headers to Folder Full of Word Documents"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about reading a CSV file and adding custom headers to a folder full of Word documents.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. The snow continues to befuddle the drivers around here, and as a result, the Scripting Wife and I are staying home. This has given me a lot of time to sit, look at the giant slip-and-slide outside (also called a road), and write Windows PowerShell scripts. When I combine Word and Windows PowerShell, it is guaranteed to produce a lot of fun.<\/p>\n<h2>Begin with my CSV file<\/h2>\n<p>The other day, I created a CSV file with the titles of various plays that will be covered in a Shakespeare class. I then copied a research paper template, and I created all of the document files that will be required for the class. Yesterday I talked about using Windows PowerShell to add a header to a document file. Today, I add an additional column to the CSV file, and go through it to add the headers to the document files. This illustrates the power of using Windows PowerShell, because I can add custom headers&mdash;that is, the title of each research paper as the header for the file. Here is the CSV file:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8535.hsg-2-19-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8535.hsg-2-19-15-01.png\" alt=\"Image of text\" title=\"Image of text\" \/><\/a><\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; Today&rsquo;s script builds off of yesterday&#039;s script. You should refer to yesterday&rsquo;s blog post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-add-headers-to-word-documents\/\" target=\"_blank\">Use PowerShell to Add Headers to Word Documents<\/a>, for an explanation of that script.<\/p>\n<p>The first thing I do is specify the path to the folder that contains the various Word documents. I then import the CSV file by using the <b>Import-CSV<\/b> cmdlet. Because the file does not have a CSV file extension, it does not mean that Windows PowerShell does not know how to properly import it. This code is shown here:<\/p>\n<p style=\"margin-left:30px\">$filePath = &quot;C:\\lit&quot;<\/p>\n<p style=\"margin-left:30px\">$topics = import-csv -Path C:\\lit\\Shakespeare.txt<\/p>\n<p>Now I create my two enumerations, the Word.Application object, and I set the application to be invisible. This script was discussed yesterday, and it is shown here:<\/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 = $false<\/p>\n<p>I gather a collection of file objects by using the <b>Get-ChildItem<\/b> cmdlet to read the location that contains my Word documents. I use the <b>FullName<\/b> property to specify the file name and the path to the file. As shown here, I use <b>Foreach<\/b> to walk through the collection of file objects:<\/p>\n<p style=\"margin-left:30px\">Foreach ($file in Get-ChildItem $filepath)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$doc = $word.documents.open($file.FullName)<\/p>\n<p>It is time to obtain my header object and to right-align the header text. This is the same code that I used yesterday:<\/p>\n<p style=\"margin-left:30px\">$header = $section.headers.item($HeaderFooterIndex::wdHeaderFooterFirstPage)<\/p>\n<p style=\"margin-left:30px\">$header.range.InsertAlignmentTab($alignmentTab::wdRight)<\/p>\n<p>I need a way to find the appropriate topic title that I want to add to my header. Because I already imported my CSV file, I did not want to cycle through it. I hit on the idea of using the <b>Where<\/b> method in Windows PowerShell&nbsp;4.0, which is added to objects so that I could find the specific play. Then I return the topic associated with that play. The rest of the script is the same as I used yesterday:<\/p>\n<p style=\"margin-left:30px\">$header.range.InsertAfter($($topics.where({$file.BaseName -match $PSItem.play}).topic))<\/p>\n<p>I save and close the document file. I then close the script block from the <b>Foreach<\/b>:<\/p>\n<p style=\"margin-left:30px\">$doc.save()<\/p>\n<p style=\"margin-left:30px\">&nbsp;$doc.close() }<\/p>\n<p>The last thing to do is quit the Word.Application object:<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;$word.quit()<\/p>\n<p>The complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">$filePath = &quot;C:\\lit&quot;<\/p>\n<p style=\"margin-left:30px\">$topics = import-csv -Path C:\\lit\\Shakespeare.txt<\/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 = $false<\/p>\n<p style=\"margin-left:30px\">Foreach ($file in Get-ChildItem $filepath)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$doc = $word.documents.open($file.FullName)<\/p>\n<p style=\"margin-left:30px\">&nbsp;$section = $doc.sections.item(1)<\/p>\n<p style=\"margin-left:30px\">&nbsp;$header = $section.headers.item($HeaderFooterIndex::wdHeaderFooterFirstPage)<\/p>\n<p style=\"margin-left:30px\">&nbsp;$header.range.InsertAlignmentTab($alignmentTab::wdRight)<\/p>\n<p style=\"margin-left:30px\">&nbsp;$header.range.InsertAfter($($topics.where({$file.BaseName -match $PSItem.play}).topic))<\/p>\n<p style=\"margin-left:30px\">&nbsp;$doc.save()<\/p>\n<p style=\"margin-left:30px\">&nbsp;$doc.close() }<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$word.quit()<\/p>\n<p>Here is one of my newly modified Word documents:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-19-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-19-15-02.png\" alt=\"Image of text\" title=\"Image of text\" \/><\/a><\/p>\n<p>That is all there is to using Windows PowerShell to add custom headers to Word documents. Join me tomorrow when I will talk about more fun Windows PowerShell 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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about reading a CSV file and adding custom headers to a folder full of Word documents. Microsoft Scripting Guy, Ed Wilson, is here. The snow continues to befuddle the drivers around here, and as a result, the Scripting Wife and I are staying home. This has given me [&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-7751","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 reading a CSV file and adding custom headers to a folder full of Word documents. Microsoft Scripting Guy, Ed Wilson, is here. The snow continues to befuddle the drivers around here, and as a result, the Scripting Wife and I are staying home. This has given me [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7751","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=7751"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7751\/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=7751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}