Advertisement
1_2002 Files/ File Controls/ Input/ Output #106909

Cheating the Printer

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.

AI

Yapay Zeka Özeti: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kaynak Kod
original-source
'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
Orijinal Yorumlar (3)
Wayback Machine'den kurtarıldı