Pada Module1 paste code berikut:
Option Explicit
Private Type DOCINFO
pDocName As String
pOutputFile As String
pDatatype As String
End Type
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias _
"StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Declare Function WritePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
pcWritten As Long) As Long
Private Type DOCINFO
pDocName As String
pOutputFile As String
pDatatype As String
End Type
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
ByVal pDefault As Long) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv" Alias _
"StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
pDocInfo As DOCINFO) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long) As Long
Declare Function WritePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
pcWritten As Long) As Long
Public lhPrinter As Long
Function LoadPrintRedirect() As Boolean
Dim lReturn As Long
Dim lDoc As Long
Dim MyDocInfo As DOCINFO
ClosePrintRedirect
lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
If lReturn = 0 Then
Exit Function
End If
MyDocInfo.pDocName = "Laporan Printer"
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
Call StartPagePrinter(lhPrinter)
LoadPrintRedirect = True
End Function
Sub ClosePrintRedirect()
Dim lReturn As Long
lReturn = EndPagePrinter(lhPrinter)
lReturn = EndDocPrinter(lhPrinter)
lReturn = ClosePrinter(lhPrinter)
End Sub
Sub WriteToPrinter(sWrittenData As String, Optional WithBR As Boolean = False)
Dim lReturn As Long
Dim lpcWritten As Long
sWrittenData = sWrittenData '& IIf(WithBR = True, vbCrLf, "")
lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
Len(sWrittenData), lpcWritten)
End Sub
Pada Form1 Buat 2 tombol (Command1,Command2) dan TextBox(Text1) lalu Paste Code berikut:
Private Sub Command1_Click()
LoadPrintRedirect
WriteToPrinter Replace(Text1.Text, vbCrLf, Chr(13))
ClosePrintRedirect
End Sub
Private Sub Command2_Click()
esc$ = Chr$(27)
boldon$ = esc$ + "(s3B"
boldoff$ = esc$ + "(s0B"
formfeed$ = Chr$(12)
LoadPrintRedirect
WriteToPrinter boldon$ + "This is in bold" + boldoff$ & vbCrLf
ClosePrintRedirect
End Sub
Private Sub Form_Load()
LoadPrintRedirect
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
ClosePrintRedirect
End Sub
LoadPrintRedirect
WriteToPrinter Replace(Text1.Text, vbCrLf, Chr(13))
ClosePrintRedirect
End Sub
Private Sub Command2_Click()
esc$ = Chr$(27)
boldon$ = esc$ + "(s3B"
boldoff$ = esc$ + "(s0B"
formfeed$ = Chr$(12)
LoadPrintRedirect
WriteToPrinter boldon$ + "This is in bold" + boldoff$ & vbCrLf
ClosePrintRedirect
End Sub
Private Sub Form_Load()
LoadPrintRedirect
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
ClosePrintRedirect
End Sub
Selesai dan selamat mencoba.
source : http://seven-technology.blogspot.com
0 komentar:
Posting Komentar