{"id":69893,"date":"2005-05-03T10:04:00","date_gmt":"2005-05-03T10:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/03\/how-can-i-insert-files-into-a-word-document\/"},"modified":"2005-05-03T10:04:00","modified_gmt":"2005-05-03T10:04:00","slug":"how-can-i-insert-files-into-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-insert-files-into-a-word-document\/","title":{"rendered":"How Can I Insert Files into a Word Document?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" 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\"> \n<P>Hey, Scripting Guy! Awhile back you showed people how they could <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/dec04\/hey1201.mspx\"><B>combine several text files into a single file<\/B><\/A>. I\u2019d like to do the same thing in Word. How can I insert files into a Word document?<BR><BR>&#8212; HK<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" 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\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, HK. Believe it or not it\u2019s actually easier to insert files into a Microsoft Word document than it is to insert a text file into another text file. That\u2019s because the Word <B>Selection<\/B> object has a method &#8211; <B>InsertFile<\/B> &#8211; that does one thing and one thing only: it opens up a specified file and inserts it into the current document. And InsertFile isn\u2019t limited to opening just text files: if it\u2019s a type of file that Word can handle, then InsertFile can open it and insert it in your document.<\/P>\n<P>Let\u2019s take a look at a script that imports a pair of text files (C:\\Scripts\\Hardware.txt and C:\\Scripts\\Software.txt):<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection<\/p>\n<p>objSelection.TypeText &#8220;Hardware Inventory&#8221;\nobjSelection.TypeParagraph()\nobjSelection.InsertFile(&#8220;C:\\Scripts\\Hardware.txt&#8221;)<\/p>\n<p>objSelection.TypeParagraph()<\/p>\n<p>objSelection.TypeText &#8220;Software Inventory&#8221;\nobjSelection.TypeParagraph()\nobjSelection.InsertFile(&#8220;C:\\Scripts\\Software.txt&#8221;)\n<\/PRE>\n<P>Like we said, <I>way<\/I> easier than trying to combine multiple files in a single text file. We begin by creating an instance of the <B>Word.Application<\/B> object and setting the <B>Visible<\/B> property to True; we then add a document and create an instance of the Selection object. That\u2019s what these lines of code are for:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection\n<\/PRE>\n<P>That was easy, but now it gets even easier. We want a heading for our hardware inventory so we type he heading <I>Hardware Inventory<\/I> followed by a paragraph return; that\u2019s what we do here:<\/P><PRE class=\"codeSample\">objSelection.TypeText &#8220;Hardware Inventory&#8221;\nobjSelection.TypeParagraph()\n<\/PRE>\n<P>And now we\u2019re ready to insert a file. That takes just one line of code:<\/P><PRE class=\"codeSample\">objSelection.InsertFile(&#8220;C:\\Scripts\\Hardware.txt&#8221;)\n<\/PRE>\n<P>That\u2019s it: you call the <B>InsertFile<\/B> method followed by the path of the file being inserted into the document. We then add another paragraph return and the heading <I>Software Inventory<\/I>, and then insert our second file, C:\\Scripts\\Software.txt. It\u2019s so easy we could sit here all day inserting files into Word documents!<\/P>\n<P>Before we do that, however, let\u2019s show you a handy little variation on the preceding script. This one creates a new Word document and then inserts all the files found in the folder C:\\Scripts\\Archive. We won\u2019t bother explaining the code line-by-line, but you should be able to figure out most of it yourself:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection<\/p>\n<p>Set FileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Scripts\\Archive&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In FileList\n    objSelection.InsertFile(objFile.Name)\n    objSelection.TypeParagraph()\nNext\n<\/PRE>\n<P>That <I>is<\/I> cool, isn\u2019t it? For more exciting scripts that use Microsoft Office applications take a peek at the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\"><B>Office Space<\/B><\/A> column located elsewhere in the Script Center. (The preceding advertisement was paid for by the Committee to Get More People to Read the Office Space Column.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Awhile back you showed people how they could combine several text files into a single file. I\u2019d like to do the same thing in Word. How can I insert files into a Word document?&#8212; HK Hey, HK. Believe it or not it\u2019s actually easier to insert files into a Microsoft Word 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":[38,84,49,3,12,5,395],"class_list":["post-69893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-microsoft-word","tag-office","tag-scripting-guy","tag-storage","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Awhile back you showed people how they could combine several text files into a single file. I\u2019d like to do the same thing in Word. How can I insert files into a Word document?&#8212; HK Hey, HK. Believe it or not it\u2019s actually easier to insert files into a Microsoft Word document [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69893","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=69893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69893\/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=69893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}