PrintRTF




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

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 getthe 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, sogive 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










( printrtf.html )- by Paolo Puglisi - Modifica del 17/12/2023