Word Interop并获取页数

时间:2017-08-17 16:03:33

标签: vb.net ms-word office-interop

我知道在VBA中,在文档中,我可以使用ActiveDocument.Range.Information(wdNumberOfPagesInDocument)获取页数,但我无法使用Microsoft.Office.Interop.Word在VB.Net中找到它的等效内容。
是否有其他方式可以达到文档中的页数?

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 个答案:

答案 0 :(得分:1)

一种方法是获取最后一页编号:

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

答案 1 :(得分:-2)

当您在VBA中编码时,父应用程序(Word,Excel等)的命名空间是显而易见的,因此诸如wdNumberOfPagesInDocument之类的常量具有定义。使用Microsoft.Office.Interop.Word,您需要提供命名空间信息;例如:

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