Microsoft Word PageBreaks and SectionBreaks VBA, C#, VB.Net Example

Microsoft Word PageBreaks and SectionBreaks VBA, C#, VB.Net Example

Break

To separate the information in different blocks, or pages, Microsoft word offers two types of breaks PageBreaks and SectionBreaks both can be inserted using a range or selection object.

Page Breaks

  1. Page: breaks the page and take the control to new page
  2. Column: the text will move to the next column
  3. Text Wrapping: separates text around the object

Section Breaks

  1. Next Page: insert a section break and start the new section on the next page
  2. Continuous: insert a section break and start the new section on the same page
  3. Even Page: insert a section break and start the new section on the next even-numbered page
  4. Odd Page: insert a section break and start the new section on the next odd-numbered page

Steps

  1. Place the cursor from where you wish to insert new break
  2. Navigate to Layout ribbon
  3. Click on Breaks dropdown button
  4. Select appropriate break

VBA Code example

Public Sub InsertPageBreaks()
    'Bind selection
    Dim oSelection As Selection
    Set oSelection = Selection
    'New page
    oSelection.InsertBreak Type:=wdSectionNewPage
    'Column
    oSelection.InsertBreak Type:=wdSectionNewColumn
    'Text Wrapping
    oSelection.InsertBreak Type:=wdTextWrappingBreak
    'Next Page
    oSelection.InsertBreak Type:=wdSectionBreakNextPage
    'Continuous
    oSelection.InsertBreak Type:=wdSectionBreakContinuous
    'Even Page
    oSelection.InsertBreak Type:=wdSectionBreakEvenPage
    'Odd Page
    oSelection.InsertBreak Type:=wdSectionBreakOddPage
End Sub

Prerequisites

  1. Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
  2. Create Excel Addin in C# or VB code style (Visual Studio Tools for Office)
  3. Excel 2010 or above
  4. Create a ribbon designer and put button

VB.Net Code example

Private Sub btnBreaks_Click(sender As Object, e As RibbonControlEventArgs) Handles btnBreaks.Click
    'Bind selection
    Dim oSelection as word.Selection
    oSelection=oApplication.Selection

    'Page break
    oSelection.InsertBreak(WdBreakType.wdPageBreak)
    'Column break
    oSelection.InsertBreak(WdBreakType.wdColumnBreak)
    'Line break
    oSelection.InsertBreak(WdBreakType.wdLineBreak)
    'Line break clear left
    oSelection.InsertBreak(WdBreakType.wdLineBreakClearLeft)
    'Line break clear right
    oSelection.InsertBreak(WdBreakType.wdLineBreakClearRight)
    'Break continuous
    oSelection.InsertBreak(WdBreakType.wdSectionBreakContinuous)
    'Even page
    oSelection.InsertBreak(WdBreakType.wdSectionBreakEvenPage)
    'Next page
    oSelection.InsertBreak(WdBreakType.wdSectionBreakNextPage)
    'Odd page
    oSelection.InsertBreak(WdBreakType.wdSectionBreakOddPage)
    'Text wrapping
    oSelection.InsertBreak(WdBreakType.wdTextWrappingBreak)
End Sub

C# code example

private void btnDiffBrakes_Click(object sender, RibbonControlEventArgs e)
{            
    //Bind selection object
    wordApp.Selection oSelection = Globals.ThisAddIn.Application.Selection;

    //Nex page break
    oSelection.InsertBreak(wordApp.WdBreakType.wdSectionBreakNextPage);

    //Page bereak
    oSelection.InsertBreak(wordApp.WdBreakType.wdPageBreak);

    //Column break
    oSelection.InsertBreak(wordApp.WdBreakType.wdColumnBreak);
    
    //Line break
    oSelection.InsertBreak(wordApp.WdBreakType.wdLineBreak);

    //Line break clear left
    oSelection.InsertBreak(wordApp.WdBreakType.wdLineBreakClearLeft);

    //Line break clear right
    oSelection.InsertBreak(wordApp.WdBreakType.wdLineBreakClearRight);

    //Continuous brek
    oSelection.InsertBreak(wordApp.WdBreakType.wdSectionBreakContinuous);

    //Even page
    oSelection.InsertBreak(wordApp.WdBreakType.wdSectionBreakEvenPage);

    //Odd break
    oSelection.InsertBreak(wordApp.WdBreakType.wdSectionBreakOddPage);

    //Text wrap
    oSelection.InsertBreak(wordApp.WdBreakType.wdTextWrappingBreak);
}

Note: all breaks constants are derived from WdBreakType enum

Next >> Microsoft Word Automation using VBA

Leave a Reply

Your email address will not be published. Required fields are marked *