microsoft-word-2013

Split Documents with Microsoft Word Macro


Split Documents – Microsoft Word

This tutorial will show you how to setup a VB macro in Word to split a document into separate ones. Someone tweaked Doug Robbins original code, and I have then tweaked his a little.

Step 1

Create a new word document. Go to View > Outline Mode.

word-macro1

Step 2

Go ahead and create three sections just to see how it will work. The first section, just type Test1. Then click on “Show Document” and click the create button right next to it. This will create subdocuments. Example below, Test1 is the main one, and Test2 and Test3 are subdocuments. Hopefully that makes sense.

word-macro2

Step 3

Save the document anywhere you like, saving as .docx is fine.

Step 4

Now we are going to setup the macro. Click on View > Macros. Name the macro whatever you want and click Create.

word-macro3

Step 5

Now we are going to replace everything with the code below: Once you paste this in there, you will want to change one thing. On line 18 there is a drive path. Change this to wherever you want to save your document. I created a folder on my C: drive called test. (double click code to copy)

Sub Splitter()
' Based on a Macro created 16-08-98 by Doug Robbins to save each letter created by a
' mailmerge as a separate file.
Dim Mask As String
Dim Letters As Long
Dim Counter As Long
Dim DocName As String
Dim oDoc As Document
Dim oNewDoc As Document
Set oDoc = ActiveDocument
oDoc.Save
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
Mask = "ddMMyy"
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
DocName = "C:\\test" & Format(Date, Mask) _
& " " & LTrim$(Str$(Counter)) & ".doc"
oDoc.Sections.First.Range.Cut
Set oNewDoc = Documents.Add
'Documents are based on the Normal template
'To use an alternative template follow the link.
With Selection
.Paste
.EndKey Unit:=wdStory
.MoveLeft Unit:=wdCharacter, Count:=1
.Delete Unit:=wdCharacter, Count:=1
End With
oNewDoc.SaveAs FileName:=DocName, _
FileFormat:=wdFormatDocument, _
AddToRecentFiles:=False
ActiveWindow.Close
Counter = Counter + 1
Wend
oDoc.Close wdDoNotSaveChanges
End Sub

word-macro4

Once you have pasted that in, simply close the window to return to word.

READ ALSO  How to Fix Error 0x800F0818 When Installing SP 1 (Windows 7)

Step 6

To run the macro now, simply click on View > Macro and hit run. It will then split up the document into three parts.

word-macro5

Step 7

It will run through everything and split them based on your subdocuments. Right now it names them with the date and counter.
If anyone is skilled with VB, it would be cool to pull the sub-document header title, if possible. Please comment if you have any suggestions or changes.

word-macro6


You may also like:

Word Document Mail Merge Only Printing One Page at... If you are banging your head trying to get a mail merge Word Document to print a custom page range, try the solution below first before uninstalling t...