Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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: 22,521
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
 

Tags
document, heading, split



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:13 AM.


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