VBA to Split a large Word Documents into seperate files

dawnellis
NewLounger
Posts: 6
Joined: 14 Aug 2015, 13:42

VBA to Split a large Word Documents into seperate files

Post by dawnellis »

Hi there:

I have a very large word document with Section breaks that seperate topics in the document. I have found VBA that spits the document by pages but not by section....some of the new files should be 1, 2 or 3 pages in length - all depending on where the Section Break (Next Page) is.

I have spent hours on this because I am not a VBA expert!

Anyone able to help?

Dawn

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

Welcome to Eileen's Lounge!

Here is a variation on a macro published by Microsoft a long time ago:

Code: Select all

Sub BreakOnSection()
    Dim i As Long
    Dim DocNum As Long
    Dim docOld As Document
    Dim docNew As Document

    Set docOld = ActiveDocument

    ' Used to set criteria for moving through the document by section.
    Application.Browser.Target = wdBrowseSection

    'A mail merge document ends with a section break next page.
    'Subtracting one from the section count stop error message.
    For i = 1 To docOld.Sections.Count
        'Select and copy the section text to the clipboard.
        docOld.Bookmarks("\Section").Range.Copy

        'Create a new document to paste text from clipboard.
        Set docNew = Documents.Add
        Selection.Paste

        ' Removes the break that is copied at the end of the section, if any.
        Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        Selection.Delete Unit:=wdCharacter, Count:=1
        DocNum = DocNum + 1
        docNew.SaveAs FileName:="Section_" & DocNum & ".docx", _
            FileFormat:=wdFormatXMLDocument
        docNew.Close
        'Move the selection to the next section in the document.
        Application.Browser.Next
    Next i

    docOld.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Best wishes,
Hans

dawnellis
NewLounger
Posts: 6
Joined: 14 Aug 2015, 13:42

Re: VBA to Split a large Word Documents into seperate files

Post by dawnellis »

Hi Hans:

That is just so close...and very similar to another I was using - but it breaks the document by page and not by the Section Break. Honestly, I have no hair left on my head. I am not like a hairless dog with a bone.....I could have copy and pasted each of the pages into new documents - the amount of time I have spent on this! :)

I just doin't know VBA well enough to figure out how to do it!

Any other ideas? Extra hair?

Thanks in advance.

Dawn

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

I have tested the code. It does split the document by section breaks, not by page breaks. Perhaps your document has a section break after each page?

Anyway, here is a slightly different version of the macro:

Code: Select all

Sub BreakOnSection()
    Dim i As Long
    Dim DocNum As Long
    Dim docOld As Document
    Dim docNew As Document

    Set docOld = ActiveDocument

    'A mail merge document ends with a section break next page.
    'Subtracting one from the section count stop error message.
    For i = 1 To docOld.Sections.Count
        'Select and copy the section text to the clipboard.
        docOld.Sections(i).Range.Copy

        'Create a new document to paste text from clipboard.
        Set docNew = Documents.Add
        Selection.Paste

        ' Removes the break that is copied at the end of the section, if any.
        Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
        Selection.Delete Unit:=wdCharacter, Count:=1
        DocNum = DocNum + 1
        docNew.SaveAs FileName:="Section_" & DocNum & ".docx", _
            FileFormat:=wdFormatXMLDocument
        docNew.Close
        'Move the selection to the next section in the document.
        Application.Browser.Next
    Next i

    docOld.Close SaveChanges:=wdDoNotSaveChanges
End Sub
Does that make a difference?
Best wishes,
Hans

dawnellis
NewLounger
Posts: 6
Joined: 14 Aug 2015, 13:42

Re: VBA to Split a large Word Documents into seperate files

Post by dawnellis »

Hi Hans: I got the same result...can I send you a small copy of the document? I have been practicing with a small version of the original.....

Please let me know and I can't thank you enough for your time!
Dawn

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

You can attach documents up to 250 KB to a reply - see Tip: adding an attachment using the prosilver skin.

