AllDocToPDF




Public Enum InternalFileFormat
Word_File = 1
Excel_File = 2
Text_File = 3
End Enum
Public Sub ConvertFiles(FolderPath As String, FileFormat _
As InternalFileFormat, Optional FileNme As String)
'Il passaggio di FileName e' opzionale, se non

'viene passato il programma assume per default

'la modifica di tutti i tipi di documento supportati

'all'interno della directory passata in formato PDF


Dim InternalFormat As Byte
Dim strFileToConvert As String
Dim strFolder As String
Dim StrFileNme As String
Dim StrResult As String

strFolder = FolderPath
InternalFormat = FileFormat
StrFileNme = FileNme
If FileNme = "" Or IsEmpty(FileNme) Then
'Conversione di tutti i files della directory

Select Case InternalFormat
Case Is = 1 'word files (tutti)
strFileToConvert = Dir(strFolder + "*.doc")
Case Is = 2 'excel files (tutti)
strFileToConvert = Dir(strFolder + "*.xls")
Case Is = 3 'text files (tutti)
strFileToConvert = Dir(strFolder + "*.txt")
End Select
While strFileToConvert <> "" 'loop sulla directory
' avvia il tentativo di conversione PDF

If (ConvertFile(strFolder + strFileToConvert, _
FileFormat) = False) Then
Exit Sub
End If
' prossimo file

strFileToConvert = Dir
Wend
Else
'Converte un solo file

Select Case InternalFormat
Case Is = 1 'word file (singolo)
StrResult = ConvertFile(strFolder + _
StrFileNme, Word_File)
Case Is = 2 'excel file (singolo)
StrResult = ConvertFile(strFolder + _
StrFileNme, Excel_File)
Case Is = 3 'text file (singolo)
StrResult = ConvertFile(strFolder + _
StrFileNme, Text_File)
End Select
End If
End Sub

Private Function ConvertFile(strSourceFileName _
As String, FileType As InternalFileFormat) _
As String
On Error Goto ErrorHandler
Dim msWord As Word.Application
Set msWord = GetObject(Class:="Word.Application.8")
msWord.ActivePrinter = "Acrobat Distiller"
If FileType = Excel_File Then SendKeys "{~}"
msWord.Documents.Open strSourceFileName
If FileType = Excel_File Then SendKeys "{~}"
msWord.ActiveDocument.PrintOut
msWord.ActiveDocument.Close True
Set msWord = Nothing
ConvertFile = True
Exit Function
ErrorHandler:
If Err.Number = 429 Then
Set msWord = CreateObject("Word.Application.8")
Err.Clear
Resume
End If
If IsCriticalError Then
ConvertFile = False
Exit Function
Else
Resume
End If
End Function

Private Function IsCriticalError() As Boolean
Dim strErrorMessage As String
Dim strFileName As String
Dim intFileNo As Integer
Dim strErrDate As String
Dim strErrRef As String
'Gli errori verranno riportati in un file LOG

Select Case Err.Number
Case Else
strFileName = App.Path & "\PDFError.log"
intFileNo = FreeFile
Open strFileName For Append As #intFileNo
strErrDate = Format(Now, "mm/dd/yyyy, hh:mm:ss AM/PM")
strErrRef = Err.Number & " " & _
Err.Description & " source: " & Err.Source
Print #intFileNo, strErrDate
Print #intFileNo, strErrRef
Print #intFileNo, vbCrLf
Close #intFileNo
IsCriticalError = True
Exit Function
End Select
IsCriticalError = False
End Function

Questo codice e' una classe che va compilata in
una DLL

NOTE:
1)Verificate di avere Adobe Acrobat Distiller
nel vostro PC e verificate di aver referenziato
Microsoft Word nel vostro progetto VB.

2)Tutti gli errori durante il processo di conversione
verranno salvati nel file PDFError.log creato nella
directory della vostra applicazione

REQUISITI:
1) una directory dal nome "c:\documents\"
2) Files del tipo supportato XLS, DOC o TXT.
3) In mancanza di riferimenti singoli, tutti
i files supportati all'interno della directory
passata, verranno convertiti










( alldoctopdf.html )- by Paolo Puglisi - Modifica del 17/12/2023