how to insert RTF text into clipboard (API) : LUSENET : MW Software's Visual BASIC Descusion System : One Thread

How do I insert a RTF-coded text buffer (e.g. stored into a String) into clipboard using Windows API? I found a sample code fragment somewhere using 16bit API, so that I simply changed the declarations and tested it in Win98 & VB6. I tried to place a sample ascii string into the clipboard but it does not work. (for RTF I should only change the CF_ constant; BTW, what's the constant? I cannot find it in the API viewer... would the vb constant from Clipboard object documentation (&HBF01) work?). In the following codepiece, GlobalAlloc throws an Overflow error: --------------------------------------------------------- Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long (... other APIs decls and constants as pasted from API viewer ...)

Public Function Text2ClipBoard(szText As String) Dim wLen As Integer Dim hMemory As Integer Dim lpMemory As Long Dim retval As Variant Dim wFreeMemory As Integer

wLen = Len(szText) + 1 szText = szText & Chr$(0) hMemory = GlobalAlloc(GHND, wLen + 1)

If hMemory = 0 Then MsgBox "Unable to allocate memory." Exit Function End If

wFreeMemory = True lpMemory = GlobalLock(hMemory)

If lpMemory = 0 Then MsgBox "Unable to lock memory." GoTo T2CB_Free End If

retval = lstrcpy(lpMemory, szText) retval = GlobalUnlock(hMemory)

If OpenClipboard(0&) = 0 Then MsgBox "Unable to open Clipboard" GoTo T2CB_Free End If If EmptyClipboard() = 0 Then MsgBox "Unable to empty the clipboard." GoTo T2CB_Close End If If SetClipboardData(CF_TEXT, hMemory) = 0 Then MsgBox "Unable to set the clipboard data." GoTo T2CB_Close End If wFreeMemory = False

T2CB_Close: If CloseClipboard() = 0 Then MsgBox "Unable to close the Clipboard." End If If wFreeMemory Then GoTo T2CB_Free Exit Function T2CB_Free: If GlobalFree(hMemory) <> 0 Then MsgBox "Unable to free global memory." End If End Function --------------------------------------------------------- Thanx to all!

-- Daniele Fusi (, May 30, 1999


I don't know how do that with API, but depending of you're looking for you should use the tip below.

There is a property called RTFSelText in Microsoft RichTextBox. With this property you can retrieve the formatted text, to exchange with another control.

PS: Excuse me, my bad english.


Fred Boratto - BRAZIL

-- Fred Boratto (, May 31, 1999.

Moderation questions? read the FAQ