Adobe Acrobat X Proの体験版をインストールして、VBAでPDFを操作するコードを書いてみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
教えて! 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