Private Type DOC_INFO_1 pDocName As String pOutputFile As String pDatatype As String End Type Public 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 Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrn As Long, pDefault As Any) As Long Public Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrn As Long, ByVal Level As Long, pDocInfo As DOC_INFO_1) As Long Public Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long Public Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrn As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long Public Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long Public Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long 调用的时候是 PrintTifFile "c:\11.tif"Public Function PrintTifFile(ByVal strFileName As String) As Boolean On Error GoTo Err_PrintTIFFile If Dir(strFileName) = "" Then PrintTifFile = False Exit Function End If
Dim lhPrint As Long Dim strPrinterName As String Dim bufWrite() As Byte Dim hFile As Integer Dim dwLeft As Long Dim lWritten As Long Const DWWRITE As Long = &H4000 Dim di As DOC_INFO_1 Dim nIndex As Integer
If InStr(strFileName, "\") Then For nIndex = Len(strFileName) To 1 Step -1 If Mid(strFileName, nIndex, 1) = "\" Then Exit For di.pDocName = Mid(strFileName, nIndex, 1) & di.pDocName Next nIndex Else di.pDocName = strFileName End If
Dim lRet As Long strPrinterName = Printer.DeviceName OutputLogFile strPrinterName & "||" & di.pDocName, "PrintTifFile" lRet = OpenPrinter(strPrinterName, lhPrint, vbNullString) If lRet = 0 Or lhPrint = 0 Then PrintTifFile = False MsgBox "Err.Desc = " & Err.Description & "=========1====Err.Num=" & Err.Number Exit Function End If OutputLogFile "PrintTifFile_2", "PrintTifFile" lRet = StartDocPrinter(lhPrint, 1, di) If lRet = 0 Then MsgBox Err.Description & "=========2====" & Err.Number PrintTifFile = False Exit Function End If OutputLogFile "PrintTifFile_3", "PrintTifFile" lRet = StartPagePrinter(lhPrint) If lRet = 0 Then PrintTifFile = False MsgBox Err.Description & "=========3====" & Err.Number Exit Function End If OutputLogFile "PrintTifFile_4", "PrintTifFile" hFile = FreeFile Open strFileName For Binary Access Read As hFile ReDim bufWrite(1 To DWWRITE) As Byte For nIndex = 1 To LOF(hFile) \ DWWRITE Get #hFile, , bufWrite lRet = WritePrinter(lhPrint, bufWrite(1), DWWRITE, lWritten) If lRet = 0 Then PrintTifFile = False MsgBox Err.Description & "========4=====" & Err.Number Exit Function End If Next nIndex OutputLogFile "PrintTifFile_5", "PrintTifFile" If LOF(hFile) Mod DWWRITE Then ReDim bufWrite(1 To (LOF(hFile) Mod DWWRITE)) As Byte Get #hFile, , bufWrite lRet = WritePrinter(lhPrint, bufWrite(1), DWWRITE, lWritten) If lRet = 0 Then PrintTifFile = False MsgBox Err.Description & "=========5====" & Err.Number Exit Function End If End If Close #hFile OutputLogFile "PrintTifFile_6", "PrintTifFile" lRet = EndPagePrinter(lhPrint) If lRet = 0 Then PrintTifFile = False MsgBox Err.Description & "=======6======" & Err.Number Exit Function End If OutputLogFile "PrintTifFile_7", "PrintTifFile" lRet = EndDocPrinter(lhPrint) If lRet = 0 Then PrintTifFile = False MsgBox Err.Description & "======7=======" & Err.Number Exit Function End If OutputLogFile "PrintTifFile_8", "PrintTifFile" lRet = ClosePrinter(lhPrint) If lRet = 0 Then PrintTifFile = False MsgBox Err.Description & "======8=======" & Err.Number Exit Function End If OutputLogFile "PrintTifFile_9", "PrintTifFile" MsgBox "OK" PrintTifFile = True Exit Function Err_PrintTIFFile: MsgBox Err.Description & "=====================" & Err.Number PrintTifFile = False End Function在调试的时候lRet = OpenPrinter(strPrinterName, lhPrint, vbNullString)在调试状态下这个函数就能正确返回值, 生成exe以后就出现错误, lhPrint的值是0, 返回值也是0, 而且弹出的信息中 err.desc = ”“ 和 err.number = 0
pDocName As String
pOutputFile As String
pDatatype As String
End Type
Public 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
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrn As Long, pDefault As Any) As Long
Public Declare Function StartDocPrinter Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrn As Long, ByVal Level As Long, pDocInfo As DOC_INFO_1) As Long
Public Declare Function StartPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Public Declare Function WritePrinter Lib "winspool.drv" (ByVal hPrn As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
Public Declare Function EndPagePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Public Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
调用的时候是 PrintTifFile "c:\11.tif"Public Function PrintTifFile(ByVal strFileName As String) As Boolean
On Error GoTo Err_PrintTIFFile If Dir(strFileName) = "" Then
PrintTifFile = False
Exit Function
End If
Dim lhPrint As Long
Dim strPrinterName As String
Dim bufWrite() As Byte
Dim hFile As Integer
Dim dwLeft As Long
Dim lWritten As Long
Const DWWRITE As Long = &H4000
Dim di As DOC_INFO_1
Dim nIndex As Integer
If InStr(strFileName, "\") Then
For nIndex = Len(strFileName) To 1 Step -1
If Mid(strFileName, nIndex, 1) = "\" Then Exit For
di.pDocName = Mid(strFileName, nIndex, 1) & di.pDocName
Next nIndex
Else
di.pDocName = strFileName
End If
di.pOutputFile = vbNullString
di.pDatatype = "RAW"
OutputLogFile "PrintTifFile_1", "PrintTifFile"
Dim lRet As Long
strPrinterName = Printer.DeviceName
OutputLogFile strPrinterName & "||" & di.pDocName, "PrintTifFile"
lRet = OpenPrinter(strPrinterName, lhPrint, vbNullString)
If lRet = 0 Or lhPrint = 0 Then
PrintTifFile = False
MsgBox "Err.Desc = " & Err.Description & "=========1====Err.Num=" & Err.Number
Exit Function
End If
OutputLogFile "PrintTifFile_2", "PrintTifFile"
lRet = StartDocPrinter(lhPrint, 1, di)
If lRet = 0 Then
MsgBox Err.Description & "=========2====" & Err.Number
PrintTifFile = False
Exit Function
End If
OutputLogFile "PrintTifFile_3", "PrintTifFile"
lRet = StartPagePrinter(lhPrint)
If lRet = 0 Then
PrintTifFile = False
MsgBox Err.Description & "=========3====" & Err.Number
Exit Function
End If
OutputLogFile "PrintTifFile_4", "PrintTifFile"
hFile = FreeFile
Open strFileName For Binary Access Read As hFile
ReDim bufWrite(1 To DWWRITE) As Byte
For nIndex = 1 To LOF(hFile) \ DWWRITE
Get #hFile, , bufWrite
lRet = WritePrinter(lhPrint, bufWrite(1), DWWRITE, lWritten)
If lRet = 0 Then
PrintTifFile = False
MsgBox Err.Description & "========4=====" & Err.Number
Exit Function
End If
Next nIndex
OutputLogFile "PrintTifFile_5", "PrintTifFile"
If LOF(hFile) Mod DWWRITE Then
ReDim bufWrite(1 To (LOF(hFile) Mod DWWRITE)) As Byte
Get #hFile, , bufWrite
lRet = WritePrinter(lhPrint, bufWrite(1), DWWRITE, lWritten)
If lRet = 0 Then
PrintTifFile = False
MsgBox Err.Description & "=========5====" & Err.Number
Exit Function
End If
End If
Close #hFile
OutputLogFile "PrintTifFile_6", "PrintTifFile"
lRet = EndPagePrinter(lhPrint)
If lRet = 0 Then
PrintTifFile = False
MsgBox Err.Description & "=======6======" & Err.Number
Exit Function
End If
OutputLogFile "PrintTifFile_7", "PrintTifFile"
lRet = EndDocPrinter(lhPrint)
If lRet = 0 Then
PrintTifFile = False
MsgBox Err.Description & "======7=======" & Err.Number
Exit Function
End If
OutputLogFile "PrintTifFile_8", "PrintTifFile"
lRet = ClosePrinter(lhPrint)
If lRet = 0 Then
PrintTifFile = False
MsgBox Err.Description & "======8=======" & Err.Number
Exit Function
End If
OutputLogFile "PrintTifFile_9", "PrintTifFile"
MsgBox "OK"
PrintTifFile = True
Exit Function
Err_PrintTIFFile:
MsgBox Err.Description & "=====================" & Err.Number
PrintTifFile = False
End Function在调试的时候lRet = OpenPrinter(strPrinterName, lhPrint, vbNullString)在调试状态下这个函数就能正确返回值, 生成exe以后就出现错误, lhPrint的值是0, 返回值也是0, 而且弹出的信息中 err.desc = ”“ 和 err.number = 0