VB icon

Cheating the Printer

Email
Submitted on: 1/8/2015 2:58:00 AM
By: Jay Kreusch (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: VB 5.0, VB 6.0
Views: 722
 
     Why mess around with the printer object if you don't have to? In my example, I print the contents of a richtextbox control to the printer with only a couple of lines of code. PERFECTLY formatted. Readily applies to just about any control or string, though.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Cheating the Printer
'**************************************
'Used for the shell printing
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal _
lpDirectory As String, ByVal nShowCmd As Long) As Long
'Used to come up with the temp file directory
Private Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" (ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long
'used to come up with the temp file name
Private Declare Function GetTempFileName Lib "kernel32" _
Alias "GetTempFileNameA" (ByVal lpszPath As String, _
ByVal lpPrefixString As String, ByVal wUnique As Long, _
ByVal lpTempFileName As String) As Long
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Cheating the Printer
' Description:Why mess around with the printer object if you don't have to? In my example, I print the contents of a richtextbox control to the printer with only a couple of lines of code. PERFECTLY formatted. Readily applies to just about any control or string, though.
' By: Jay Kreusch (from psc cd)
'
' Side Effects:Sometimes displays the splash screen of another program or a print dialog box
'**************************************

'All you need to provide is a prefix if desired, and the file extention
Private Function CreateTempFile(sPrefix As String, sSuffix As String) As String
Dim sTmpPath As String * 512
Dim sTmpName As String * 576
Dim nRet As Long
'Some API and string manipulation to get the temp file created
nRet = GetTempPath(512, sTmpPath)
If (nRet > 0 And nRet < 512) Then
 nRet = GetTempFileName(sTmpPath, sPrefix, 0, sTmpName)
 If nRet <> 0 Then
 sTmpName = Left$(sTmpName, _
InStr(sTmpName, vbNullChar) - 1)
CreateTempFile = Left(Trim(sTmpName), Len(Trim(sTmpName)) - 3) & sSuffix
 End If
End If
End Function
Private Sub Command1_Click()
Dim sTmpFile As String
Dim sMsg As String
Dim hFile As Long
'We're trying to print a richtextbox, so give it something to name
'it by, and make sure you set the extention to rtf.
'You could print a textbox by using txt, etc.
sTmpFile = CreateTempFile("jTmp", "rtf")
'Gets the next available open number
hFile = FreeFile
'open the file and give it the textRTF of the richtextbox
'if you don't want to use boxed, you could just pass a string here
Open sTmpFile For Binary As hFile
 Put #hFile, , RichTextBox1.TextRTF
Close hFile
'shell print it
Call ShellExecute(0&, "Print", sTmpFile, vbNullString, vbNullString, vbHide)
'delete it.
Kill sTmpFile
End Sub


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.