• Macro to split large doc by Heading 2 (Word 2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Macro to split large doc by Heading 2 (Word 2003)

    Author
    Topic
    #441996

    I have a large document that I would really like to break into individual documents. 20 to be exact. It may seem silly but I want it to break into individual documents from Heading Level 2. The main document has one big long TOC. A wish come true would be for the document to be broken down and for each new doc to have its own TOC.

    Is this possible?

    Kerry

    Viewing 1 reply thread
    Author
    Replies
    • #1062783

      Does your document also have Heading 1 paragraphs? How should those be treated?

      • #1062787

        Yes there is a heading 1. This is the document title. I would like to disregard this Heading leve.

        Kerry

        • #1062789

          Here is a relatively simple way to split your document:
          – Make a backup copy of the document.
          – Switch to Outline view.
          – On the Outlining toolbar, select Show Level 2 from the Show Level dropdown.
          – Select all Header 2 paragraphs.
          – Click ‘Create Subdocument’ on the Outlining toolbar.
          – Save your document. Word will automatically create a separate document for each Heading 2.

          • #1062790

            My document has 20 chapters with which all start with Level 2 headings. (195 pages) I tried your suggestion and it takes a long time to select each Level 2 heading and it just names the file 1, 2 3 etc. and the docs it creates just have the heading and no text.

            I was hoping for something that would be very quick and name the file the same name as the Heading as I will be running this quite a lot. The main document has one large TOC at the beginning. Ideally the split docs would have their own TOC.

            • #1062792

              > as I will be running this quite a lot

              Wouldn’t it be easier to set up the documents correctly from the start, instead of creating a problem and then trying to find a way out of it?

            • #1062795

              I agree with your thoughts. Perhaps I need to give you a bit of background.

              I originally developed this series of documents at you suggest. Individual files for each chapter and Tables of contents etc. It was all linked by one file which linked it all together. It ends up being 13mb in total size. This makes a manual. I have never been able to post it to our Intranet due to the size and the clumsy way it would be accessed.

              After a lot of searching I found a product that converts all documents into XML format with a TOC. It ends up being very nice for the intranet. The product is Republico – XDK. It also creates one document containing all the chapters with one big TOC at the beginning. I want to be able to allow the user to not only be able to view the content online, but to be able to print the particular chapter should they wish. I dont want them to print the whole 195 pages at the click of the button.

              So what I want to do is split the document and put a button on each Heading 2 Frame to print the document. This is where I need the topic name to be the file name. If it always keeps the same name the button reference will always be correct.

              Does this info help give you an idea of my issue?

            • #1062797

              I would have published each chapter as a separate intranet document…

              I’ll be offline for several hours; if nobody else comes up with an idea in the meantime, I’ll look at it when I come back.

            • #1062798

              That wasnt possible as there are links between chapters and our bandwidth didnt allow such big documents. Trust me I had my reasons to go to this program.

            • #1062828

              Cross references between the chapters change the situation. I’d dump the idea of Word documents altogether and switch to web pages (HTML).

            • #1062869

              Just trust me Hans, I do have a need to split a large document. Can you help me with this please?

            • #1062876

              The attached document contains a macro that will create a new document for each Heading 2 paragraph. Everything before the first Heading 2 paragraph will be copied into each new document; this should include the title and the TOC. The name of the documents will be taken from the Heading 2 paragraphs.
              I have provided the code with a lot of comments. It could be made a bit more elegant, but let’s see whether it does what you want first. You can test the macro on the document itself.

            • #1062881

              Hans it is wonderful! Thankyou.

              I am having just one minor problem. My styles seem to be changing when it I do the split. Not sure why.

            • #1062891

              Try the following:
              – If your document is based on a custom template that is empty (i.e. it defines styles etc. but it has no contents), use that in the following.
              – Otherwise, create a copy of your manual, clear all contents, save the copy and use that in the following.
              – Change both occurrences of the line

              Set docNew = Documents.Add

              to

              Set docNew = Documents.Add(Template:=”…”)

              where you must provide the path and filename of the template or document mentioned above in place of the …

            • #1062893

              I have been fiddling all afternoon and was just about to send you this sample to show you what is happening. Can you take a look at it before I attempt your other solution?

              Thanks

            • #1062895

              You *should* try the solution I suggested. The problem is that if you simply use

              Set docNew = Documents.Add

              Word creates a new document based on Normal.dot. While copying text from the manual into the new document, Word sees that the Normal style is already in use in the new document, and uses that instead of the Nornal style from the manual. That is default behavior. So you must create a new document bases on a document or template with the same styles as the manual.

            • #1062944

              Edited by HansV to remove white space from attached image

              Hans I am sorry to bug you again. I successfully run this macro today at work. Now I have brought it home and run it on both my laptop and desktop and keep getting this error message.

              I made a template from the actual document and then referenced the file name and path in the macro “Set docNew = Documents.Add(Template:=”F:split template.dot”)”

              What am I doing wrong?

            • #1062951

              I’m wondering whether the box character at the beginning of the file name causes the problem. What is the actual text of the Heading 2 paragraph this comes from? Do you have paragraph numbering for this paragraph?

            • #1063392

              Sorry to take so long to get back. Yes you were right. I have a fiddle with the paragraph in question and it worked.

              Thanks Hans

            • #1081066

              Hi Hans

              I have come back to the original post hoping that you can recall what we did here. I am finding it hard to explain what I am trying to achieve so please bare with me here.

              I have been using this macro as you suggested, but the process is quite cumbersome. I have to keep the .dot template on a flash drive and refer to it in my large document. I drag this module into my large document and run it. The Drive letter can vary from machine to Machine, so I have to edit the line Set docNew = Documents.Add(Template:=”F:split template.dot”) each time I run it. I need to teach someone else how to do this and I have to admit I have to think hard to remeber what to do everytime and it is a bit hit and miss.

              The split chapters would be using the same template style as the document it is split from. Is there a way to make the split chapters simply be formatted the same? There by eliminating some of the manual handling of the macro.

              Option Explicit

              Sub SplitLevel2()
              Dim docCur As Document
              Dim docNew As Document
              Dim rngTitle As Range
              Dim rngChapter As Range
              Dim rngTarget As Range
              Dim lngStart As Long
              Dim lngEnd As Long
              Dim lngCnt As Long
              Dim strChapter As String

              On Error GoTo ErrHandler

              Application.ScreenUpdating = False

              ‘ Source document
              Set docCur = ActiveDocument
              ‘ Set up to find Header 2
              With docCur.Content.Find
              .Text = “”
              .ClearFormatting
              .Style = wdStyleHeading2
              .Format = True
              ‘ Find each occurrence
              Do While .Execute
              ‘ Start and end of range
              lngStart = lngEnd
              lngEnd = .Parent.Start
              ‘ Are we at the beginning?
              If lngCnt = 0 Then
              ‘ If so, define range with title and TOC
              Set rngTitle = docCur.Range(Start:=lngStart, End:=lngEnd)
              Else
              ‘ Else, define chapter range
              Set rngChapter = docCur.Range(Start:=lngStart, End:=lngEnd)
              ‘ Create new document
              ‘Set docNew = Documents.Add
              Set docNew = Documents.Add(Template:=”F:split template.dot”)

              ‘ Copy and paste title/TOC range to new doc
              rngTitle.Copy
              docNew.Content.Paste
              ‘ Copy and paste chapter range at end of new doc
              rngChapter.Copy
              Set rngTarget = docNew.Content
              rngTarget.Collapse Direction:=wdCollapseEnd
              rngTarget.Paste
              ‘ Update TOC
              docNew.TablesOfContents(1).Update
              ‘ Save new doc
              docNew.SaveAs strChapter
              ‘ And close it
              docNew.Close
              End If
              ‘ Set up name for document in next round
              strChapter = .Parent.Text
              strChapter = Left(strChapter, Len(strChapter) – 1)
              ‘ Increase counter
              lngCnt = lngCnt + 1
              Loop
              ‘ Handle last chapter separately
              Set rngChapter = docCur.Range(Start:=lngEnd, End:=docCur.Content.End)
              ‘ Create new document
              ‘Set docNew = Documents.Add
              Set docNew = Documents.Add(Template:=”F:split template.dot”)
              ‘ Copy and paste title/TOC range to new doc
              rngTitle.Copy
              docNew.Content.Paste
              ‘ Copy and paste chapter range at end of new doc
              rngChapter.Copy
              Set rngTarget = docNew.Content
              rngTarget.Collapse Direction:=wdCollapseEnd
              rngTarget.Paste
              ‘ Update TOC
              docNew.TablesOfContents(1).Update
              ‘ Save new doc
              docNew.SaveAs strChapter
              ‘ And close it
              docNew.Close
              End With

              ExitHandler:
              Application.ScreenUpdating = True
              Exit Sub

              ErrHandler:
              MsgBox Err.Description, vbExclamation
              Resume ExitHandler
              End Sub

            • #1081076

              I have attached a zipped template Split.dot. Make this template available to whomever has to perform the splitting.
              To start the process, double click the template from Windows Explorer, or create a new document based on the template from within Word.
              The File Open dialog will automatically be displayed. Select the document to be split, then click Open.
              You should get a message that the split documents have been created.
              Note: the large document will not be open at the end of the process.

            • #1081095

              You understood my babble!! Thanks Hans. This is SO much better! It even puts the newly created documents in the same folder as the original large file. You are brilliant! I cant thank you enough!

              Kerry

            • #1081099

              Just one little thing….. the split documents have a header that says “test”. How can I change this?

            • #1081117

              That’s because I created the template from your sample document by deleting all text. The sample document had TEST in the page header for page 2 and higher.
              To remove the header:
              – Right-click Split.dot and select Open from the popup menu.
              – The template will be opened in Word (you may get a macro prompt; it doesn’t matter whether you enable macros or not).
              – Press Ctrl+Enter to insert a page break.
              – Select View | Header and Footer.
              – Delete the text in the header for page 2.
              – Close Header/Footer view.
              – Delete the page break that you inserted (press Ctrl+A then Delete).
              – Close and save the template.

            • #1062899

              Hans you are a champ! Thankyou once again. It all works perfectly now.

              This is soooo handy for me to be able to do.

              Kind Regards

      • #1062788

        Or on second thoughts, Heading one (the Title) could be at the top of each of the subsequent documents.

        Kerry

    • #1062799

      (Edited by wdwells on 29-Apr-07 08:34. )

      Assuming your headers and footers are consistent throughout the document, it should be a relatively simple task to create a template based on your intended layout, then create a macro to:

      1. copy each group of paragraphs that begin with a level 2 heading and end where the next paragraph is either a level 2 heading or the end of the document;
      2. open a new document based on the template which you created;
      3. paste the clipboard into the new document;
      4. set the numbering to start at the appropriate spot;
      5. save the new document in accordance with your naming scheme;
      6. close the new document;
      7. return to step 1

      You may come unglued if there are cross references between chapters that have not been changed to hard text.
      HTH

    Viewing 1 reply thread
    Reply To: Macro to split large doc by Heading 2 (Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: