1

I found this VBA function which reads PDF file for Binary to find out total page numbers in the file. Could someone please shed some light about how can we do this using VBScript. Here is the VBA code:

Function GetPageNum(PDF_File As String)
    Dim FileNum As Long
    Dim strRetVal As String
    Dim RegExp
    Set RegExp = CreateObject("VBscript.RegExp")
    RegExp.Global = True
    RegExp.Pattern = "/Type\s*/Page[^s]"
    FileNum = FreeFile
    Open PDF_File For Binary As #FileNum
        strRetVal = Space(LOF(FileNum))
        Get #FileNum, , strRetVal
    Close #FileNum
    GetPageNum = RegExp.Execute(strRetVal).Count
End Function
0

1 Answer 1

3

You could do something like this:

Function GetPageNum(PDF_File)
    Dim strRetVal 
    Dim RegExp: Set RegExp = CreateObject("VBscript.RegExp")
    RegExp.Global = True
    RegExp.Pattern = "/Type\s*/Page[^s]"

    Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    Dim oFile: Set oFile = oFSO.GetFile(PDF_File)

    If IsNull(oFile) Then
        'TODO: handle file-not-found, etc.
        Exit Function
    End If

    With oFile.OpenAsTextStream()
        strRetVal = .Read(oFile.Size)
        .Close
    End With

    GetPageNum = RegExp.Execute(strRetVal).Count
End Function

I just tested it and it works just fine. Here's an example of usage:

Dim pageNum
pageNum = GetPageNum("Your\PDF\FilePath.pdf")
MsgBox pageNum

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Ahmed. This is great. Now I can explore more and learn about it. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.