Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用VBA获取pdf文档中的页数?_Vba_Excel_Pdf_Acrobat - Fatal编程技术网

如何使用VBA获取pdf文档中的页数?

如何使用VBA获取pdf文档中的页数?,vba,excel,pdf,acrobat,Vba,Excel,Pdf,Acrobat,我将发布我对这个问题的解决方案,但也许其他人已经找到了更好的方法 我想使用VBA获得pdf文档中的页数 我回顾了类似的[vba]和[acrobat]问题,但没有找到独立的解决方案。在阅读了其他文章、AdobeAcrobat的SDK和VBA对象浏览器之后,我学到了足够的知识来拼凑这个解决方案 我正在运行Excel 2013和Adobe Acrobat 9.0 Pro 我理解可以。安装Excel 2013 Professional和Adobe Acrobat 9.0 Pro时,此解决方案可以工作 您

我将发布我对这个问题的解决方案,但也许其他人已经找到了更好的方法

我想使用VBA获得pdf文档中的页数

我回顾了类似的[vba]和[acrobat]问题,但没有找到独立的解决方案。在阅读了其他文章、AdobeAcrobat的SDK和VBA对象浏览器之后,我学到了足够的知识来拼凑这个解决方案

我正在运行Excel 2013和Adobe Acrobat 9.0 Pro


我理解可以。

安装Excel 2013 Professional和Adobe Acrobat 9.0 Pro时,此解决方案可以工作

您需要启用Adobe对象模型:选中工具->引用->Acrobat复选框

关于GetNumPages方法的文档有限

安装Adobe Acrobat 9 Professional后的
'
'选中工具->参考->Acrobat复选框
Sub-AcrobatGetNumPages()
将AcroDoc作为对象
设置AcrodDoc=新建AcrodDoc
打开(“C:\Users\Public\Lorem ipsum.pdf”)'更新文件位置
PageNum=AcroDoc.GetNumPages
MsgBox PageNum
AcroDoc.关闭
端接头

此解决方案在安装Excel 2013 Professional和Adobe Acrobat 9.0 Pro时有效

您需要启用Adobe对象模型:选中工具->引用->Acrobat复选框

关于GetNumPages方法的文档有限

安装Adobe Acrobat 9 Professional后的
'
'选中工具->参考->Acrobat复选框
Sub-AcrobatGetNumPages()
将AcroDoc作为对象
设置AcrodDoc=新建AcrodDoc
打开(“C:\Users\Public\Lorem ipsum.pdf”)'更新文件位置
PageNum=AcroDoc.GetNumPages
MsgBox PageNum
AcroDoc.关闭
端接头
灵感来源于:

我创建了下面的函数。我没有安装Adob accrobat pro

子测试()

端接头

函数fNumberOfPages\u在\u PDF\u文件中(vFolder,vFileName)

Dim xStr作为字符串
Dim xFileNum尽可能长
Dim RegExp作为对象
'---如果文件不是PDF文件,则页数=0
如果不是像“*.pdf”这样的vFileName,那么
fNumberOfPages\u在\u PDF\u文件中=0
退出功能
如果结束
“---如果路径分隔符(\”)不存在,请添加它
如果正确(vFolder,1)Application.PathSeparator,则
vFolder=vFolder&Application.PathSeparator
如果结束
'---计算Pdf文件中的页数
xStr=“”
设置RegExp=CreateObject(“VBscript.RegExp”)
RegExp.Global=True
RegExp.Pattern=“/Type\s*/Page[^s]”
xFileNum=FreeFile
将二进制文件作为#xFileNum打开(vFolder和vFileName)
xStr=空间(LOF(xFileNum))
获取#xFileNum,xStr
关闭#xFileNum
fnumberofpage_在PDF_文件中=RegExp.Execute(xStr.Count)
端部功能的灵感来源于:

我创建了下面的函数。我没有安装Adob accrobat pro

子测试()

端接头

函数fNumberOfPages\u在\u PDF\u文件中(vFolder,vFileName)

