![]() |
|
#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] |
| 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 |