I need help with the macro at the bottom of this post.
This macro is supposed to take a large Word document and save each page as a new, separate file.
The master file I am trying to separate is 213 pages. When I run the macro, it generates 213 new files, but the problem is that each new file it generates is the first page of the master file. In other words, all 213 new files are exactly the same.
Does anyone know why this would happen? What in the macro would cause it to create 213 copies of page 1 instead of 1 copy of each of the 213 pages in the master file?
Thanks in advance!
---
Sub SavePagesAsDoc()
Dim orig As Document
Dim page As Document
Dim numPages As Integer
Dim idx As Integer
Dim fn As String
' Keep a reference to the current document.
Set orig = ActiveDocument
' Calculate the number of pages
numPages = ActiveDocument.Range.Information(wdActiveEndPageNumber)
For idx = 1 To numPages
' Make sure the document is active
orig.Activate
' Go to the page with index idx
Selection.GoTo What:=wdGoToPage, Name:=idx
' Select the current page
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
' Copy the selection
Selection.Copy
' Create a new document
Set page = Documents.Add
' Activate it
page.Activate
' Paste the selection
Selection.PasteAndFormat wdFormatOriginalFormatting
' Generate the file name
fn = "Page" + CStr(idx) + ".doc"
' Save the document as Word 97-2003
page.SaveAs FileName:=fn, FileFormat:=wdFormatDocument, AddToRecentFiles:=False
' Close the document
page.Close
Next
End Sub
This macro is supposed to take a large Word document and save each page as a new, separate file.
The master file I am trying to separate is 213 pages. When I run the macro, it generates 213 new files, but the problem is that each new file it generates is the first page of the master file. In other words, all 213 new files are exactly the same.
Does anyone know why this would happen? What in the macro would cause it to create 213 copies of page 1 instead of 1 copy of each of the 213 pages in the master file?
Thanks in advance!
---
Sub SavePagesAsDoc()
Dim orig As Document
Dim page As Document
Dim numPages As Integer
Dim idx As Integer
Dim fn As String
' Keep a reference to the current document.
Set orig = ActiveDocument
' Calculate the number of pages
numPages = ActiveDocument.Range.Information(wdActiveEndPageNumber)
For idx = 1 To numPages
' Make sure the document is active
orig.Activate
' Go to the page with index idx
Selection.GoTo What:=wdGoToPage, Name:=idx
' Select the current page
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
' Copy the selection
Selection.Copy
' Create a new document
Set page = Documents.Add
' Activate it
page.Activate
' Paste the selection
Selection.PasteAndFormat wdFormatOriginalFormatting
' Generate the file name
fn = "Page" + CStr(idx) + ".doc"
' Save the document as Word 97-2003
page.SaveAs FileName:=fn, FileFormat:=wdFormatDocument, AddToRecentFiles:=False
' Close the document
page.Close
Next
End Sub