Custom Word document splitter! VBA

arvex

New Member
Joined
May 10, 2011
Messages
18
Hello experts!

I have code by Jacob Hilderbrand:

Code:
Option Explicit 
 
Sub AllSectionsToSubDoc() 
     
    Dim x               As Long 
    Dim Sections        As Long 
    Dim Doc             As Document 
     
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 
     
    Set Doc = ActiveDocument 
    Sections = Doc.Sections.Count 
    For x = Sections - 1 To 1 Step -1 
        Doc.Sections(x).Range.Copy 
        Documents.Add 
        ActiveDocument.Range.Paste 
        ActiveDocument.SaveAs (Doc.Path & "\" & x & ".doc") 
        ActiveDocument.Close False 
    Next x 
     
    Application.ScreenUpdating = True 
    Application.DisplayAlerts = True 
     
End Sub

All i want is to work code custom like:

Split document when 7th paragraph changes!

Document is like (might be different):

PAGE1
Text
Text
Text
Text
Text
Text
Text1
Text
Text

PAGE2
Text
Text
Text
Text
Text
Text
Text1
Text
Text

PAGE3
Text
Text
Text
Text
Text
Text
Text2
Text
Text

PAGE4
Text
Text
Text
Text
Text
Text
Text3
Text
Text

PAGE5
Text
Text
Text
Text
Text
Text
Text3
Text
Text

ETC

As a result from given example I want to see is 3 Word documents:
  • 1st Word document all pages when Text1 is in paragraph 7;
  • 2nd Word document all pages when Text2 is in paragraph 7;
  • 3rd Word document all pages when Text3 is in paragraph 7;
  • ETC...

And save each document by trimmed text from 1st page 7th and 3rd paragraph of new document (of whole document each page's 3rd paragraph is different)
Save for 1st document with Text1 would be: It's "Paragraph7_Paragraph3.docx"
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
How are these documents that you want to split being produced?
 
Last edited:
Upvote 0
They would be produced from the product of mail merge. Mail merge result document's each page break is with section breaks (next page).
PS. Graham Mayor's add-on can't use.
 
Upvote 0
Unless you're using a Directory merge, there would necessarily be a Section break between the records and even a Directory merge can be configured to insert them.
Regardless, it would be better to split the records when the merge is executed, rather than trying to do it later. To that end, see Send Mailmerge Output to Individual Files in the Mailmerge Tips and Tricks thread at:
Mailmerge Tips & Tricks
or:
Word Mailmerge Tips & Tricks | Windows Secrets Lounge
 
Upvote 0
I found Your code Sub Merge_To_Individual_Files() pritty useful, but is it possible to run it as my given example. In this case to combine same datafield records in one word document. Also problem is with record count, need it to modify to be like from which record I want to start split to individual files (Its like doing mail merge individual files from/to).

Many thanks for tip.
 
Upvote 0
The macro in the link will run with any mailmerge. All you need do is identify the data fields that are to be used for the naming and, perhaps, the save folder.

As for starting from a particular record, that can be done by changing the '1' in:
For i = 1 To .MailMerge.DataSource.RecordCount
to whatever starting record # you want to use.
 
Upvote 0
Many thanks, sorted this one out. Now I need (if possible) to have combined Word documents by one data field. Like when Datafields("Last_Name") changes then make new document and again when changes then make another one, etc... It's possible with Directory merge?
 
Last edited:
Upvote 0
Yes, it's possible with a directory merge, though, depending on precisely what you're trying to achieve, a letter merge using a DATABASE field might be simpler. That would especially be the case if the grouped data are to be output as a table. You'll need to provide more details.
 
Upvote 0
Haven't managet yet to split documents with same Datafield. I want to have records with same datafield to be in one word document. Your macro makes each record, to new word document.
 
Upvote 0
As I said, it's possible to do so with a Directory merge. I didn't say you could do it with a macro designed for a Letter merge - which is what you're using. I also said that, depending on what you're trying to output, you might be able to get the desired result with a Letter merge using a DATABASE field. I finished by saying:
You'll need to provide more details.
You have yet to do that.
 
Upvote 0

Forum statistics

Threads
1,225,762
Messages
6,186,895
Members
453,384
Latest member
BigShanny

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top