#1
|
||||
|
||||
Word VBA - Split Document By Headings - Save File Name As Heading Name
Hi,
I hope everyone's day is going well! I am trying to fix some code unsuccessfully to split documents by headings. I have tested a number of macros for splitting documents, available here and elsewhere, but they fall short on one feature that I need: Saving the file name AS the heading name. You can split by Section or Page as referenced here. There are others that split by delimiter. https://support.microsoft.com/en-us/kb/306348 I found a macro from this forum and have been fiddling about testing it – the code refuses to co-operate. It splits the documents - but you have to give it an initial file name. The problem is that with this approach - I don’t know what’s inside my files, so it creates more problems for me than solves. https://www.msofficeforums.com/word-...documents.html The above macro with initial file name test outputs as : Test1 Test2 Test3 Test4 It also has some other call to functions - I am not sure what for. I am stuck on the below - code from macro above - and not sure - how to fix it Code:
Dim heading As Long If Selection.Style = "Heading 1" Then Set Rng = aDoc.Range(Rng1.Start, Rng2.End) Set bDoc = Documents.Add bDoc.Content.FormattedText = Rng 'bDoc.SaveAs Ans$ & Counter, wdFormatDocument 'bDoc.SaveAs aDoc.path & "\" & Ans$ & Counter, wdFormatDocument ActiveDocument.SaveAs FilePath & "\" & FileName(heading) & ".docx" bDoc.Close I am trying to split a large document by its headings · Split Files by Heading 1 · Save each split file with the heading name I would be very grateful for some guidance, as always thanking you for the time and expertise shared is greatly appreciated. Thank you so much in advance for your time. J |
#2
|
||||
|
||||
If these headings are preceded by Section breaks, you could use the 'Split Merged Output to Separate Documents' macro in the Mailmerge Tips and Tricks Sticky thread at the top of the Mailmerge forum:
https://www.msofficeforums.com/mail-...ps-tricks.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
Hi Paul,
thanks for your response. I will to need some time to read it all and digest it! I ran the SplitMergedDocument() it gave this error 4605 no text selected Set Rng = .Range With Rng If j > 1 Then .MoveEnd wdSection, j - 1 'Contract the range to exclude the Section break .MoveEnd wdCharacter, -1 ' Copy the range .Copy The other solution I was thinking was maybe to re save the files with the first paragraph in the document. That would work - what ever gets the job done really. So I could Insert section breaks before the headings - Use macro to split by section Rename the Files with the first paragraph? is that an easier solution thank you j |
#4
|
||||
|
||||
Quote:
Quote:
Code:
Sub SplitDocByHeading1() Application.ScreenUpdating = False Dim StrTmplt As String, StrPath As String, StrFlNm As String, Rng As Range, i As Long, Doc As Document Const StrNoChr As String = """*./\:?|" With ActiveDocument StrTmplt = .AttachedTemplate.FullName StrPath = .Path & "\" With .Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Style = wdStyleHeading1 .Format = True .Forward = True .Wrap = wdFindStop .Execute End With Do While .Find.Found Set Rng = .Duplicate StrFlNm = Split(Rng.Paragraphs(1).Range.Text, vbCr)(0) For i = 1 To Len(StrNoChr) StrFlNm = Replace(StrFlNm, Mid(StrNoChr, i, 1), "_") Next StrFlNm = StrFlNm & ".docx" Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel") Set Doc = Documents.Add(Template:=StrTmplt, Visible:=False) With Doc .Range.FormattedText = Rng.FormattedText .SaveAs2 FileName:=StrPath & StrFlNm, Fileformat:=wdFormatXMLDocument, AddToRecentFiles:=False .Close False End With .Collapse wdCollapseEnd .Find.Execute Loop End With End With Set Doc = Nothing: Set Rng = Nothing Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
Hi Paul,
what kind of magic is this - It saves the files with the heading names - so thats yipeee great! |
#6
|
||||
|
||||
Hi Paul,
I've been struggling with the Splitting documents VBA macro for a while - testing and debugging code is really time consuming and can be really frustrating, especially when sometimes the error just won't go away, typical example vis a vis 5152. Splitting files by copying and pasting them into new docs is something I do all the time. Today I had a big file - and that really made me struggle for an hour with the mouse, highlighting , dragging to get the right text, to then copy and paste into 20 documents. Yikes! So thank you very much for this Macro - its a diamond! I have no doubt many people will benefit from this macro its a common task to split documents up - sometimes the idea seems simple enough - but coding wise it can be a lot of code to deal with and having the wisdom to know what goes where and when, when confronted by stubborn errors does make it stressful. Thanking you again for your generous help, and sharing your time and expertise and coding skills to help others. I am really happy - as I have so many documents waiting to be split, I can go and finish them all. Not too fussed about the 5152, I can live with that, until Microsoft fixes it with some update or so. Please mark as solved! Have an super awesome week! J Also you saved me the hassle of inserting sections in each document before splitting - one less task for me to do - as they say every little helps! Thank you J |
#7
|
||||
|
||||
You could try inserting:
On Error Resume Next before: .SaveAs2 That should result in the 5152 errors being skipped over.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Inserts blank page at end
The code in post #4 inserts a blank page at the end of each of my documents. Is there a way to prevent that?
EDIT: I see what the issue is. My document ended each section with an odd section break, so it inserted a blank page. |
Tags |
document, heading, split |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
split word document based on bookmarks with each new document title of the bookmark | megatronixs | Word VBA | 9 | 09-05-2020 02:29 PM |
How to Restore Heading 1, Heading 2, etc. within a Word Document | cheech1981 | Word | 9 | 01-11-2017 02:14 AM |
How can I save a Word Document as a PDF file with a merged field filename? | kp2009 | Word VBA | 5 | 08-27-2015 11:45 PM |
Split multi-page mail merge document, then name file from letter info. | BriMan83 | Mail Merge | 1 | 04-24-2013 11:35 PM |
Macro to create new word doc and save the file using String found in the document | VBNation | Word VBA | 2 | 02-08-2013 07:14 AM |