addohm
addohm

Reputation: 2455

Word Interop and getting page count

I know in VBA, within a document, I can get page count using ActiveDocument.Range.Information(wdNumberOfPagesInDocument), But I can't find an equivalent of it in VB.Net using Microsoft.Office.Interop.Word.
Is there, perhaps another way I can attain the quantity of pages in a document?

Public Class Window
    'set form level declarations
    Dim appPath As String
    Dim objWordApp As New Word.Application
    Dim objDoc As Word.Document
    Dim errorPosition As String
    Private Sub Window_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objDoc = objWordApp.ActiveDocument
        With objDoc
           pages = .ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
        End With
        objDoc = Nothing
     End Sub
     objWordApp = Nothing
 End Class

Upvotes: 1

Views: 3091

Answers (3)

Park jihyoung
Park jihyoung

Reputation: 1

In the Office.Interop.Word Verion 15.0.0

you can try paginate. like this.

objWordApp.Options.Pagination = true;
objWordApp.ActiveDocument.Repagenate();

And Then DocumentFormat.OpenXml Version 2.12.3

using (WordprocessingDocument document = WordprocessingDocument.Open(filePath, false))
{ document.ExtentedFilePropertuesPart.Properties.Pages.Text }

Upvotes: 0

shA.t
shA.t

Reputation: 16968

A way is to get last page number:

lastPageNumber = objDoc.Words.Last.Information[Wd.WdInformation.wdActiveEndPageNumber]

Upvotes: 3

Major Fifth
Major Fifth

Reputation: 90

When you code in VBA, the namespace of the parent application (Word, Excel, etc.) is obvious, so constants such as wdNumberOfPagesInDocument have definitions. With Microsoft.Office.Interop.Word you need to provide the namespace information; for example:

...
With objDoc
    pages = .Range.Information(Word.WdInforma‌​tion.wdNumberOfPagesInDocument)
End With
....

Upvotes: -2

Related Questions