Option Public
Option Declare
Declare Private Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" ( ByVal strDest As Any, ByVal lpSource As Any, ByVal Length As Any)
Declare Private Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
Declare Private Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Declare Private Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Private Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Private Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Declare Private Function GetClipboardData Lib "user32" (ByVal uFormat As Long) As Long
Declare Private Function CloseClipboard Lib "user32" () As Long
Declare Private Function EmptyClipboard Lib "user32" () As Long
Declare Private Function SetClipboardData Lib "user32" (ByVal uFormat As Long, ByVal hData As Long) As Long
'Clipboard Constants...
Private Const GMEM_MOVABLE = &H2&
Private Const GMEM_DDESHARE = &H2000&
Private Const CF_TEXT = 1
Private Const CANNOTOPENCLIPBOARD = 2
Private Const CANNOTGLOBALLOCK = 4
Private Const CANNOTCLOSECLIPBOARD = 5
Private Const CANNOTGLOBALALLOC = 6
Private Const CANNOTEMPTYCLIPBOARD = 7
Private Const CANNOTSETCLIPBOARDDATA = 8
Private Const CANNOTGLOBALFREE = 9
Function fSendToClipboard(strText As String) As Variant
Dim varRet As Variant
Dim fStClpData As Long
Dim hMem As Long
Dim lpMemory As Long
Dim lngSize As Long
Dim varTemp As Variant
varRet = False
fStClpData = False
lngSize = Len(strText) + 1
hMem = GlobalAlloc(GMEM_MOVABLE Or _
GMEM_DDESHARE, lngSize)
If (hMem) =0 Or IsNull(hMem)Then
varRet = Error(CANNOTGLOBALALLOC)
GoTo sTxtDone
End If
lpMemory = GlobalLock(hMem)
If (lpMemory) =0 Or IsNull(lpMemory) Then
varRet = Error(CANNOTGLOBALLOCK)
GoTo sTxtGlblFree
End If
Call MoveMemory(lpMemory, strText, lngSize)
Call GlobalUnlock(hMem)
varTemp = (OpenClipboard(0&))
If varTemp=0 Or IsNull(varTemp) Then
varRet = Error(CANNOTOPENCLIPBOARD)
GoTo sTxtGlblFree
End If
varTemp = (emptyClipboard())
If varTemp=0 Or IsNull(varTemp) Then
varRet = Error(CANNOTEMPTYCLIPBOARD)
GoTo fSendToClipboardCloseClipboard
End If
varTemp = SetClipboardData(CF_TEXT, hMem)
If varTemp=0 Or IsNull(varTemp) Then
varRet = Error(CANNOTSETCLIPBOARDDATA)
GoTo fSendToClipboardCloseClipboard
Else
fStClpData = True
End If
fSendToClipboardCloseClipboard:
varTemp = closeclipboard()
If varTemp=0 Or IsNull(varTemp) Then
varRet = Error(CANNOTCLOSECLIPBOARD)
End If
sTxtGlblFree:
If Not fStClpData Then
varTemp = globalfree(hmem)
If varTemp=0 Or IsNull(varTemp) Then
varRet = Error(CANNOTGLOBALFREE)
End If
End If
sTxtDone:
fSendToClipboard = varRet
End Function