Word

Split file into multiple documents using chosen delimiter and filename

Ease of Use

Intermediate

Version tested with

2003 

Submitted by:

lucas

Description:

Allows you to split one document into multiple documents(one for each section between the delimiter). You have control over the delimiter chosen as a text string and the filename. Files are created in the same directory as the original document. 

Discussion:

VBAX Member Norie came up with this code but did not wish to submit it and so with his permission I am submitting it because I think its a very useful script. Often folks wish to split one document into many and this will allow that easily with complete control over the delimiter used and the filenames of the documents created. Original document remains unchanged. Each document is numbered also. It is marked intermediate only because you can change the delimiter and the filenames in the code. All of these changes are made in the sub Test. 

Code:

instructions for use

			

Put this code In a standard module: Option Explicit Sub SplitNotes(delim As String, strFilename As String) Dim doc As Document Dim arrNotes Dim I As Long Dim X As Long Dim Response As Integer arrNotes = Split(ActiveDocument.Range, delim) Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections. Do you wish to proceed?", 4) If Response = 7 Then Exit Sub For I = LBound(arrNotes) To UBound(arrNotes) If Trim(arrNotes(I)) <> "" Then X = X + 1 Set doc = Documents.Add doc.Range = arrNotes(I) doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "000") doc.Close True End If Next I End Sub Sub test() ' delimiter & filename SplitNotes "///", "Notes " End Sub

How to use:

  1. Open the VBE of the document you wish to split(Alt+F11) and add the code above to a new standard module.
  2. Be sure to add your delimiter to the document between each section of text you wish to separate and be sure the delimiter is the same as the one in the sub test.
  3. Change the filename in the sub Test to suit your needs
 

Test the code:

  1. Hit Alt+F8 and select the sub "Test" then select run.
  2. Your files will be created in the same directory as the original file.
  3. They will be named according to the choices you make in the sub Test and numbered.
 

Sample File:

Text Split.zip 9.45KB 

Approved by mdmackillop


This entry has been viewed 185 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express