Copy RTF Text to Clipboard
Based on How To Add Custom Table to the Clipboard to be Pasted into Word
MSDN: Windows Clipboard API$SAMPLECODE$
1 2 3 4 5 6 7 8 9 10 11 12 13 TEXT TO lcRtf NOSHOW PRETEXT 15 {\rtf1\par \trowd \trgaph30\trleft-30\trrh262\cellx980\cellx1991\cellx3001 \intbl \qr \f0\fs20 \cf 1\cell \qr 2\cell\qr 3\cell \intbl \row \trowd \trgaph30\trleft-30\trrh262\cellx980\cellx1991\cellx3001 \intbl \qr \f0\fs20 \cf 4\cell \qr 5\cell\qr 6\cell \intbl \row} ENDTEXT ? CopyRtfText2Clipboard(lcRtf) RETURN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 * Copy RTF text into Clipboard FUNCTION CopyRtfText2Clipboard(tcRtfText) LOCAL lnDataLen, lcDropFiles, llOk, i, lhMem, lnPtr, lcRtfText, lnRtfFormat * Global Memory Variables with Compile Time Constants #DEFINE GMEM_MOVABLE 0x0002 #DEFINE GMEM_ZEROINIT 0x0040 #DEFINE GMEM_SHARE 0x2000 * Load required Windows API functions =LoadApiDlls() llOk = .T. lcRtfText = tcRtfText + CHR(0) lnDataLen = LEN(lcRtfText) * Copy text into the allocated memory lhMem = GlobalAlloc(GMEM_MOVABLE+GMEM_ZEROINIT+GMEM_SHARE, lnDataLen+4) lnPtr = GlobalLock(lhMem) =CopyFromStr(lnPtr, @lcRtfText, lnDataLen) =GlobalUnlock(lhMem) * Open clipboard and store RTF text into it llOk = (OpenClipboard(0) <> 0) IF llOk =EmptyClipboard() lnRtfFormat = RegisterClipboardFormat("Rich Text Format") llOk = (SetClipboardData(lnRtfFormat, lhMem) <> 0) * If call to SetClipboardData() is successful, the system will take ownership of the memory * otherwise we have to free it IF NOT llOk =GlobalFree(lhMem) ENDIF * Close clipboard =CloseClipboard() ENDIF RETURN llOk FUNCTION LoadApiDlls * Clipboard Functions DECLARE LONG OpenClipboard IN WIN32API LONG HWND DECLARE LONG CloseClipboard IN WIN32API DECLARE LONG EmptyClipboard IN WIN32API DECLARE LONG SetClipboardData IN WIN32API LONG uFormat, LONG hMem DECLARE LONG RegisterClipboardFormat IN WIN32API STRING lpszFormat * Memory Management Functions DECLARE LONG GlobalAlloc IN WIN32API LONG wFlags, LONG dwBytes DECLARE LONG GlobalFree IN WIN32API LONG HMEM DECLARE LONG GlobalLock IN WIN32API LONG HMEM DECLARE LONG GlobalUnlock IN WIN32API LONG HMEM DECLARE LONG RtlMoveMemory IN WIN32API As CopyFromStr LONG lpDest, String @lpSrc, LONG iLen RETURN
Comments