1
votes

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
2
WdInformation.wdNumberOfPagesInDocument ? - braX
The Microsoft.Office.Interop namespace reflects the API provided by Office VBA (as close as possible), so basically anything that is possible in VBA can also be done in .NET using the Microsoft.Office.Interop namespace. Where exactly are you stuck / having problems with? Is it getting the ActiveDocument? In that case: You can get the ActiveDocument from the Word.Application class. - bassfader
@bassfader I've tried that, but can't get it to work. I'll post some of my code. - addohm
I think I got it. pages = .Application.ActiveDocument.Range.Information(Word.WdInformation.wdNumberOfPagesInDocument) - addohm
pages = objDoc.ComputeStatistics(WdStatistic.wdStatisticPages, False), give that a shot... - Trevor

2 Answers

3
votes

A way is to get last page number:

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

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
....