{"id":52253,"date":"2009-10-14T00:01:00","date_gmt":"2009-10-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/10\/14\/hey-scripting-guy-can-i-add-an-endnote-to-a-word-document\/"},"modified":"2009-10-14T00:01:00","modified_gmt":"2009-10-14T00:01:00","slug":"hey-scripting-guy-can-i-add-an-endnote-to-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-add-an-endnote-to-a-word-document\/","title":{"rendered":"Hey, Scripting Guy! Can I Add an Endnote to a Word Document?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>     <!-- AddThis Button END --><\/p>\n<p><img decoding=\"async\" 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\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I have a rather strange question. Do you know how to add a reference to a document in Microsoft Word? I am not talking about by using the GUI tools, but by using a Windows PowerShell or a VBScript to add a reference at the bottom of a page in a document. I am not trying to play &#8220;stump the chump.&#8221; I really do have a good reason for asking this. I have a document at work that contains a paragraph I copied out of a book, and I would like to give credit to the book from whence the information was gathered. I do not want to do a formal footnote thing like I did in college, but I do think that some kind of reference would be appropriate to show that I did not plagiarize anything. I figure that as a writer you would appreciate my honesty. What do you say?<\/p>\n<p class=\"MsoNormal\">&#8212; TL<\/p>\n<p><img decoding=\"async\" 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 class=\"MsoNormal\">Hello TL, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I have spent much of the morning today on <a href=\"https:\/\/twitter.com\/ScriptingGuys\"><font face=\"Segoe\">Twitter<\/font><\/a>, answering questions, tweeting, and retweeting. It is a cool rainy morning, and I am munching on a bowl of raw walnuts and a small piece of 85% <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cacao\"><font face=\"Segoe\">Cacao<\/font><\/a> chocolate, and I&rsquo;m enjoying a pot of freshly brewed <a href=\"http:\/\/en.wikipedia.org\/wiki\/English_Breakfast_tea\"><font face=\"Segoe\">English Breakfast tea<\/font><\/a> with <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cymbopogon\"><font face=\"Segoe\">lemon grass<\/font><\/a> and a stick of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cinnamon\"><font face=\"Segoe\">ceylon<\/font><\/a> cinnamon. I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Weather_Report\"><font face=\"Segoe\">Weather Report<\/font><\/a> this morning on my Zune, as I monitor both Twitter and incoming e-mail sent to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a>. It is a wonderful day and a great morning to be up early and hanging out with virtual friends (as opposed to imaginary friends who hung out with me when I was a child). <\/p>\n<p class=\"MsoNormal\">Write a script to add end notes to a Word document? Dude (or dudette), why didn&#8217;t I think of that? Obviously I can do it, but I never thought of such a thing. Man, oh man, how much different my life would have been when I was in graduate school (along with my good friend, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fred_Flintstone\"><font face=\"Segoe\">Fred Flintstone<\/font><\/a>). If I could have scripted my references instead of wrestling with a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Typewriter\"><font face=\"Segoe\">manual typewriter<\/font><\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Kate_L._Turabian\"><font face=\"Segoe\">Kate Turabian<\/font><\/a>, I might have been able to spend more time in town, instead of in the library. Life would have been more fun, or at the very least, my studies would have been more efficient.<\/p>\n<p class=\"MsoNormal\">Because of the cool jazz from Weather Report, the caffeine from the English Breakfast tea, and the rush from the 85% Cacao, I am susceptible to suggestion. I loved your suggestion for adding end notes to a Word Document so much that I wrote the AddEndNote.ps1 script for you. The complete AddEndNote.ps1 script is shown here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>AddEndNote.ps1<\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">$Text = &#8220;Windows PowerShell Best Practices, Microsoft Press, Forthcoming, Ed Wilson&#8221;<br>$document = &#8220;C:fsoWhyUsePs2.docx&#8221;<br>$word = New-Object -ComObject word.application<br>$word.Visible = $false<br>$doc = $word.Documents.Open($document)<br>$range = $doc.Paragraphs.item(4).range<br>$endNotes = $doc.endNotes<br>$endNotes.Add($range,&#8221;reference&#8221;,$text) | Out-Null<br>$doc.Save()<br>$doc.close()<br>$word.quit()<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Before jumping into the AddEndNote.ps1 script, look at the following image, which includes an excerpt from the Microsoft Press book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-2-0-Best-Practices\/dp\/0735626464\"><font face=\"Segoe\">Windows PowerShell 2.0 Best Practices<\/font><\/a>, by yours truly:<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Image of excerpt from Windows PowerShell 2.0 Best Practices\" alt=\"Image of excerpt from Windows PowerShell 2.0 Best Practices\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1014\/hsg-10-14-09-01.jpg\" width=\"600\" height=\"429\"><\/p>\n<p class=\"MsoNormal\">Because the material is excerpted from a book, I need to add attribution to the selection&mdash;the AddEndNote.ps1 script is used to do that. The first step is to initialize:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$Text = &#8220;Windows PowerShell Best Practices, Microsoft Press, Forthcoming, Ed Wilson&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$document = &#8220;C:fsoWhyUsePs2.docx&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Next you need to create the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb148369.aspx\"><font face=\"Segoe\">word.application<\/font><\/a> object, which is the main object used in working with Microsoft Word automation. In nearly every case of Microsoft Word automation, you will need to first create the application object. In the AddEndNote.ps1 script, the application object is stored in the <b>$word<\/b> variable. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$word = New-Object -ComObject word.application<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have the <b>word.application<\/b> object stored in a variable, you need to decide if you wish to make the Microsoft Word visible or invisible. When I am in the process of writing my script, I will generally make the Microsoft Word document visible. After I have the script working properly, I will make it invisible. To control the visibility of the Microsoft Word document, you need to set the appropriate value for the <b>visible<\/b> property. To make the Microsoft Word document visible, you set the <b>visible<\/b> property to <b>$true<\/b>. To make the Microsoft Word document invisible, you set the <b>visible<\/b> property to <b>$false<\/b>. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$word.Visible = $false<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the <b>word.application<\/b> object has been created, the <b>Documents<\/b> property is queried to return a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb244910.aspx\"><font face=\"Segoe\">documents collection object<\/font><\/a>. The <b>documents<\/b> collection object provides the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb216319.aspx\">open method<\/a>, which you use to open a Microsoft Word document. The <b>open<\/b> method returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb244898.aspx\"><font face=\"Segoe\">document object<\/font><\/a> that is stored in the <b>$doc<\/b> variable. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$doc = $word.Documents.Open($document)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the <b>document<\/b> object has been created, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb241567.aspx\"><font face=\"Segoe\">paragraphs<\/font><\/a> property is used to return a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb178832.aspx\"><font face=\"Segoe\">paragraphs collection object<\/font><\/a>. To return a specific <b>paragraph<\/b> object from the <b>paragraphs collection<\/b> object, you use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb215605.aspx\"><font face=\"Segoe\">item<\/font><\/a> method from the <b>paragraphs<\/b> collection object. Paragraph numbering begins at one and includes headings in the count. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb259496.aspx\"><font face=\"Segoe\">paragraph object<\/font><\/a> supplies a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb255952.aspx\"><font face=\"Segoe\">range property<\/font><\/a>. When you query the <b>range<\/b> property, it returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb178841.aspx\"><font face=\"Segoe\">range object<\/font><\/a>. The <b>range<\/b> object is stored in the <b>$range<\/b> variable as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$range = $doc.Paragraphs.item(4).range<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>endnotes<\/b> property of the <b>document<\/b> object is used to return an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211916.aspx\"><font face=\"Segoe\">endnotes collection object<\/font><\/a>. The <b>endnotes<\/b> collection object is stored in the <b>$endNotes<\/b> variable as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$endNotes = $doc.endNotes<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you have an <b>endnotes<\/b> collection object, you can use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb237093.aspx\"><font face=\"Segoe\">add method<\/font><\/a> to add an endnote to the document. The <b>add<\/b> method accepts three parameters: The first is the <b>range<\/b> object that represents the location for placement of the endnote; the second is the text for the custom reference mark; and the last parameter is the text of the endnote. When the <b>add<\/b> method is called, it returns an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb211912.aspx\"><font face=\"Segoe\">endnote object<\/font><\/a>. Because I am not using the returned <b>endnote<\/b> object in the script, I pipe it to the <b>Out-Null<\/b> cmdlet. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$endNotes.Add($range,&#8221;reference&#8221;,$text) | Out-Null<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After the endnote has been added to the Microsoft Word document, it is time to clean up the scripting environment. To do this, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb241528.aspx\"><font face=\"Segoe\">save method<\/font><\/a> is called from the <b>document<\/b> object to save the changes to the Microsoft Word document. After the changes have been saved, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb214403.aspx\"><font face=\"Segoe\">close method<\/font><\/a> from the <b>document<\/b> object is called to close the document. And after the document has been closed, it is time to call the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb215475.aspx\"><font face=\"Segoe\">quit method<\/font><\/a> from the <b>application<\/b> object. <\/p>\n<p class=\"Readeraidonly\">If you do not call the <b>quit<\/b> method from the <b>application<\/b> object, you will leave an instance of Microsoft Word running in memory. If you are running several scripts and you do not call the <b>quit<\/b> method, it would be possible to consume all of your available memory on your computer, and it would cause performance problems at the very least. In the worst case scenario, it might be possible to crash your computer. As a best practice when scripting any Microsoft Office product, ensure you properly clean up after the script. <\/p>\n<p class=\"MsoNormal\">The cleanup code for the AddEndNote.ps1 script is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$doc.Save()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$doc.close()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$word.quit()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When the AddEndNote.ps1<b> <\/b>script is run, it adds a reference mark to the end of the last paragraph and adds an endnote at the bottom of the page. This is shown here: <b><\/p>\n<p><\/b><\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of reference mark and endnote\" alt=\"Image of reference mark and endnote\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1014\/hsg-10-14-09-02.jpg\" width=\"600\" height=\"429\"><\/p>\n<p class=\"MsoNormal\"><br>TL, that was such a cool suggestion, and it was fun too! <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/www.twitter.com\/scriptingguys\/\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/www.facebook.com\/group.php?gid=5901799452\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@Microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;!&#8211;  <P class=\"MsoNormal\">When the AddEndNote.ps1<B> <\/B>script is run, it adds a reference mark to the end of the last paragraph and adds an endnote at the bottom of the page. This is shown here: <B><\/B><\/P>      <P class=\"Fig-Graphic\">http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1014\/hsg-10-14-09-02.jpg<\/P>  &#8211;&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a rather strange question. Do you know how to add a reference to a document in Microsoft Word? I am not talking about by using the GUI tools, but by using a Windows PowerShell or a VBScript to add a reference at the bottom of a page in a document. [&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,45],"class_list":["post-52253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a rather strange question. Do you know how to add a reference to a document in Microsoft Word? I am not talking about by using the GUI tools, but by using a Windows PowerShell or a VBScript to add a reference at the bottom of a page in a document. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52253","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=52253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52253\/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=52253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}