Consulting

Results 1 to 2 of 2

Thread: MS Word - Save Pages into Individual page with same format

  1. #1

    MS Word - Save Pages into Individual page with same format

    Hi all,

    I have a file containing 30 pages which I need to save each page as individual file. I found some vba code for processing. However, the format of each individual page is not same as the original. Is there any way to make it?

    Thank you very much for your kind help orz

    Here is the code I used:

    Sub BreakOnPage()
    ' Used to set criteria for moving through the document by page.
    Application.Browser.Target = wdBrowsePage


    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")

    'Select and copy the text to the clipboard.
    ActiveDocument.Bookmarks("\page").Range.Copy

    ' Open new document to paste the content of the clipboard into.
    Documents.Add
    Selection.Paste
    ' Removes the break that is copied at the end of the page, if any.
    Selection.TypeBackspace
    ChangeFileOpenDirectory "C:\"
    DocNum = DocNum + 1
    ActiveDocument.SaveAs FileName:="test_" & 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

  2. #2
    Probably not. It depends on how the original document was formatted. The closest you can get is to base the new documents on the existing document e.g

    Documents.Add(Template:=ActiveDocument.FullName)
    Activedocument.Range.Paste 'thus overwriiting the document content.

    Maybe something like (untested)

    Sub BreakOnPage()
    Dim oDoc As Document
    Dim oNewDoc As Document
    Dim strTemplate As String
    
        ' Used to set criteria for moving through the document by page.
        Application.Browser.Target = wdBrowsePage
        Set oDoc = ActiveDocument
        oDoc.Save
        If Len(oDoc.Path) = 0 Then GoTo lbl_Exit
        strTemplate = oDoc.FullName
        For i = 1 To oDoc.BuiltInDocumentProperties("Number of Pages")
    
            'Select and copy the text to the clipboard.
            oDoc.Bookmarks("\page").Range.Copy
    
            ' Open new document to paste the content of the clipboard into.
            Set oNewDoc = Documents.Add(Template:=strTemplate)
            oNewDoc.Range.Paste
            ' Removes the break that is copied at the end of the page, if any.
            oNewDoc.Range.Characters.Last.Delete
            
            DocNum = DocNum + 1
            oNewDoc.SaveAs Filename:="C:\test_" & DocNum & ".doc"
            oNewDoc.Close
    
            ' Move the selection to the next page in the document.
            Application.Browser.Next
        Next i
        oDoc.Close savechanges:=wdDoNotSaveChanges
    lbl_Exit:
        Set oDoc = Nothing
        Set oNewDoc = Nothing
        Exit Sub
    End Sub
    If this is the result of a mail merge see http://www.gmayor.com/individual_merge_letters.htm and/or http://www.gmayor.com/MergeAndSplit.htm and if you really want to be certain of the format, use http://www.gmayor.com/ManyToOne.htm in one to one mode as this doesn't affect the document at all.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •