과정은 다음과 같다.
1.변환할 문서 선택
2.PDF-Pro 프린터로 인쇄해서 PS파일 생성
3.PDF-Pro에 포함되어 있는 ps2pdf 프로그램을 통해 PS를 PDF로 변환
4.실행(여러개의 문서를 순차적으로 변환하는 작업이라면 PDF변환이 끝날 떄 까지 기다려야 문제가 발생하지 않으므로 WindowsAPI 함수 import해서 씀. )
5.임시 ps파일 삭제
VB:
sub convert(name_transfile as string, upload_path as string)
Windows("" & name_transfile & ".xls").Activate
'임시로 저장할 위치
temp_path = "c:\windows\temp"
'임시로 생성할 ps파일이름
psfilename = temp_path & "\" & name_transfile & ".ps"
pdffilename = upload_path & "\" & name_transfile & ".pdf"
'select printer
Application.ActivePrinter = "PPFR:에 있는 PDF-Pro Free"
'generate ps file
ActiveSheet.PrintOut Copies:=1, Collate:=True, printtoFile:=True, prToFilename:=psfilename
'setup command string for generating pdf file from ps file
distillercall = "cmd /c ""C:\progra~1\epapyrus\pdf-pr~1\ps2pdf.exe " & pdffilename & " < " & psfilename & """"
'run command and wait
msiShellAndWait distillercall, True
'파일 삭제
If Len(Dir$(psfilename)) > 0 Then
SetAttr psfilename, vbNormal
Kill psfilename
End If
'저장 후 닫기
ActiveWorkbook.Save
ActiveWindow.Close
end sub
'PFD 변환 프로그램 control 위한 함수
'Windows API/Global Declarations for :Shell And Wait
Private Const INFINITE = -1&
Private Const NORMAL_PRIORITY_CLASS = &H20&
Public Const STARTF_FORCEOFFFEEDBACK = &H80
Public Const STARTF_FORCEONFEEDBACK = &H40
Public Const STARTF_RUNFULLSCREEN = &H20 ' ignored For non-x86 platforms
Public Const STARTF_USECOUNTCHARS = &H8
Public Const STARTF_USEFILLATTRIBUTE = &H10
Public Const STARTF_USEPOSITION = &H4
Public Const STARTF_USESHOWWINDOW = &H1
Public Const STARTF_USESIZE = &H2
Public Const STARTF_USESTDHANDLES = &H100
Public Const SW_ERASE = &H4
Public Const SW_HIDE = 0
Public Const SW_INVALIDATE = &H2
Public Const SW_MAX = 10
Public Const SW_MAXIMIZE = 3
Public Const SW_MINIMIZE = 6
Public Const SW_NORMAL = 1
Public Const SW_OTHERUNZOOM = 4
Public Const SW_OTHERZOOM = 2
Public Const SW_PARENTCLOSING = 1
Public Const SW_PARENTOPENING = 3
Public Const SW_RESTORE = 9
Public Const SW_SCROLLCHILDREN = &H1
Public Const SW_SHOW = 5
Public Const SW_SHOWDEFAULT = 10
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOWNORMAL = 1
Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadID As Long
End Type
Private Declare Function _
WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Private Declare Function _
CreateProcessA Lib "kernel32" ( _
ByVal lpApplicationName As Long, ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Sub msiShellAndWait(ByVal CommandLine As String, _
ByVal bShowWindow As Boolean)
Dim ReturnValue As Long
Dim Start As STARTUPINFO
Dim Process As PROCESS_INFORMATION
' Initialize the STARTUPINFO structure:
Start.cb = Len(Start)
If bShowWindow = False Then
Start.dwFlags = STARTF_USESHOWWINDOW
Start.wShowWindow = SW_HIDE
End If
' Start the shelled application:
ReturnValue = CreateProcessA(0&, CommandLine, 0&, 0&, _
1&, NORMAL_PRIORITY_CLASS, 0&, 0&, Start, Process)
' Wait for the shelled application to finish:
ReturnValue = WaitForSingleObject(Process.hProcess, INFINITE)
ReturnValue = CloseHandle(Process.hProcess)
End Sub