Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multi-page word doc extracted into individual page docs

Status
Not open for further replies.

hb2

Programmer
Jul 22, 2002
20
GB
I use MS Word XP.

I'd like to break a multi-page word document (produced in MS Word XP) with Header/Footers, into individual word documents (eg. Page 1 extracted to page1.doc, page 2 extracted to page2.doc etc.). The individual document must contain the Header/Footers.

Any suggestions?

thanks in advance

hb
 
And what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi hb2,

Copying the page is (relatively) easy (check out the "\Page" predefined bookmark) but copying the headers and footers is more complex.

The new document will always be the first page of the first section so you'll have to determine the appropriate header and footer depending on the setup and copy them from the original to the new.

Will any other page formatting (e.g. margins) want copying?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
This is what I'm currently using, which only copies the body of the page and not the header/footers or margins (which would be useful):-

Code:
Sub BreakIntoPages2()
'
' Divide a document into individual documents - One for each page
'
'
   
    'Used to set criteria for moving through the document by page.
    Application.Browser.Target = wdBrowsePage

    'Loop through the pages of the document
    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
      
        'Select and copy the text to the clipboard
        ActiveDocument.Bookmarks("\page").Range.Copy
        DocName = Split(ActiveDocument.Name, ".")

        'Open new document to paste the content of the clipboard into.
        Documents.Add
        Selection.Paste
        
        'scan document for page breaks. Remove if present.
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^m"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
                     
        'Removes the break that is copied at the end of the page, if any.
        Selection.TypeBackspace
              
        'Save the document with a new name
        ChangeFileOpenDirectory "C:\"
        DocNum = DocNum + 1
        ActiveDocument.SaveAs FileName:=DocName(0) & "_" & DocNum & ".doc"
        ActiveDocument.Close

        'Move the selection to the next page  in the document
        Application.Browser.Next
     
    Next i
    
    ActiveDocument.Close savechanges:=wdDoNotSaveChanges


End Sub

How could I modify it to include the footer/headers and margins?

thanks
 
Hi hb2,

I've had a quick look but there are so many combinations of variables involved - section breaks and page number formatting in particular - that I cannot write any general purpose code quickly. Is your requirement for a particular document (or type of document) which makes, perhaps, limited use of all the possible options? If so, perhaps you could let me know whether you use section breaks (and if so what type(s)) and whether you reset page numbers at any point? Or even whether you use multiple headers and footers at all.

Actually, the more I think about this, the more I think there are a host of potential problems with page body content as well - numbered lists (and other sequences) could get renumbered, etc. What is your real objective here?

Incidentally, after copying a page, there is no real point in scanning for page breaks - there is, at most, one at the end.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi hb2.

Tony is correct. There are so many possibilities that it would be difficult to write a general routine, unless your source documents are essentially similar. In that case, you would not be writing a general routine, you would be writing a specific routine.

Tony also pointed out the crux - what is your objective?

RE: headers and footers. This all depends on what you have as a header/footer structure in your original document.

If your original document has only one header (or footer) across the whole document, then it is fairly straightforward. However, if you have different headers (or footers) - say Different first page, or Different Odd/Even then it gets more complicated. AND, if you have different sections (as in Word Section, that is a Section break is present), then it gets complicated even more.

If you just have the one header (or footer) you can access it directly, copy it, and recreate it for your new documents.

If you have multiple headers (or footers), then you have to determine which one applies for the current page that is being extracted into a new document. Plus you need to make a design decision. Say you have Different first page. The current page is page three. Do you have a Different first page header in the new document, or the existing header for page 3?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top