Microsoft Word Page Object VBA, C#, VB.Net code example

Microsoft Word Page Object VBA, C#, VB.Net code example

Page

Refers to a page in a document. Use the Page object and the related methods and properties for dynamically defining page layout in a document. You can use Item method to access a specific page in a document. The following example accesses the second page in the active document and print width and height of the page.

Code example

Public Sub GetPage()
    'Declare page object
    Dim oPage As Page
    'Bind reference
    Set oPage = ActiveDocument.ActiveWindow.Panes(1).Pages.Item(2)
    'Print page width and height
    Debug.Print oPage.Width
    Debug.Print oPage.Height
End Sub

Output

Information : its a property to access the page number which is underlying Range or Selection object.

Code example:

Public Sub GetPageNumber()
    'Bind selection
    Dim oSelection As Selection
    Set oSelection = Selection
    'Active
    Debug.Print oSelection.Information(wdActiveEndAdjustedPageNumber)
End Sub

Prerequisites

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

C# code example

private void btnDiaplayPageWidthAndHeight_Click(object sender, RibbonControlEventArgs e)
{            
    //Declare Document object
    wordApp.Document oActDocument = Globals.ThisAddIn.Application.ActiveDocument;
    //get active window
    wordApp.Window oActWindow = oActDocument.ActiveWindow;
    //Declare pane object
    wordApp.Pane oActPane = oActWindow.Panes[1];
    //Declare page object and bind 2nd Page
    wordApp.Page oPage = oActPane.Pages[2];

    //Print page width
    Console.WriteLine("Page Width: " + oPage.Width);
    //Print page height
    Console.WriteLine("Page Height: " + oPage.Height);
}

Output

Page Width: 612
Page Height: 792

C# Selection example

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

    //Print active page number
    Console.WriteLine("Your selection located " + oSelection.Information[wordApp.WdInformation.wdActiveEndAdjustedPageNumber] +  " Number Page");
}

Output

Your selection located 2 Number Page

VB.Net Code example

Private Sub btnDiaplayPageWidthAndHeight_Click(sender As Object, e As RibbonControlEventArgs) Handles btnDiaplayPageWidthAndHeight.Click
    'Declare Document object
    Dim oDocument as word.Document
    'Set the reference of active document
    oDocument=Globals.ThisAddIn.Application.ActiveDocument
    
    'Get Active Window object
    Dim oActWindow As word.Window
    oActWindow = oActDocument.ActiveWindow
    
    'Declare pane object
    Dim oActPane As word.Pane
    oActPane = oActWindow.Panes(1)
    
    'Declare page object and bind 2nd Page
    Dim oPage As word.Page
    oPage = oActPane.Pages(2)
    
    'Print page width
    Console.WriteLine("Page Width: " + oPage.Width)
    'Print page height
    Console.WriteLine("Page Height: " + oPage.Height)
End Sub

Output

Page Width: 612
Page Height: 792

VB.Net Selection example:

Private Sub btnDiaplayPageNumber_Click(sender As Object, e As RibbonControlEventArgs) Handles btnDiaplayPageNumber.Click
    'Bind selection object
    Dim oSelection As word.Selection
    oSelection = Globals.ThisAddIn.Application.Selection
    
    'Print active page number
    Console.WriteLine("Your selection located " + oSelection.Information(word.WdInformation.wdActiveEndAdjustedPageNumber) +  " Number Page")
End Sub

Output

Your selection located 2 Number Page

Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.

Leave a Reply

Your email address will not be published.