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.
Create a new word document. Go to View > Outline Mode.
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.
Save the document anywhere you like, saving as .docx is fine.
Now we are going to setup the macro. Click on View > Macros. Name the macro whatever you want and click Create.
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)
‘ 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
Letters = Selection.Information(wdActiveEndSectionNumber)
Mask = "ddMMyy"
Counter = 1
While Counter < Letters
DocName = "C:\\test" & Format(Date, Mask) _
& " " & LTrim$(Str$(Counter)) & ".doc"
Set oNewDoc = Documents.Add
‘Documents are based on the Normal template
‘To use an alternative template follow the link.
.MoveLeft Unit:=wdCharacter, Count:=1
.Delete Unit:=wdCharacter, Count:=1
oNewDoc.SaveAs FileName:=DocName, _
Counter = Counter + 1
Once you have pasted that in, simply close the window to return to word.
To run the macro now, simply click on View > Macro and hit run. It will then split up the document into three parts.
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.