'=============================================
' Crea un file PDF oppure stampa su carta
'
' Per la creazione del file o la stampa su carta
' utilizza il medesimo codice
'=============================================
Option Explicit
Private WithEvents PDFCreator1 As PDFCreator.clsPDFCreator
Private pErr As clsPDFCreatorError, opt As clsPDFCreatorOptions
Private noStart As Boolean, fac As Double, StartTime As Date
Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Type WindowsDevice
WindowsDeviceUserName As String
WindowsDeviceShortName As String
WindowsDevicePortName As String
End Type
Const WINDOWS_SECTION_NAME = "windows"
Const DEVICE_KEY_NAME = "device"
Sub Stampa()
'--- questa sub è lanciata da entrambi i pulsanti
Printer.ScaleMode = 6
Printer.CurrentX = 10: Printer.CurrentY = 100
Printer.FontName = "Arial": Printer.FontSize = 14
Printer.Print "PROVA"
End Sub
Private Sub BotCreaPDF_Click()
Dim OldP As String
Dim PP As Printer
Dim OrgPrinter As WindowsDevice
Dim CartellaPDF As String
Dim NomeFilePDF As String
Dim Z As Long
CartellaPDF = App.Path
NomeFilePDF = "ProvaPDF.pdf"
Call GetDefaultPrinter(OrgPrinter)
OldP = OrgPrinter.WindowsDeviceUserName
With opt
.AutosaveDirectory = CartellaPDF
.AutosaveFilename = NomeFilePDF
.UseAutosave = 1
.UseAutosaveDirectory = 1
.AutosaveFormat = 0
End With
Set PDFCreator1.cOptions = opt
Set Printer = Printers(PrinterIndex("PDFCreator"))
With Printer
.ScaleMode = 6
.PrintQuality = 150
End With
'-----------------
Stampa
'-----------------
Printer.EndDoc
PDFCreator1.cPrinterStop = False
Do
Z = DoEvents()
Loop Until Dir$(CartellaPDF + "/" + NomeFilePDF) <> ""
PDFCreator1.cPrinterStop = False
'--- Ripristino la stampante di default
For Each PP In Printers
If UCase(PP.DeviceName) Like UCase(OldP) Then
Set Printer = PP
Exit For
End If
Next
PDFCreator1.cClose
While PDFCreator1.cProgramIsRunning
DoEvents
Sleep 100
Wend
DoEvents
Set PDFCreator1 = Nothing
Set pErr = Nothing
Set opt = Nothing
MsgBox ("Fatto")
End Sub
Private Sub BotStampaSuCarta_Click()
Stampa
Printer.EndDoc
End Sub
Private Sub GetDefaultPrinter(recDefaultPrinter As WindowsDevice)
Dim StrPos As Integer
Dim DefaultPrinter As String
'
DefaultPrinter = GetString(WINDOWS_SECTION_NAME, DEVICE_KEY_NAME, "", "")
StrPos = InStr(DefaultPrinter, ",")
recDefaultPrinter.WindowsDeviceUserName = Left$(DefaultPrinter, StrPos - 1)
DefaultPrinter = Mid$(DefaultPrinter, StrPos + 1)
StrPos = InStr(DefaultPrinter, ",")
recDefaultPrinter.WindowsDeviceShortName = Left$(DefaultPrinter, StrPos - 1)
recDefaultPrinter.WindowsDevicePortName = Mid$(DefaultPrinter, StrPos + 1)
End Sub
Private Function PrinterIndex(Printername As String) As Long
Dim i As Long
For i = 0 To Printers.Count - 1
If UCase(Printers(i).DeviceName) = UCase$(Printername) Then
PrinterIndex = i
Exit For
End If
Next i
End Function
Function GetString(SectionName As String, KeyName As String, DefaultValue As String, ProfileName As String) As String
Dim KeyValueLength As Integer
Dim KeyValue As String
KeyValue = Space$(256)
If Trim$(ProfileName) = "" Then
KeyValueLength = GetProfileString(SectionName, KeyName, DefaultValue, KeyValue, Len(KeyValue))
Else
KeyValueLength = GetPrivateProfileString(SectionName, KeyName, DefaultValue, KeyValue, Len(KeyValue), ProfileName)
End If
GetString = Left$(KeyValue, KeyValueLength)
End Function
Private Sub Form_Load()
'=================== PDF CREATOR
Set PDFCreator1 = New clsPDFCreator
Set pErr = New clsPDFCreatorError
With PDFCreator1
.cVisible = True
If .cStart("/NoProcessingAtStartup") = False Then
If .cStart("/NoProcessingAtStartup", True) = False Then
Exit Sub
End If
.cVisible = True
End If
' Get the options
Set opt = .cOptions
.cClearCache
noStart = False
End With
End Sub