View Single Post
 
Old 10-10-2020, 05:36 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There are a couple of issues with your code, but the likely problem issues are that you have not defined the file type and you have not set the save path. Saving to the root of the C:\ drive would almost certainly be blocked by security issues.
It is better to declare your variables and name the documents e.g.


Code:
Sub BreakOnPage()
Dim i As Integer
Dim oSource As Document, oTarget As Document
Dim oRng As Range
Dim strPath As String
    strPath = "C:\Path\" 'enter the path here
    ' Used to set criteria for moving through the document by page.
    Application.Browser.Target = wdBrowsePage
    Set oSource = ActiveDocument
    For i = 1 To oSource.BuiltInDocumentProperties("Number of Pages")
        'Set a range to the page
        Set oRng = oSource.Bookmarks("\page").Range
        ' Remove the break that is copied at the end of the page, if any.
        oRng.End = oRng.End - 1
        'Create new document to copy the range to.
        Set oTarget = Documents.Add
        oTarget.Range.FormattedText = oRng.FormattedText
        oTarget.SaveAs FileName:=strPath & "page_" & i & ".html", FileFormat:=wdFormatFilteredHTML
        oTarget.Close
        ' Move the selection to the next page in the document
        Application.Browser.Next
    Next i
    oSource.Close savechanges:=wdDoNotSaveChanges
    Set oSource = Nothing
    Set oTarget = Nothing
    Set oRng = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote