{"id":68573,"date":"2005-11-08T10:09:00","date_gmt":"2005-11-08T10:09:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/08\/how-can-i-insert-multiple-files-into-a-word-document-putting-a-page-break-between-each-file\/"},"modified":"2005-11-08T10:09:00","modified_gmt":"2005-11-08T10:09:00","slug":"how-can-i-insert-multiple-files-into-a-word-document-putting-a-page-break-between-each-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-insert-multiple-files-into-a-word-document-putting-a-page-break-between-each-file\/","title":{"rendered":"How Can I Insert Multiple Files Into a Word Document, Putting a Page Break Between Each File?"},"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! On May 3<SUP>rd<\/SUP> you showed us how to merge multiple files in Microsoft Word. I\u2019d like to do that, but put a page break between each file.<BR><BR>&#8212; CE<\/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, CE. You know, when we read your email our first thought was this: no way did we show people how to <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/may05\/hey0503.mspx\"><B>merge multiple files<\/B><\/A> back on May 3<SUP>rd<\/SUP> 2005. That wasn\u2019t because we didn\u2019t think that the subject matter &#8211; merging multiple files into a single Microsoft Word document -sounded like the kind of topic we\u2019d tackle in this column. Instead, it\u2019s just that on most days we don\u2019t do <I>any<\/I> work, and we couldn\u2019t see why May 3<SUP>rd<\/SUP> would be an exception. But it turns out you were right: we actually <I>did<\/I> do some work on May 3<SUP>rd<\/SUP>. What were we thinking?<\/P>\n<P>As you no doubt recall (for surely you all memorize each and every <I>Hey, Scripting Guy! <\/I>column), in that initial column we used a script to import all the files found in the folder C:\\Scripts\\Archive into a Microsoft Word document. That was fine, except all we did was type a paragraph return between the end of one file and the beginning of the next. Needless to say, if it\u2019s important to know where one file leaves off and the next one begins, one measly little paragraph return probably won\u2019t be of much use. You\u2019re right, CE: inserting a page break between the files is definitely a step in the right direction. And here\u2019s how you do that:<\/P><PRE class=\"codeSample\">Const wdPageBreak = 7 <\/p>\n<p>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.InsertBreak(wdPageBreak)\nNext\n<\/PRE>\n<P>For the most part we\u2019ve simply replicated our script for inserting multiple files; therefore, we won\u2019t go over the code in line-by-line detail. (If you need that information, take a look at the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/may05\/hey0503.mspx\"><B>original column<\/B><\/A>.) Instead, we\u2019ll focus on the two changes we made.<\/P>\n<P>To begin with, the script now starts off by defining a constant named wdPageBreak and setting the value to 7. We\u2019ll use this constant to tell Word to insert a page break when we call the <B>InsertBreak<\/B> method. (As you probably know, Word allows you to insert other kinds of breaks, including column breaks, text-wrapping breaks, and several kinds of section breaks.)<\/P>\n<P>We then have a bunch of code that binds to the WMI service on the local computer, creates a new Microsoft Word document, and then uses an <B>Associators Of<\/B> query to retrieve a collection of all the files found in the C:\\Scripts\\Archive folder. Once we have that collection we set up a For Each loop to walk through the collection, using the <B>InsertFile<\/B> method to insert each and every file.<\/P>\n<P>Here\u2019s the second place where the new script differs from the original. In our initial script, we called the InsertFile method, then used TypeParagraph() to simulate pressing the ENTER key on the keyboard. This time around, our For Each loop looks like this:<\/P><PRE class=\"codeSample\">For Each objFile in FileList\n    objSelection.InsertFile(objFile.Name)\n    objSelection.InsertBreak(wdPageBreak)\nNext\n<\/PRE>\n<P>As you can see, after inserting the first file we call the InsertBreak method, specifying that we want to insert a page break. It\u2019s only after inserting this page break that we loop around and insert the next file in the collection. The net effect: we end up with page breaks between each of the files we imported into our document.<\/P>\n<P>Yes, much better. And seeing as how we\u2019ve already done <I>some<\/I> work today, we might as well do a little bit more. Here\u2019s a revised script that not only inserts page breaks between each file, but also types in the file path after each page break, formatting that path in 14-point Arial. (The rest of the text is formatted in 10-point Arial.) Here\u2019s what this little bonus script looks like:<\/P><PRE class=\"codeSample\">Const wdPageBreak = 7 <\/p>\n<p>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>objSelection.Font.Name = &#8220;Arial&#8221;<\/p>\n<p>For Each objFile in FileList\n    objSelection.Font.Size = &#8220;14&#8221;\n    objSelection.TypeText objFile.Name\n    objSelection.TypeParagraph()\n    objSelection.Font.Size = &#8220;10&#8221;\n    objSelection.InsertFile(objFile.Name)\n    objSelection.InsertBreak(wdPageBreak)\nNext\n<\/PRE>\n<TABLE id=\"EVE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. For more information about formatting text in a Word document, take a look at the Microsoft Word articles that have appeared in the weekly <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/archive.mspx\"><B>Office Space<\/B><\/A> column.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>You know, that was kind of fun. Maybe, just for a change of pace, we\u2019ll do some work <I>every<\/I> May 3<SUP>rd<\/SUP> from now on.<\/P>\n<P>Nah \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! On May 3rd you showed us how to merge multiple files in Microsoft Word. I\u2019d like to do that, but put a page break between each file.&#8212; CE Hey, CE. You know, when we read your email our first thought was this: no way did we show people how to merge multiple [&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":[715,38,84,49,3,12,5],"class_list":["post-68573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-associators-of","tag-files","tag-microsoft-word","tag-office","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! On May 3rd you showed us how to merge multiple files in Microsoft Word. I\u2019d like to do that, but put a page break between each file.&#8212; CE Hey, CE. You know, when we read your email our first thought was this: no way did we show people how to merge multiple [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68573","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=68573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68573\/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=68573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}