Saturday, July 5, 2008

Print via DOS mode using a DotMatrix printer in VB 6.0.

Q:
Hi Iam preparing a report on VB6. I wish to have my report printed in the DOS mode using a DotMatrix printer. How will I do it, since Iam not working on the command prompt. Is there any way to solve this issue? Are there any general rules irrespective of the tools used to undertake such printing ? Any help/suggestion in this regard would be highly appreciated.
Answer:
I wrote a driver module using windows API's for DOS print through VB 6.0. If you want the Module please send me your mail ID via comments in the bottom of this Post. Or simply copy and paste the solution below . if you have any doubt in this feel free to ask me...
Program:


Declare Function PrinterProperties Lib "winspool.drv" (ByVal hWnd As Long, ByVal hPrinter As Long) As Long
Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Public Declare Function DefineDosDevice Lib "kernel32" Alias "DefineDosDeviceA" (ByVal dwFlags As Long, ByVal lpDeviceName As String, ByVal lpTargetPath As String) As Long
Public Const DDD_RAW_TARGET_PATH = &H1
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
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
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal _
hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
pcWritten As Long) As Long
Dim lhPrinter As Long
Dim lsp As Integer
Dim hsp As Integer
'------------------------------------------------------------
Function load_pr() As Boolean
Dim lReturn As Long
Dim lDoc As Long
Dim MyDocInfo As DOCINFO
lReturn = OpenPrinter(Printer.DeviceName, lhPrinter, 0)
If lReturn = 0 Then
MsgBox "The Printer Name you typed wasn't recognized."
Exit Function
End If
MyDocInfo.pDocName = "AAAAAA"
MyDocInfo.pOutputFile = vbNullString
MyDocInfo.pDatatype = vbNullString
lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
Call StartPagePrinter(lhPrinter)
End Function
'---------------------------------------------------
Function send_pr(sss As String) As Boolean
Dim lReturn As Long
Dim lpcWritten As Long
'Dim sWrittenData As String
'sWrittenData = "How's that for Magic !!!!" & vbCrLf
sss = sss & vbCrLf
lReturn = WritePrinter(lhPrinter, ByVal sss, Len(sss), lpcWritten)

End Function
'--------------------------------------------------------------------
Function unload_pr() As Boolean
Dim lReturn As Long

lReturn = EndPagePrinter(lhPrinter)
lReturn = EndDocPrinter(lhPrinter)
lReturn = ClosePrinter(lhPrinter)

End Function

1 comment:

Unknown said...

its working fine.

Can u help me to change the fonts of the printer in dos mode itself