Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adobe Acrobat X Proの体験版をインストールして、VBAでPDFを操作するコードを書いてみた
Option Explicit
Sub sample1()
Const XLS_PATH As String = "C:\テスト.xls"
Const PDF_PATH As String = "C:\テスト.pdf"
Dim PDF As PDF
Set PDF = New PDF
PDF.doc2pdf XLS_PATH, PDF_PATH
End Sub
Sub sample2()
Const PDF_PATH As String = "C:\テスト2.pdf"
Dim PDF As PDF
Set PDF = New PDF
MsgBox PDF.GetNumPages(PDF_PATH)
End Sub
Option Explicit
' OfficeドキュメントなどをPDFファイルに変換
Public Sub doc2pdf(DocPath As String, PdfPath As String)
Const PDSaveFull = 1
Dim AVDoc As Object ' Acrobat.AcroAVDoc
Dim IsOK As Boolean
' ファイルを開くと同時にPDFに変換されて表示される
Set AVDoc = CreateObject("AcroExch.AVDoc")
IsOK = AVDoc.Open(DocPath, "")
If IsOK = False Then
Err.Raise 1, , "Can not open file."
End If
'名前をつけてPDFファイルとして保存
IsOK = AVDoc.GetPDDoc().Save(PDSaveFull, PdfPath)
AVDoc.Close True
If IsOK = False Then
Err.Raise 2, , "Can not save file."
End If
End Sub
' ページ数取得
Public Function GetNumPages(PdfPath As String) As Long
Dim PDDoc As Object
Set PDDoc = CreateObject("AcroExch.PDDoc")
If PDDoc.Open(PdfPath) Then
GetNumPages = PDDoc.GetNumPages
PDDoc.Close
Else
Err.Raise 1, , "Can not open file."
End If
End Function
教えて! Watch - VBからPDFファイル自動生成するには
http://oshiete1.watch.impress.co.jp/qa197098.html
VBからPDFのページ数を取得するには?
http://homepage1.nifty.com/MADIA/vb/vb_bbs2/200302/200302_03020030.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.