Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-03-2025, 09:00 AM
JingleBelle JingleBelle is offline Addition to Split Document Code (by Macropod) Windows 10 Addition to Split Document Code (by Macropod) Office 2016
Novice
Addition to Split Document Code (by Macropod)
 
Join Date: Nov 2020
Posts: 26
JingleBelle is on a distinguished road
Default Addition to Split Document Code (by Macropod)

I was tasked with splitting many documents into separate files. I was relieved to find Macropod's code (from 2015 on this forum) to split documents at a specified heading levels and it has worked perfectly (no surprise there).



I am hoping someone here would help me update the code so that it would give me a count of the new documents created, via a message box, once the sub has completed. This way I would not have to count the number of files and compare them to the original document's table of contents. It is a minor inconvenience, but it has left me curious nonetheless. I spent the better part of this morning googling and, thus far, I've been unable to find anything specific to what I need. Even if I did find something, I'm not sure I would know how to add it without breaking the code.

Code:
Sub SplitDoc2()
''*Sub Name:  SplitDocLev2
'* Author:       Macropod
'* Date:         November 16, 2015
'* Notes:       Splits Docs into Separate Files at Heading Level 2

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 = wdStyleHeading2  'Change level here
      .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
         On Error Resume Next 
        .Close False
      End With
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Set Doc = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Reply With Quote
 

Tags
count new files, split at heading level, split document



Similar Threads
Thread Thread Starter Forum Replies Last Post
Addition to Split Document Code (by Macropod) Adapting Macropod's Acronym Word VBA Code for Excel scienceguy Excel Programming 3 11-20-2021 06:59 AM
Addition to Split Document Code (by Macropod) split word document based on bookmarks with each new document title of the bookmark megatronixs Word VBA 9 09-05-2020 02:29 PM
Addition to Split Document Code (by Macropod) Mail Merge - split merged documents and rename each split document based on text in header FuriousD Word VBA 1 05-12-2019 04:06 AM
Pizza Tips - Simple Addition In Word Document StephenRay Word VBA 8 09-12-2017 07:13 AM
Addition to Split Document Code (by Macropod) How do I see one document map for each half of a split MS WORD 2010 document? quickwin Word 3 07-09-2013 10:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:27 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