Skip to content

Instantly share code, notes, and snippets.

@johnpaulhayes
Created May 8, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnpaulhayes/a9c81a70860d7006757a to your computer and use it in GitHub Desktop.
Save johnpaulhayes/a9c81a70860d7006757a to your computer and use it in GitHub Desktop.
Macro for splitting a mail-merge document into single files.
Sub Splitter()
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:\\single_" & Format(Date, Mask) _
& " " & LTrim$(Str$(Counter)) & ".docx"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment