VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Cheating the Printer

by Jay Kreusch (6 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (7 Votes)

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.

Side Effects
Sometimes displays the splash screen of another program or a print dialog box
API Declarations
'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

Rate Cheating the Printer

'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

Download this snippet    Add to My Saved Code

Cheating the Printer Comments

No comments have been posted about Cheating the Printer. Why not be the first to post a comment about Cheating the Printer.

Post your comment

Subject:
Message:
0/1000 characters