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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45015108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:52:28  来源:igfitidea点击:

How to get the number of pages in a pdf document using VBA?

vbaexcel-vbapdfacrobatexcel

提问by Tim

I will post my solution to this question, but maybe others have found a better way.

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

I wanted to obtain the number of pages in a pdf document using VBA.

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

I reviewed similar [vba] and [acrobat] questions, but I did not find a stand alone solution. After reviewing other posts, Adobe Acrobat's SDK, and the VBA object browser, I learned enough to piece together this solution.

我查看了类似的 [vba] 和 [acrobat] 问题,但没有找到独立的解决方案。在查看了其他帖子、Adobe Acrobat 的 SDK 和 VBA 对象浏览器后,我学到了足够的知识来拼凑这个解决方案。

I am running Excel 2013 and Adobe Acrobat 9.0 Pro.

我正在运行 Excel 2013 和 Adob​​e Acrobat 9.0 Pro。

I understand its ok to answer my own question.

我明白可以回答我自己的问题

回答by Tim

This solution works when Excel 2013 Professional and Adobe Acrobat 9.0 Pro are installed.

当安装了 Excel 2013 Professional 和 Adob​​e Acrobat 9.0 Pro 时,此解决方案有效。

You will need to enable the Adobe object model: Tools -> References -> Acrobat checkbox selected.

您需要启用 Adob​​e 对象模型:选择工具 -> 参考 -> Acrobat 复选框。

Adobe's SDKhas limited documentation on the GetNumPages method.

Adobe 的 SDK对 GetNumPages 方法的文档有限。

'with Adobe Acrobat 9 Professional installed
'with Tools -> References -> Acrobat checkbox selected

Sub AcrobatGetNumPages()

Dim AcroDoc As Object

Set AcroDoc = New AcroPDDoc

AcroDoc.Open ("C:\Users\Public\Lorem ipsum.pdf") 'update file location

PageNum = AcroDoc.GetNumPages

MsgBox PageNum

AcroDoc.Close

End Sub