{"id":113,"date":"2015-09-10T10:34:23","date_gmt":"2015-09-10T10:34:23","guid":{"rendered":"http:\/\/99excel.com\/?p=113"},"modified":"2021-05-14T16:09:21","modified_gmt":"2021-05-14T16:09:21","slug":"split-word-document-into-multiple-documents-vba","status":"publish","type":"post","link":"https:\/\/99excel.com\/home\/split-word-document-into-multiple-documents-vba\/","title":{"rendered":"Split word document into multiple documents vba"},"content":{"rendered":"<p>Option Explicit &#8216;This goes in the Declarations section of your code module.<br \/> &#8216;Hopefully it is already there because you have ticked the &#8216;Require Variable Declaration&#8217; <em><br \/>checkbox. (Tools\/Options, Editor tab.)<\/p>\n<p>Sub SplitIntoPages()<br \/> Dim docMultiple As Document<br \/> Dim docSingle As Document<br \/> Dim rngPage As Range<br \/> Dim iCurrentPage As Integer<br \/> Dim iPageCount As Integer<br \/> Dim strNewFileName As String<\/p>\n<p> Application.ScreenUpdating = False &#8216;Makes the code run faster and reduces screen _<br \/> flicker a bit.<br \/> Set docMultiple = ActiveDocument &#8216;Work on the active document _<br \/> (the one currently containing the Selection)<br \/> Set rngPage = docMultiple.Range &#8216;instantiate the range object<br \/> iCurrentPage = 1<br \/> &#8216;get the document&#8217;s page count<br \/> iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)<br \/> Do Until iCurrentPage &gt; iPageCount<br \/> If iCurrentPage = iPageCount Then<br \/> rngPage.End = ActiveDocument.Range.End &#8216;last page (there won&#8217;t be a next page)<br \/> Else<br \/> &#8216;Find the beginning of the next page<br \/> &#8216;Must use the Selection object. The Range.Goto method will not work on a page<br \/> Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 4<br \/> &#8216;Set the end of the range to the point between the pages<br \/> rngPage.End = Selection.Start<br \/> End If<br \/> rngPage.Copy &#8216;copy the page into the Windows clipboard<br \/> Set docSingle = Documents.Add &#8216;create a new document<br \/> docSingle.Range.Paste &#8216;paste the clipboard contents to the new document<br \/> &#8216;remove any manual page break to prevent a second blank<br \/> docSingle.Range.Find.Execute Findtext:=&#8221;^m&#8221;, ReplaceWith:=&#8221;&#8221;<br \/> &#8216;build a new sequentially-numbered file name based on the original multi-paged file name and path<br \/> strNewFileName = Replace(docMultiple.FullName, &#8220;.doc&#8221;, &#8220;<\/em>&#8221; &amp; Right$(&#8220;000&#8221; &amp; iCurrentPage, 4) &amp; &#8220;.doc&#8221;)<br \/> docSingle.SaveAs strNewFileName &#8216;save the new single-paged document<br \/> iCurrentPage = iCurrentPage + 4 &#8216;move to the next page<br \/> docSingle.Close &#8216;close the new document<br \/> rngPage.Collapse wdCollapseEnd &#8216;go to the next page<br \/> Loop &#8216;go to the top of the do loop<br \/> Application.ScreenUpdating = True &#8216;restore the screen updating<\/p>\n<p> &#8216;Destroy the objects.<br \/> Set docMultiple = Nothing<br \/> Set docSingle = Nothing<br \/> Set rngPage = Nothing<br \/>End Sub<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Option Explicit &#8216;This goes in the Declarations section of your code module. &#8216;Hopefully it is already there because you have ticked the &#8216;Require Variable Declaration&#8217; checkbox. (Tools\/Options, Editor tab.) Sub SplitIntoPages() Dim docMultiple As Document Dim docSingle As Document Dim rngPage As Range Dim iCurrentPage As Integer Dim iPageCount As Integer Dim strNewFileName As String &#8230; <a title=\"Split word document into multiple documents vba\" class=\"read-more\" href=\"https:\/\/99excel.com\/home\/split-word-document-into-multiple-documents-vba\/\" aria-label=\"More on Split word document into multiple documents vba\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":1,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":898,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/posts\/113\/revisions\/898"}],"wp:attachment":[{"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/99excel.com\/home\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}