Separating Sections into separate word document

I have a large word document, which happens to be a song book, that has multiple songs on one page. I am wanting to seperate each song into it owns document and use the name of the song as the document file name.

I have gone in and started doing this by copying and pasting each song into its own seperate document, but this takes forever and I was wanting to automate this process by using VBA code.

Could someone provide me with some guideance?

Thanks!
Answer
Answer
While your document starts out with Heading 1 style being used for the titles, it does not do that all the way through and there a various other issues that need to be fixed before a macro can be used to create individual files.

 

After a bit of manual editing to apply the Heading 1 style to all of the song titles and removing multiple consecutive instances of that style in a number of places and using the replace facility and number of times to remove other irregularities and then finally using it to replace Heading 1 style paragraphs by  ^m^& in the replace control to get the document to the point where each song started on a new page , I then used the following macro to create separate files for each song, named with the title of the song

 

Dim Counter As Long, Source As Document, Target As Document

Set Source = ActiveDocument

Dim Pages As Long

Dim rngDocName As Range

Dim strDocName As String, strFolder As String

Dim fd As FileDialog

Set fd = Application.FileDialog(msoFileDialogFolderPicker)

With fd

    .Title = "Select the folder into which the documents will be saved."

    If .Show = -1 Then

        strFolder = .SelectedItems(1) & "\"

    Else

        MsgBox "The documents will be saved in the default document file location."

        strFolder = ""

    End If

End With

Selection.HomeKey Unit:=wdStory

Pages = Source.BuiltInDocumentProperties(wdPropertyPages)

While Counter < Pages

    Counter = Counter + 1

    Source.Bookmarks("\Page").Range.Cut

    Set Target = Documents.Add

    Target.Range.Paste

    Set rngDocName = Target.Range.Paragraphs(1).Range

    rngDocName.End = rngDocName.End - 1

    strDocName = Replace(rngDocName.Text, ",", "")

    strDocName = Replace(strDocName, "'", "")

    strDocName = Replace(strDocName, ":", "")

    Target.SaveAs FileName:=strFolder & strDocName

    Target.Close

Wend

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated September 13, 2022 Views 674 Applies to: