|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 游乐缘 于 2022-3-16 16:06 编辑
最近与PDF有缘,遇到一些PDF相关的需求。在此分享利用Acrobat库的JSO对象,在指定的PDF文件相同路径下,按照指定的类型另存文件。
支持转换的文件类型有:"eps", "html", "htm", "jpeg", "jpg", "jpe", "jpf", "jpx", "jp2", "j2k", "j2c", "jpc", "docx", "doc", "png", "ps", "rft", "xlsx", "xls", "txt", "tiff", "tif", "xml", "html"*
*新增转换类型"html"
作者水平有限,如有问题,欢迎反馈。- Option Explicit
- 'Tools -> Reference -> Acrobat
- Sub test()
- Dim sFilePathPDF As String, sFileExtension As String
-
- sFilePathPDF = "C:\Temp\testConvert.pdf"
- sFileExtension = "jpg"
-
- ConvertPDF sFilePathPDF, sFileExtension
- End Sub
- Sub ConvertPDF(sFilePathPDF As String, sFileExtension As String)
- Dim objAcroApp As New Acrobat.AcroApp
- Dim objAcroAVDoc As New Acrobat.AcroAVDoc
- Dim objAcroPDDoc As Acrobat.AcroPDDoc
- Dim lRet As Long
- Dim jso As Object
- Dim sFilePath As String
- Dim sExportFormat As String
-
- If Dir(sFilePathPDF) = "" Then
- MsgBox "File not found!", _
- vbCritical, "File Path Error"
- Exit Sub
- End If
- If LCase(Right(sFilePathPDF, 3)) <> "pdf" Then
- MsgBox "Not PDF file!", vbCritical, "File Type Error"
- Exit Sub
- End If
-
- Select Case sFileExtension
- Case "eps"
- sExportFormat = "com.adobe.acrobat.eps"
- Case "html", "htm"
- sExportFormat = "com.adobe.acrobat.html"
- Case "jpeg", "jpg", "jpe"
- sExportFormat = "com.adobe.acrobat.jpeg"
- Case "jpf", "jpx", "jp2", "j2k", "j2c", "jpc"
- sExportFormat = "com.adobe.acrobat.jp2k"
- Case "docx"
- sExportFormat = "com.adobe.acrobat.docx"
- Case "doc"
- sExportFormat = "com.adobe.acrobat.doc"
- Case "png"
- sExportFormat = "com.adobe.acrobat.png"
- Case "ps"
- sExportFormat = "com.adobe.acrobat.ps"
- Case "rft"
- sExportFormat = "com.adobe.acrobat.rft"
- Case "xlsx"
- sExportFormat = "com.adobe.acrobat.xlsx"
- Case "xls"
- sExportFormat = "com.adobe.acrobat.spreadsheet"
- Case "txt"
- sExportFormat = "com.adobe.acrobat.accesstext"
- Case "tiff", "tif"
- sExportFormat = "com.adobe.acrobat.tiff"
- Case "xml"
- sExportFormat = "com.adobe.acrobat.xml-1-00"
- Case "html"
- sExportFormat = "com.adobe.acrobat.html-3-20"
- Case Else
- sExportFormat = "NA"
- End Select
-
- If sExportFormat <> "NA" Then
- sFilePath = Left(sFilePathPDF, InStrRev(sFilePathPDF, ".")) & sFileExtension
- lRet = objAcroApp.Show
- lRet = objAcroAVDoc.Open(sFilePathPDF, "")
- Set objAcroPDDoc = objAcroAVDoc.GetPDDoc()
- 'JavaScript Object
- Set jso = objAcroPDDoc.GetJSObject
- jso.SaveAs sFilePath, sExportFormat
-
- lRet = objAcroAVDoc.Close(1)
- lRet = objAcroApp.Hide
- lRet = objAcroApp.Exit
-
- Set jso = Nothing
- Set objAcroAVDoc = Nothing
- Set objAcroApp = Nothing
- End If
- End Sub
复制代码
|
评分
-
5
查看全部评分
-
|