If the document is too large, you can make it available through one of the websites that let you upload and share a file, such as Microsoft OneDrive (https://onedrive.live.com" onclick="window.open(this.href);return false;), FileDropper (http://filedropper.com" onclick="window.open(this.href);return false;) or DropBox (http://www.dropbox.com" onclick="window.open(this.href);return false;). Post a link to the uploaded and shared file in a reply here.

Please remove sensitive/proprietary information from the copy that you attach/upload.
Best wishes,
Hans

dawnellis
NewLounger
Posts: 6
Joined: 14 Aug 2015, 13:42

Re: VBA to Split a large Word Documents into seperate files

Post by dawnellis »

Hi Hans:

Here is a shortened copy of the document I am trying to split!

Thanks agina.

Dawn
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

Thank you. There is a section break at the end of each page of the document. For example, here is a screenshot of the section break at the end of page 1, shown in Draft View.
S0599.png
As a result, the macro creates a separate document for each page.

I see two alternatives, neither of which might be easy to implement or even feasible.

1) Change the superfluous section breaks, such as the one at the end of page 1, to page breaks. The macro that I posted will then work as intended.
2) Leave the section breaks as they are, and come up with a different way of specifyingwhere you want to break the document. The macro would have to be modified.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

dawnellis
NewLounger
Posts: 6
Joined: 14 Aug 2015, 13:42

Re: VBA to Split a large Word Documents into seperate files

Post by dawnellis »

That is so strange Hans....Here is what I see! Attached a screen shot for you!

I am at a loss as to how to do it...

Do you think if I added in the VBA for it to look for limited revision - go to the end of the line and seperate the documents there? I think I have code to do that somewhere!

Dawn
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

You have to switch to Draft View (on the View tab of the ribbon) to see all section breaks.

Here is a macro that splits the document at "Limited revision":

Code: Select all

Sub BreakOnRevision()
    Dim lngStart As Long
    Dim lngEnd As Long
    Dim lngDocNum As Long
    Dim docOld As Document
    Dim docNew As Document

    Set docOld = ActiveDocument
    lngStart = 1
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = "Limited revision*^13"
        .MatchWildcards = True
        .Wrap = wdFindStop
        Do While .Execute
            Selection.Collapse Direction:=wdCollapseEnd
            lngEnd = Selection.End
            ' Copy the "section"
            docOld.Range(lngStart, lngEnd).Copy
            'Create a new document to paste text from clipboard.
            Set docNew = Documents.Add
            Selection.Paste
            ' Save the new document
            lngDocNum = lngDocNum + 1
            docNew.SaveAs FileName:="Section_" & lngDocNum & ".docx", _
                FileFormat:=wdFormatXMLDocument
            docNew.Close
            ' set new start
            lngStart = lngEnd + 1
        Loop
    End With
End Sub
Best wishes,
Hans

dawnellis
NewLounger
Posts: 6
Joined: 14 Aug 2015, 13:42

Re: VBA to Split a large Word Documents into seperate files

Post by dawnellis »

Hans...I can't even begin to tell you how much I appreciate your help! I learned soooo much today! I combined the changed VBA that you sent me with a few others and now we can run the VBA and it splits the file, saves it by the drug name as both docx and pdf! I am a hero to our pharmacy staff and I have your name ALL over my cape!

Thank you soooo much! I pasted the VBA below for you to take a look at! I am going to be a programmer in my next life! So much FUN!

Dawn

Sub BreakOnRevision()
' Hans from EileensLounge provided the modified code
Dim lngStart As Long
Dim lngEnd As Long
Dim lngDocNum As Long
Dim docOld As Document
Dim docNew As Document

Set docOld = ActiveDocument
lngStart = 1
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "Limited revision*^13"
.MatchWildcards = True
.Wrap = wdFindStop
Do While .Execute
Selection.Collapse Direction:=wdCollapseEnd
lngEnd = Selection.End
' Copy the "section"
docOld.Range(lngStart, lngEnd).Copy
'Create a new document to paste text from clipboard.
Set docNew = Documents.Add
Selection.Paste
' Save the new document

ChangeFileOpenDirectory "C:\Sandie\PDF"
Dim lastParagraphText As String
lastParagraphText = ActiveDocument.Paragraphs(2).Range.Text
lastParagraphText = Left$(lastParagraphText, Len(lastParagraphText) - 1)

docNew.SaveAs FileName:="" & lastParagraphText & ".docx", _
FileFormat:=wdFormatXMLDocument