Dim xStr作为字符串
Dim xFileNum尽可能长
Dim RegExp作为对象
'---如果文件不是PDF文件,则页数=0
如果不是像“*.pdf”这样的vFileName,那么
fNumberOfPages\u在\u PDF\u文件中=0
退出功能
如果结束
“---如果路径分隔符(\”)不存在,请添加它
如果正确(vFolder,1)Application.PathSeparator,则
vFolder=vFolder&Application.PathSeparator
如果结束
'---计算Pdf文件中的页数
xStr=“”
设置RegExp=CreateObject(“VBscript.RegExp”)
RegExp.Global=True
RegExp.Pattern=“/Type\s*/Page[^s]”
xFileNum=FreeFile
将二进制文件作为#xFileNum打开(vFolder和vFileName)
xStr=空间(LOF(xFileNum))
获取#xFileNum,xStr
关闭#xFileNum
fnumberofpage_在PDF_文件中=RegExp.Execute(xStr.Count)

结束功能

请在回答中添加一些说明,避免只回答代码。请在回答中添加一些说明,避免回答只回答代码1。请解释该解决方案的工作原理:页面计数上的正则表达式匹配。2.该解决方案是否适用于所有PDF文件?你检查过几个不同的文件了吗?3.请更正:请用Adobe Acrobat替换“Adobb Acrobat”" 4. 您似乎想要一个代码块而不是两个代码块—您可以使用预览模式来改善答案的外观,并使其更易于理解。1。请解释该解决方案的工作原理:页面计数上的正则表达式匹配。2.该解决方案是否适用于所有PDF文件?你检查过几个不同的文件了吗?3.请更正:请将“Adobb Acrobat”替换为Adobe Acrobat“4”。似乎您需要一个代码块而不是两个代码块-您可以使用预览模式来改善答案的外观,使其更易于理解。
Option Explicit
Public PDFDoc As AcroPDDoc, PDFPage As Object, A3&, A4&
Sub Main()
    Dim fso As FileSystemObject, fld As Folder, filePDF As File, fileName$, i&, Arr()
    Set fso = New FileSystemObject
    Set PDFDoc = New AcroPDDoc
    Set fld = fso.GetFolder(ThisWorkbook.Path)
    ReDim Arr(1 To 1000, 1 To 4)
    For Each filePDF In fld.Files
    
     Application.Calculation = xlCalculationManual
    
        fileName = filePDF.Name
        If Right(fileName, 4) = ".pdf" Then
            CountPagesPDF (ThisWorkbook.Path & "\" & fileName)
            i = i + 1
            Arr(i, 1) = fileName
            Arr(i, 2) = A3 + A4
            Arr(i, 3) = A3
            Arr(i, 4) = A4
            
        End If
    Next
    Range("A3:D" & Cells.Rows.Count).Clear
    Range("A3:D" & (i + 1)) = Arr
    Set PDFPage = Nothing
    Set PDFDoc = Nothing
    Set fso = Nothing
    
     Application.Calculation = xlCalculationAutomatic
    
End Sub

Sub CountPagesPDF(FullFileName$)
    Dim j&, n&, x, y
    A3 = 0
    A4 = 0
    PDFDoc.Open (FullFileName)
    n = PDFDoc.GetNumPages
    
    Application.Calculation = xlCalculationManual
    
    For j = 0 To n - 1
        Set PDFPage = PDFDoc.AcquirePage(j)
        x = PDFPage.GetSize().x
        y = PDFPage.GetSize().y
        If x + y > 1500 Then A3 = A3 + 1 Else A4 = A4 + 1
    Next
    
    Application.Calculation = xlCalculationAutomatic
    
    PDFDoc.Close
End Sub
Dim vFolder, vFileName

vFolder = "D:\Test Count Pages In PDF File\"
'vFolder = "D:\Test Count Pages In PDF File\" '--> fine for both forms (with or without PathSeparator)
vFileName = "My File.pdf"
Debug.Print fNumberOfPages_in_PDF_File(vFolder, vFileName)
    
Dim xStr As String
Dim xFileNum As Long
Dim RegExp As Object

'--- Number of Pages =0 if the file is not a PDF file
If Not vFileName Like "*.pdf" Then
    fNumberOfPages_in_PDF_File = 0
    Exit Function
End If

'--- Add PathSeparator ("\") if it does not exist
If Right(vFolder, 1) <> Application.PathSeparator Then
    vFolder = vFolder & Application.PathSeparator
End If

'--- Count the number of pages in Pdf File
xStr = ""
Set RegExp = CreateObject("VBscript.RegExp")
RegExp.Global = True
RegExp.Pattern = "/Type\s*/Page[^s]"
xFileNum = FreeFile
Open (vFolder & vFileName) For Binary As #xFileNum
    xStr = Space(LOF(xFileNum))
    Get #xFileNum, , xStr
Close #xFileNum

fNumberOfPages_in_PDF_File = RegExp.Execute(xStr).Count