2017-08-17 34 views
0

我知道在VBA中,在一個文檔中,我可以使用ActiveDocument.Range.Information(wdNumberOfPagesInDocument)得到頁數,但我無法在VB.Net中使用Microsoft.Office.Interop.Word找到它的等效值。
有沒有,也許我可以通過另一種方式獲得文檔中的頁面數量?字互操作和獲取頁數

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 
+0

WdInformation.wdNumberOfPagesInDocument? – braX

+1

「Microsoft.Office.Interop」命名空間反映了Office VBA提供的API(儘可能接近),因此VBA中可能實現的任何操作都可以使用「Microsoft.Office.Interop」命名空間在.NET中完成。你在哪裏卡住/有問題?它是否獲得'ActiveDocument'?在這種情況下:您可以從['Word.Application'類](https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._application.activedocument)獲取'ActiveDocument'。 ASPX)。 – bassfader

+0

@bassfader我已經嘗試過,但無法使其正常工作。我會發布一些我的代碼。 – Jaberwocky

回答

-2

當你的代碼VBA,父應用程序(如Word,Excel等)的命名空間是顯而易見的,所以如wdNumberOfPagesInDocument常量的定義。使用Microsoft.Office.Interop.Word您需要提供名稱空間信息;例如:

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

不知道你想用這個帖子做什麼,但它已在上面的評論中得到解答。 'pages = .Application.ActiveDocument.Range.Information(Word.WdInformation.wdNumberOfPages InDocument)' – Jaberwocky

1

一種方法是讓尾頁頁次:

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