stephenlecomptejr
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.FullNa me, InStrRev(ActiveDocument.Fu llName, "\"))
sName = Right(ActiveDocument.FullN ame, Len(ActiveDocument.FullNam e) - InStrRev(ActiveDocument.Fu llName, "\"))
sName = Replace(sName, ".doc", "")
'records number of Pages per Word Doc
varNumberPages = ActiveDocument.Content.Inf ormation(w dActiveEnd AdjustedPa geNumber)
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.ClearFormat ting
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
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.FullNa
sName = Right(ActiveDocument.FullN
sName = Replace(sName, ".doc", "")
'records number of Pages per Word Doc
varNumberPages = ActiveDocument.Content.Inf
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.ClearFormat
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER