Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-16-2015, 01:40 PM
jc491's Avatar
jc491 jc491 is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 7 64bit Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2013
VBA Novice
Word VBA  - Split Document By Headings - Save File Name As Heading Name
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default 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
To Recap:

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
Reply With Quote
  #2  
Old 11-16-2015, 02:40 PM
macropod's Avatar
macropod macropod is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 7 64bit Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 11-16-2015, 03:18 PM
jc491's Avatar
jc491 jc491 is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 10 Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2016
VBA Novice
Word VBA  - Split Document By Headings - Save File Name As Heading Name
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 11-16-2015, 03:48 PM
macropod's Avatar
macropod macropod is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 7 64bit Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by jc491 View Post
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
That suggests you're trying to run the code against an empty document.
Quote:
Originally Posted by jc491 View Post
So I could
Insert section breaks before the headings -
Use macro to split by section
Rename the Files with the first paragraph?
The code I posted assumes the Section breaks already exist. While you could insert the Section breaks or the code could be modified to do that itself, perhaps a better way is:
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]
Reply With Quote
  #5  
Old 11-16-2015, 04:09 PM
jc491's Avatar
jc491 jc491 is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 10 Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2016
VBA Novice
Word VBA  - Split Document By Headings - Save File Name As Heading Name
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

Hi Paul,

what kind of magic is this -

It saves the files with the heading names - so thats yipeee great!
Reply With Quote
  #6  
Old 11-16-2015, 08:29 PM
jc491's Avatar
jc491 jc491 is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 10 Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2016
VBA Novice
Word VBA  - Split Document By Headings - Save File Name As Heading Name
 
Join Date: Sep 2015
Location: UK
Posts: 55
jc491 is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 11-16-2015, 08:52 PM
macropod's Avatar
macropod macropod is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 7 64bit Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 01-21-2022, 11:04 AM
Venteux Venteux is offline Word VBA  - Split Document By Headings - Save File Name As Heading Name Windows 10 Word VBA  - Split Document By Headings - Save File Name As Heading Name Office 2019
Novice
 
Join Date: May 2021
Posts: 22
Venteux is on a distinguished road
Default 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.
Reply With Quote
Reply

Tags
document, heading, split

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word VBA  - Split Document By Headings - Save File Name As Heading Name split word document based on bookmarks with each new document title of the bookmark megatronixs Word VBA 9 09-05-2020 02:29 PM
Word VBA  - Split Document By Headings - Save File Name As Heading Name How to Restore Heading 1, Heading 2, etc. within a Word Document cheech1981 Word 9 01-11-2017 02:14 AM
Word VBA  - Split Document By Headings - Save File Name As Heading Name 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
Word VBA  - Split Document By Headings - Save File Name As Heading Name Split multi-page mail merge document, then name file from letter info. BriMan83 Mail Merge 1 04-24-2013 11:35 PM
Word VBA  - Split Document By Headings - Save File Name As Heading Name 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft