Created
May 8, 2015 11:39
-
-
Save johnpaulhayes/a9c81a70860d7006757a to your computer and use it in GitHub Desktop.
Macro for splitting a mail-merge document into single files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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