Option Compare Database
Option Explicit
Const MAX_SIZE = 255
Const MAX_SECTION = 2048
Dim pdfPath As String
Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal strAppName As String, ByVal strKeyName As String, ByVal strValue As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Declare Function GetVersion Lib "kernel32" () As Long
Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal strAppName As String, ByVal strKeyName As String, ByVal strDefault As String, ByVal strReturned As String, ByVal lngSize As Long) As Long
Type prnDevice
drDeviceName As String
drDriverName As String
drPort As String
End Type
Function GetDefaultPrinter(dr As prnDevice) As Boolean
'*******************************************
'Purpose: Get the default printer
'Date: 10-oct-2002, 12:04:50
'Called by:
'Calls: GetToken(), GetINIString()
'Inputs: ?GetDefaultPrinter(dr)
'*******************************************
Dim strBuffer As String
strBuffer = GetINIString("Windows", "Device"

If Len(strBuffer) > 0 Then
With dr
.drDeviceName = GetToken(strBuffer, ",", 1)
.drDriverName = GetToken(strBuffer, ",", 2)
.drPort = GetToken(strBuffer, ",", 3)
End With
GetDefaultPrinter = True
Else
GetDefaultPrinter = False
End If
End Function
Function GetINIString(ByVal strGroup As String, ByVal strItem As String) As Variant
'*******************************************
'Purpose: Get a string value from the WIN.INI file, from the register
'Author: A126423
'Date: 10-oct-2002, 12:02:47
'Called by: GetDefaultPrinter
'Calls: GetProfileString
'Inputs: ? GetINIString("Windows", "Device"

'*******************************************
Dim intChars As Integer
Dim strBuffer As String
strBuffer = String(MAX_SIZE, 0)
intChars = GetProfileString(strGroup, strItem, "", strBuffer, MAX_SIZE)
GetINIString = Left(strBuffer, intChars)
End Function
Function GetToken(ByVal strValue As String, ByVal strDelimiter As String, ByVal intPiece As Integer) As Variant
'*******************************************
'Date: 10-oct-2002, 12:08:25
'Called by: GetDefaultPrinter
'Inputs: ? GetToken(strBuffer, ",", 1)
'Output: "Acrobat PDFWriter"
'*******************************************
Dim intPos As Integer
Dim intLastPos As Integer
Dim intNewPos As Integer
On Error GoTo GetTokenExit
' Make sure the delimiter is just one character.
strDelimiter = Left(strDelimiter, 1)
' If the delimiter doesn't occur at all, or if
' the user's asked for a negative item, just return the item
' they passed in.
If (InStr(strValue, strDelimiter) = 0) Or (intPiece <= 0) Then
GetToken = strValue
Else
intPos = 0
intLastPos = 0
Do While intPiece > 0
intLastPos = intPos
intNewPos = InStr(intPos + 1, strValue, strDelimiter)
If intNewPos > 0 Then
intPos = intNewPos
intPiece = intPiece - 1
Else
' Catch the last piece, where there's no
' trailing token.
intPos = Len(strValue) + 1
Exit Do
End If
Loop
If intPiece > 1 Then
GetToken = Null
Else
GetToken = Mid$(strValue, intLastPos + 1, intPos - intLastPos - 1)
End If
End If
GetTokenExit:
Exit Function
GetTokenErr:
MsgBox "Error in GetToken: " & Error & " (" & Err & "

"
Resume GetTokenExit
End Function
Function SetDefaultPrinter(dr As prnDevice) As Boolean
'*******************************************
'Purpose: Set the default printer
'Date: 10-oct-2002, 12:11:59
'Called by: SendToPDF(),...
'Calls: WriteProfileString() [aht_apiWriteProfileString]
'Inputs: Call SetDefaultPrinter(dr)
'Output: False/True
'*******************************************
' Set the default printer device in Win.INI
' In:
' dr: a aht_tagDeviceRec structure to use as the source of information.
' Out:
' Return Value: True if set correctly, False otherwise.
' If successful, writes a string in the form:
' device=HP LaserJet 4,HPPCL5E,LPT1:
' to your Win.INI file.
'
' Requires the aht_apiWriteProfileString() declaration
' Requires type definitions
Dim strBuffer As String
' Build up the appropriate string.
strBuffer = dr.drDeviceName & ","
strBuffer = strBuffer & dr.drDriverName & ","
strBuffer = strBuffer & dr.drPort
' Now write that string out to WIN.INI.
SetDefaultPrinter = (WriteProfileString("Windows", "Device", strBuffer) <> 0)
End Function
Function OSVersion() As Boolean
'Purpose: Identify OS
'Date: 10-oct-2002, 12:28:17
'Called by: WriteIniFile
'Calls: GetVersion()
'Inputs: If OSVersion Then ...
'Output: True/False
'*******************************************
Dim nOSMajorVersion As Integer
Dim nOSMinorVersion As Integer
Dim versionNum As Long
Dim temp As Long
On Error Resume Next
nOSMajorVersion = 0
nOSMinorVersion = 0
versionNum = GetVersion()
If ((versionNum And &H80000000) = 0) Then
nOSMajorVersion = versionNum And &HFF
temp = (versionNum - (versionNum And &HFFFF0000)) / 256
nOSMinorVersion = temp
If (nOSMajorVersion > 4) Then 'WinNT4/2000
OSVersion = True
Exit Function
End If
If (nOSMajorVersion = 3) Then
OSVersion = False
Exit Function
End If
Else
OSVersion = False
Exit Function
End If
End Function
Sub WriteIniFile(key As String, value As String)
'*******************************************
'Purpose: Write in the registry, the default name for the Acrobat file
'Date: 10-oct-2002, 12:26:23
'Called by:
'Calls: OSVersion, WritePrivateProfileString
'Inputs: WriteIniFile "PDFFilename", pdfPath & codOwner & "0.pdf"
'*******************************************
Dim svPDFIni As String
If OSVersion Then
svPDFIni = "c:\winnt\system32\spool\drivers\w32x86\2\__pdf.ini" 'CARE: Windows NT
Else
svPDFIni = "c:\windows\system\pdfwritr.ini" 'CARE: Windows 95/98
End If
Call WritePrivateProfileString("Acrobat PDFWriter", key, value, svPDFIni)
End Sub
'Call Print_To_PDF(.dat01.Column(1), "Report", "_tmp\"

Function Print_To_PDF(rptName As String, FileName As String, URL As String)
'*******************************************
'Purpose:
'Date: octubre 24, 2001, 10:13:15
'*******************************************
Dim dr As prnDevice
Dim s0 As String, s1 As String, s2 As String
Dim pdfApp As Object
pdfPath = CurrentDBDir & URL & "\" ' "_tmp\"
If GetDefaultPrinter(dr) Then
s0 = dr.drDeviceName
s1 = dr.drDriverName
s2 = dr.drPort
End If
With dr
.drDeviceName = "Acrobat PDFWriter"
'.drDeviceName = "Acrobat Distiller"
.drDriverName = "winspool"
.drPort = "LPT1:"
End With
' set default printer to "Acrobat PDFWriter"
Call SetDefaultPrinter(dr)
' write the pdf file
Set pdfApp = CreateObject("Acroexch.app"
If Dir(pdfPath & FileName & "*.*"

<> "" Then 'if pdf doc exist
If URL = "_tmp\" Then Kill pdfPath & rptName & "*.*" 'del it
End If
DoCmd.Hourglass True
WriteIniFile "PDFFilename", pdfPath & FileName & ".pdf"
DoCmd.OpenReport rptName, acViewNormal
Set pdfApp = Nothing
' restore default printer
With dr
.drDeviceName = s0
.drDriverName = s1
.drPort = s2
End With
Call SetDefaultPrinter(dr)
DoCmd.Hourglass False
End Function
Function CurrentDBDir() As String
'*******************************************
'Name: CurrentDBDir (Function)
'Purpose: Devuelve el directorio de trabajo, donde está ubicada la BD
'Author: U500026@u50000@cm
'Date: mayo 11, 2001, 09:28:05
'Output: string: "k:\project\ovempr\"
'*******************************************
Dim strDBPath As String
Dim strDBFile As String
strDBPath = CurrentDb.Name
strDBFile = Dir(strDBPath)
CurrentDBDir = Left(strDBPath, Len(strDBPath) - Len(strDBFile))
End Function