| Function file2PDF(fileNames As Variant,outPath As String,outFile As String) As String |
| %REM |
| ********************************************************************************** |
| Uses the PDFCreator COM - http: |
| to convert one or multiple files (and combine, if neccessary) to PDF |
| ********************************************************************************** |
| Example use: |
| Dim fileNames List As String |
| Dim outPath As String |
| Dim outFile As String |
| fileNames(1) = "c:\temp\test1.doc" |
| fileNames(2) = "c:\temp\test2.doc" |
| fileNames(3) = "c:\temp\test.xls" |
| outPath="c:\temp\" |
| Combined output file(s) name: |
| outFile="output.pdf" |
| If you don't want a combined output file, specify |
| outFile="" |
| Msgbox file2PDF(fileNames,outPath,outFile),64,"file2PDF Ausgabemeldung" |
| ********************************************************************************** |
| by jo@chim - version 2007/05/04 |
| ********************************************************************************** |
| %END REM |
| Dim PDFCreator As Variant |
| Dim combine As Boolean |
| Dim DefaultPrinter As String |
| Dim success As String |
| |
| Const sleepTime = 1 'Printer needs some sleepin' to avoid PDFCreator crash |
| |
| If Dir(outPath)="" Then |
| file2PDF="The output path '" & outPath & "' does not exist" |
| Exit Function |
| End If |
| |
| If outFile ="" Then |
| combine=False |
| Else |
| combine=True |
| End If |
| |
| Set PDFCreator = CreateObject("PDFCreator.clsPDFCreator") |
| With PDFCreator |
| .cStart "/NoProcessingAtStartup" |
| .cVisible = False |
| .cWindowState = 1 |
| .cOption("UseAutosave") = 1 |
| .cOption("UseAutosaveDirectory") = 1 |
| .cOption("AutosaveFormat") = 0 |
| .cOption("AutosaveDirectory") = outPath |
| DefaultPrinter = .cDefaultprinter |
| .cDefaultprinter = "PDFCreator" |
| .cClearcache |
| |
| Forall cFile In fileNames |
| If Dir(cfile)="" Then |
| success= success & Chr$(10) & "'"& cFile & "' does not exist" |
| Elseif Not .cIsPrintable(cFile) Then |
| success= success & Chr$(10) & "'"& cFile & "' cannot be converted to PDF" |
| Else |
| j=0 |
| If combine=False Then |
| For i = 1 To Len(cFile) |
| If Instr(i,cFile,"\")<>0 Then |
| j=Instr(i,cFile,"\") |
| End If |
| Next i |
| outFile=Right(cFile,Len(cFile)-j) |
| .cOption("AutosaveFilename") = outFile |
| .cPrintfile cFile |
| .cPrinterStop = False |
| Sleep sleepTime |
| Else |
| .cPrintfile cFile |
| Sleep sleepTime |
| End If |
| success= success & Chr$(10) & "'"& cFile & "' has been converted" |
| End If |
| End Forall |
| |
| If combine =True Then |
| .cOption("AutosaveFilename") = outFile |
| .cCombineAll |
| .cPrinterStop = False |
| Sleep sleepTime |
| success= success & Chr$(10) & Chr$(10) & "Combined output file:" & Chr$(10) & outPath & outFile |
| End If |
| |
| .cDefaultprinter = DefaultPrinter |
| .cClearcache |
| .cClose |
| End With |
| Set PDFCreator =Nothing |
| file2PDF=success |
| End Function |