Advertisement
exp111

Untitled

Feb 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. Sub SplitIntoPages()
  2. Dim docMultiple As Document
  3. Dim docSingle As Document
  4. Dim rngPage As Range
  5. Dim iCurrentPage As Integer
  6. Dim iPageCount As Integer
  7. Dim strNewFileName As String
  8. Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
  9. flicker a bit.
  10. Set docMultiple = ActiveDocument 'Work on the active document _
  11. (the one currently containing the Selection)
  12. Set rngPage = docMultiple.Range 'instantiate the range object
  13. iCurrentPage = 1
  14. 'get the document's page count
  15. iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)
  16. Do Until iCurrentPage > iPageCount
  17. If iCurrentPage = iPageCount Then
  18. rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
  19. Else
  20. 'Find the beginning of the next page
  21. 'Must use the Selection object. The Range.Goto method will not work on a page
  22. Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1
  23. 'Set the end of the range to the point between the pages
  24. rngPage.End = Selection.Start
  25. End If
  26. rngPage.Copy 'copy the page into the Windows clipboard
  27. Set docSingle = Documents.Add 'create a new document
  28. docSingle.Range.Paste 'paste the clipboard contents to the new document
  29. 'remove any manual page break to prevent a second blank
  30. docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""
  31. 'build a new sequentially-numbered file name based on the original multi-paged file name and path
  32. strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
  33. docSingle.SaveAs strNewFileName 'save the new single-paged document
  34. iCurrentPage = iCurrentPage + 1 'move to the next page
  35. docSingle.Close 'close the new document
  36. rngPage.Collapse wdCollapseEnd 'go to the next page
  37. Loop 'go to the top of the do loop
  38. Application.ScreenUpdating = True 'restore the screen updating
  39. 'Destroy the objects.
  40. Set docMultiple = Nothing
  41. Set docSingle = Nothing
  42. Set rngPage = Nothing
  43. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement