View Single Post
 
Old 10-18-2023, 01:20 PM
learnerlamp learnerlamp is offline Windows 10 Office 2021
Novice
 
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