Method 1:
VBA Code: Split Document Into Multiple Documents According To
Delimiter OR SECTION:
To split a document
into multiple documents according to delimiter you need to follow these steps;
Step 1: Press “Alt-F11” Ã Microsoft
Visual Basic for Application window;
Step 2: Click Insert
tab à Module to copy and paste the following VBA code into
the Module window;
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
OR use this code
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
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
Step 3: Then click Run Press button or F5 to apply the VBA.
Step 4: In the popping out Microsoft Word document, please click the Yes button to go ahead.
Note:
· Be sure to add your delimiter as the same
as “///” in the sub test to the document between each section
of text you wish to separate. Also, you can change “///” to
any delimiters to meet your need.
· You can change the documents “Notes “in
the sub Test to suit your needs.
· And the splitting documents will be saved to
the same place with the original file.
· You do not need to add delimiter to the end of
the original file, if you do, there will be a blank document after splitting.
Method 2:
VBA Code: Split Document Into Multiple Documents By Pages:
To split a document
into multiple documents according by pages you need to follow these steps;
Step 1: Press “Alt-F11” Ã Microsoft
Visual Basic for Application window;
Step 2: Click Insert
tab à Module to copy and paste the following VBA code into
the Module window;
Sub SplitIntoPages()
Dim docMultiple As Document
Dim docSingle As Document
Dim rngPage As Range
Dim iCurrentPage As Integer
Dim iPageCount As Integer
Dim strNewFileName As String
Application.ScreenUpdating = False 'Makes the code run faster
and reduces screen _
flicker a bit.
Set docMultiple = ActiveDocument 'Work
on the active document _
(the one currently containing the Selection)
Set rngPage = docMultiple.Range 'instantiate
the range object
iCurrentPage = 1
'get the document's page count
iPageCount =
docMultiple.Content.ComputeStatistics(wdStatisticPages)
Do Until iCurrentPage
> iPageCount
If iCurrentPage = iPageCount Then
rngPage.End = ActiveDocument.Range.End 'last page (there won't be a
next page)
Else
'Find the beginning of the next page
'Must use the Selection object. The Range.Goto
method will not work on a page
Selection.GoTo wdGoToPage, wdGoToAbsolute,
iCurrentPage + 1
'Set the end of the range to the point between the
pages
rngPage.End = Selection.Start
End If
rngPage.Copy 'copy the page into the Windows
clipboard
Set docSingle = Documents.Add 'create
a new document
docSingle.Range.Paste 'paste the clipboard
contents to the new document
'remove any manual page break to prevent a second
blank
docSingle.Range.Find.Execute
Findtext:="^m", ReplaceWith:=""
'build a new sequentially-numbered file name based
on the original multi-paged file name and path
strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new
single-paged document
iCurrentPage = iCurrentPage + 1 'move to the
next page
docSingle.Close 'close the new document
rngPage.Collapse wdCollapseEnd 'go to the
next page
Loop 'go to the top of the do loop
Application.ScreenUpdating = True 'restore the screen updating
'Destroy the objects.
Set docMultiple = Nothing
Set docSingle = Nothing
Set rngPage = Nothing
End Sub
Step 3: Then click Run Press button or F5 to
apply the VBA.
Note:
The
splitting documents will be saved to the same place with the original file.
So it is too much better that create a New Folder and copy that file into New folder.
0 Comments