' Save as PDF
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
Replace(ActiveDocument.FullName, ".docx", ".pdf"), _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
docNew.Close
' set new start

lngStart = lngEnd + 1
Loop
End With
End Sub

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

Good for you! Glad to have been able to help!
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: VBA to Split a large Word Documents into seperate files

Post by macropod »

See also 'Split Merged Output to Separate Documents' in the Mailmerge Tips and Tricks thread at:
https://www.msofficeforums.com/mail-mer ... ricks.html
Last edited by macropod on 29 Oct 2022, 14:15, edited 2 times in total.
Paul Edstein
[Fmr MS MVP - Word]

Priyantha
StarLounger
Posts: 86
Joined: 10 Oct 2022, 02:52

Re: VBA to Split a large Word Documents into seperate files

Post by Priyantha »

Dear All,

There is a file in a Folder (Eg. "C/Test/Split/Test.doc") consisting of several pages (Eg. Merge with 7 Pages) and save the pages as separate files in a folder (Eg. "C/Test/Split/ *.doc"). I tried the above code to split my *.doc file (Attached herewith). But the pages did not separate. please help me to solve this issue.

Thanks,


Best Regarding,

Priyantha
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

Your Test.doc document has 4 sections. Since the first section break is at the top of the document, the first section is empty.
When I run the code from your text document, it produces 4 documents: one for each section. The first of those documents is empty, as was to be expected.

S1931.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

Priyantha
StarLounger
Posts: 86
Joined: 10 Oct 2022, 02:52

Re: VBA to Split a large Word Documents into seperate files

Post by Priyantha »

Dear Hans,

Looking forward to your further help.

01). My file (Test.doc) has 03 pages and cannot be separated as 03 doc files in a Folder (eg Page 01.doc, Page 02.doc, Page 03. doc). I run this code but Nothing happened What changes should be made? (I am not an expert in Excel VBA)

02). If this Macro is not run from Excel, then it is more useful for me.

Please help me.

Thanks,
Regarding,

Priyantha
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

01) The code works, but you must make sure that Test.doc is the active document when you run the macro.
02) What do you mean by "If this Macro is not run from Excel, then it is more useful me"?
Best wishes,
Hans

Priyantha
StarLounger
Posts: 86
Joined: 10 Oct 2022, 02:52

Re: VBA to Split a large Word Documents into seperate files

Post by Priyantha »

Dear Hans,

01). yes, it is running. But, My original is in " C:\Users\User\Desktop\New\Test.doc" and separate files (Section01.doc, Section02.doc, section 03. doc)
save in "C:\Users\User\Documents". but I want to save them in the original file location (Eg. "C:\Users\User\Desktop\New\")

02. The logo in the original file is moved to another location, the "ETF 03% EMPLOYER CONTRIBUTION 3,555.90" section at the end of the page is
changed and every new file is created as 2 pages. (Attached here).

03. I want to run this macro from an excel sheet so, Can't change your code as below.

Your Code: Set docOld = ActiveDocument
Suggestion Code: Set docOld = " C:\Users\User\Desktop\New\Test.doc"

Please help me.

Thanks,

Regarding,
Priyantha.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA to Split a large Word Documents into seperate files

Post by HansV »

01) You TWICE wrote that the code doesn't do anything, now you write that it does.
02) The document has a weird structure. Was it originally a PDF file?
03) You wrote "If this Macro is NOT run from Excel, then it is more useful me" and now you want to run it from Excel.

I cannot get it to run the way you want. Perhaps someone else knows how to do that.
Best wishes,
Hans

Priyantha
StarLounger
Posts: 86
Joined: 10 Oct 2022, 02:52

Re: VBA to Split a large Word Documents into seperate files

Post by Priyantha »

Dear Hans,

I apologize if you have been inconvenienced in any way.

1) When running the macro, I hoped that the relevant files would be created in the folder where the original file was (C:\Users\User\Desktop\New\Test.doc) but when I checked, it was already created in ",C:\Users\User\Documents"
2). I got a file like this. I think this doc file.

3). I want to run this macro by clicking a button from ecxel.

Thanks


Regarding
Priyantha