PDA

View Full Version : MS Word - Save Pages into Individual page with same format



victor1123
10-07-2015, 02:19 AM
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

gmayor
10-10-2015, 01:09 AM
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.