Link to home
Start Free TrialLog in
Avatar of stephenlecomptejr
stephenlecomptejrFlag for United States of America

asked on

How to take MS Word document and split each page and save as an individual file using VBA?

All I want to do is take any Word document and run some VBA that will loop through each page and save each one as an individual document.  I've got 90% of the code already to do this but what syntax I need to add below that would enable me to do this?  Thank you in advance!

Public Sub SplitWordDoc()

  Dim intNumberOfPages As Integer
  Dim intPage As Integer
  Dim varNumberPages As Variant
  Dim strPage As String
  Dim sPath As String
  Dim sName As String
  Dim sNewName As String
 
'gets document application path to provide saving location
  sPath = Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, "\"))
  sName = Right(ActiveDocument.FullName, Len(ActiveDocument.FullName) - InStrRev(ActiveDocument.FullName, "\"))
  sName = Replace(sName, ".doc", "")
 
  'records number of Pages per Word Doc
  varNumberPages = ActiveDocument.Content.Information(wdActiveEndAdjustedPageNumber)
  intNumberOfPages = Val(varNumberPages)
  'loops through each page per Word Doc
  For intPage = 1 To intNumberOfPages
    Debug.Print intPage
    strPage = intPage
    'actually moves focus to the page in the Word Doc
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=strPage
    Selection.Find.ClearFormatting
    With Selection.Find
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
    End With
   
    'place
    'coding to save page of the document as a file
    'here
    'saving of selection should occur
    'save document as the variable:  sNewName
    '
    sNewName = sPath & sName & "_Page" & strPage & ".doc"
    '
    'Save selection with the name: sNewName
   
  Next intPage

End Sub
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stephenlecomptejr

ASKER

Thank you for the solid code.