How do you insert a page break in a Word document using Visual Basic? RRS feed

  • Question

  • I've created a new Word document using Visual Basic.  I would just like to know how to insert a page break using Visual Basic.

    Thanks in advance,


    James Hutchinson

    Sunday, March 27, 2016 10:52 PM


All replies

  • You could use code like:

    With ActiveDocument.Paragraphs(3).Range
      .Collapse wdCollapseEnd
      .InsertBreak Type:=wdPageBreak
    End With

    Paul Edstein
    [MS MVP - Word]

    Monday, March 28, 2016 12:16 AM
  • Hi, jhutch03

    Use the following code to insert page break in newly created document

    Sub pagebreak()
    Const wdPageBreak = 7
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    objSelection.TypeText "This is page 1"
    objSelection.InsertBreak (wdPageBreak)
    objSelection.TypeText "This is page 2"
    End Sub



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 28, 2016 2:07 AM
  • If you're automating Word, you don't need to use 'Const wdPageBreak = 7' or '.InsertBreak (wdPageBreak)'. You could use just:

    objSelection.TypeText "This is page 1" & Chr(12) & "This is page 2"

    Likewise, for use from within Word, one could adapt my previous code and use:

    ActiveDocument.Paragraphs(3).Range.InsertAfter Chr(12)

    Paul Edstein
    [MS MVP - Word]

    Monday, March 28, 2016 2:16 AM
  • Hi,

    Check this solution which presents how to insert page break in a word document, it's based on a free word library, hope you can find some useful information from there.


    Tuesday, March 29, 2016 7:09 AM
  •   'Dim fso As New FileSystemObject
        Dim NoOfFiles As Double
        Dim counter As Integer
        Dim r_counter As Integer
        Dim s As String
        Dim listfiles As Files
        Dim newfile As Worksheet
        Dim mainworkbook As Workbook
        Dim FetchFileClicked
        Dim Folderpath As Variant
    Sub Sumit()
        If FetchFileClicked = False Then
            MsgBox "First click the 'Load Control File' button"
        End If
        Application.ScreenUpdating = False
        strRandom = Replace(Replace(Replace(Now, ":", ""), "/", ""), " ", "")
        MergeFileName = "Merger" & strRandom & ".doc"
        MergeFolder = mainworkbook.Sheets("Main").Range("L10").Value
        Set objWord = CreateObject("Word.Application")
       Set objdoc = objWord.Documents.Add
       'objWord.Visible = True
       Set objSelection = objWord.Selection
       'objSelection.TypeText ("Saving this file after this text")
       objdoc.SaveAs (MergeFolder & MergeFileName)
        For i = 1 To NoOfFiles
            If Range("B" & i).Value = "Yes" Then
                Set objTempWord = CreateObject("Word.Application")
                Set tempDoc = objWord.Documents.Open(Folderpath & "\" & Range("A" & i).Value)
                'Set wb = Documents.Open(MyPath & "\" & MyName)
                Set objTempSelection = objTempWord.Selection
                'Selection.EndKey Unit:=wdLine
                objSelection.PasteSpecial xlPasteAll
            End If
        Application.ScreenUpdating = True
        MsgBox "Completed...Merge File is saved at " & MergeFolder & MergeFileName
        FetchFileClicked = False
    End Sub
    Sub fetchFiles()
        Set mainworkbook = ActiveWorkbook
        Folderpath = mainworkbook.Sheets("Main").Range("L8").Value
        Set fso = CreateObject("Scripting.FileSystemObject")
        NoOfFiles = fso.GetFolder(Folderpath).Files.Count
        'MsgBox NoOfFiles
        Set listfiles = fso.GetFolder(Folderpath).Files
        counter = 0
        For Each fls In listfiles
            counter = counter + 1
            Range("A" & counter).Value = fls.Name
            'Range("B" & counter).Value = "Yes"
            Range("A" & counter).Borders.Value = 1
            Range("B" & counter).Borders.Value = 1
            With Range("B" & counter).Validation
                'replace "=A1:A6" with the range the data is in.
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                 Operator:=xlBetween, Formula1:="Yes,No"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True
            End With
        Call controlFile
         MsgBox "Control File Loaded"
         FetchFileClicked = True
    End Sub
    Sub controlFile()
     Worksheets("Main").Range("b1:b6").Formula = "=iferror(VLOOKUP(A1,Table2,MATCH(""load"",Table2[#Headers],0),0),"""")&"""""
    Application.Wait (Now + TimeValue("0:00:03"))
    End Sub
    You would think I would know how to do this....but based on this code, for the life of me, I can NOT figure out how to add a page break at the end of each document.  What the code does is take individual MS Word documents and combine them into one 1 document.  What I am trying to do is add the page break before adding the next Word file so that it starts on a new page....ugh...can you please help????...I have been at it for TOO MANY HOURS.  Thanks! Scott
    Wednesday, September 4, 2019 4:31 AM
    • Edited by macropodMVP Wednesday, September 4, 2019 6:55 AM
    Wednesday, September 4, 2019 6:54 AM