Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-18-2023, 01:20 PM
learnerlamp learnerlamp is offline Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies") Windows 10 Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies") Office 2021
Novice
Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies")
 
Join Date: Oct 2023
Posts: 1
learnerlamp is on a distinguished road
Default Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies")

Hi there! My team and I are looking to edit the Macro from this thread:



https://www.msofficeforums.com/word-...save-file.html

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
The original Macro is intended to split a Word doc by Heading 1s into multiple documents, with the the file name of each split matching each heading title.

This is great for our organization! However, there are times where we want to separate out the bibliography (also a Heading 1) and times where we want to keep it in. We would like to keep two versions of the macro for this reason -- the above and a modified version that keeps in the bibliography.

Potential Theoretical Solutions:
  • Add VBA code that says if the heading string starts with the text "Bibliography", then the code should not be executed
  • Add VBA code that converts the headings that start "Bibliography" from heading level 1 to 2


Would anyone be able to advise or help me with this task? I am not familiar enough with VBA to know exactly how to parse this out.


Thank you!
Reply With Quote
Reply

Tags
heading split, macro help, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2003: VBA "Function" causes "#VALUE!" errors after running "insert/delete row" custom macro Matt C Excel Programming 2 01-08-2022 06:03 AM
Keyboard shortcut to "Show all headings" with the Heading 1 style etc. Retko Word 1 12-29-2020 05:41 AM
Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies") Word 2013's Navigation Pane does not show "level 4" headings, even when I click "Show Heading 4" zrcarlson Word 1 10-26-2016 09:51 AM
Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies") Navigation Pane: Preselect "results" instead of "headings" DirkMcCallahan Word 1 03-24-2016 01:38 PM
Modifying a Macro that Splits Word Docs by Heading to Skip Some Headings ("Bibliographies") How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM

Other Forums: Access Forums

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