none
Calculate the page numbers of all Word documents in a folder via vba RRS feed

  • Question

  • Hi,

    I want to calculate the page numbers of all Word documents in a folder using VBA.

    Here is the code , but I don't know why it returns wrong page numbers when running.

    Who can tell me what's wrong with my code? Thank you!

    Sub GetAllPageNumbers()
        Dim objWordApplication As Word.Application
        Dim nPageNumber As Integer
        Dim objFile As Variant
        Dim objFileSystem As Object
        Dim objFolders As Object
        
        Set objWordApplication = New Word.Application
        Set objFileSystem = CreateObject("scripting.filesystemobject")
        Set objFolders = objFileSystem.getfolder("E:\Temp")
        
        For Each objFile In objFolders.Files
            objWordApplication.Documents.Open (objFile)
            nPageNumber = nPageNumber + objWordApplication.ActiveDocument.BuiltinDocumentProperties(wdPropertyPages)
            objWordApplication.ActiveDocument.Close (False)
        Next objFile
        
        MsgBox nPageNumber    
        Set objFile = Nothing
        Set objFileSystem = Nothing
        Set objFolders = Nothing
        Set objWordApplication = Nothing
    End Sub


    • Edited by JO1221 Friday, January 13, 2017 7:33 AM
    Friday, January 13, 2017 7:28 AM

Answers

  • It's a bit hard to diagnose when you don't say what you're getting vs what you expect and whether you've confirmed that all files were in fact processed. That said, try:

    Sub GetAllPageNumbers()
    Dim wdApp As New Word.Application, wdDoc As Word.Document, lPageCount As Long, strFile
    Const strFldr As String = "E:\Temp\"
    strFile = Dir(strFldr & "*.doc", vbNormal)
    While strFile <> ""
      With wdApp
        Set wdDoc = .Documents.Open(FileName:=strFldr & strFile, _
          AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
        With wdDoc
          lPageCount = lPageCount & .ComputeStatistics(wdStatisticPages)
          .Close SaveChanges:=False
        End With
      End With
      strFile = Dir()
    Wend
    MsgBox lPageCount
    Set wdDoc = Nothing: Set wdApp = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by Chenchen LiModerator Monday, January 16, 2017 1:47 AM
    • Marked as answer by JO1221 Monday, January 16, 2017 7:58 AM
    Friday, January 13, 2017 8:30 AM

All replies

  • It's a bit hard to diagnose when you don't say what you're getting vs what you expect and whether you've confirmed that all files were in fact processed. That said, try:

    Sub GetAllPageNumbers()
    Dim wdApp As New Word.Application, wdDoc As Word.Document, lPageCount As Long, strFile
    Const strFldr As String = "E:\Temp\"
    strFile = Dir(strFldr & "*.doc", vbNormal)
    While strFile <> ""
      With wdApp
        Set wdDoc = .Documents.Open(FileName:=strFldr & strFile, _
          AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
        With wdDoc
          lPageCount = lPageCount & .ComputeStatistics(wdStatisticPages)
          .Close SaveChanges:=False
        End With
      End With
      strFile = Dir()
    Wend
    MsgBox lPageCount
    Set wdDoc = Nothing: Set wdApp = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by Chenchen LiModerator Monday, January 16, 2017 1:47 AM
    • Marked as answer by JO1221 Monday, January 16, 2017 7:58 AM
    Friday, January 13, 2017 8:30 AM
  • Hi,Paul

    Thank you so much for your help!

    After comparing with  your macro code, I think I find my answer.It's my code ".BuiltinDocumentProperties(wdPropertyPages)" which can't get the right page number.

    Thanks again!

    Best Regards

    Monday, January 16, 2017 8:01 AM