
Using Clipboard object in Access 97
I've written sort of a toolbox with api's and other commonly used
functions.
That's why I use a prefix Tb... everywhere.
I use api calls for handling the clipboard. I found these on a technet CD
and modified them somewhat:
Public Declare Function TbApiOpenClipboard Lib "user32" Alias
"OpenClipboard" (ByVal hwnd As Long) As Long
Public Declare Function TbApiCloseClipboard Lib "user32" Alias
"CloseClipboard" () As Long
Public Declare Function TbApiEmptyClipboard Lib "user32" Alias
"EmptyClipboard" () As Long
Public Declare Function TbApiSetClipboardData Lib "user32" Alias
"SetClipboardData" (ByVal wFormat As Long, _
ByVal hMem As Long) As Long
Public Declare Function TbApiGlobalUnlock Lib "kernel32" Alias
"GlobalUnlock" (ByVal hMem As Long) As Long
Public Declare Function TbApiGlobalLock Lib "kernel32" Alias "GlobalLock"
(ByVal hMem As Long) As Long
Public Declare Function TbApiGlobalAlloc Lib "kernel32" Alias "GlobalAlloc"
(ByVal wFlags As Long, _
ByVal
dwBytes As Long) As Long
Public Declare Function TbApiLstrcpy Lib "kernel32" Alias "lstrcpy" (ByVal
lpString1 As Any, _
ByVal
lpString2 As Any) As Long
Public Declare Function TbApiGetClipboardData Lib "user32" Alias
"GetClipboardData" (ByVal wFormat As Long) As Long
Public Declare Function TbAliGlobalSize Lib "kernel32" Alias "GlobalSize"
(ByVal hMem As Long) As Long
Public Const TbGHND = &H42
Public Const TbCF_TEXT = 1
Public Const TbMAXSIZE = 4096
Public Function TbClipBoardGetData() As String
On Error GoTo Err_TbClipBoardGetData
Dim hClipMemory As Long
Dim lpClipMemory As Long
Dim strString As String
Dim RetVal As Long
If TbApiOpenClipboard(0&) = 0 Then
Exit Function
End If
' Obtain the handle to the global memory
' block that is referencing the text.
hClipMemory = TbApiGetClipboardData(TbCF_TEXT)
If IsNull(hClipMemory) Then
strString = ""
GoTo Exit_TbClipBoardGetData
End If
' Lock Clipboard memory so we can reference
' the actual data string.
lpClipMemory = TbApiGlobalLock(hClipMemory)
If Not IsNull(lpClipMemory) Then
strString = Chr(0) & Space$(TbMAXSIZE - 1)
RetVal = TbApiLstrcpy(strString, lpClipMemory)
RetVal = TbApiGlobalUnlock(hClipMemory)
' Peel off the null terminating character.
strString = Mid(strString, 1, InStr(1, strString, Chr$(0), 0) - 1)
Else
strString = ""
End If
Exit_TbClipBoardGetData:
RetVal = TbApiCloseClipboard()
TbClipBoardGetData = strString
Exit Function
Err_TbClipBoardGetData:
' use our own errorhandler instead of: Call TbShowError("Function
TbClipBoardGetData []", Erl, Err.Number, Err.Description)
Resume Exit_TbClipBoardGetData
End Function
Public Function TbClipBoardSetData(strString As String) As Boolean
On Error GoTo Err_TbClipBoardSetData
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long, x As Long
' Allocate moveable global memory.
hGlobalMemory = TbApiGlobalAlloc(TbGHND, Len(strString) + 1)
' Lock the block to get a far pointer
' to this memory.
lpGlobalMemory = TbApiGlobalLock(hGlobalMemory)
' Copy the string to this global memory.
lpGlobalMemory = TbApiLstrcpy(lpGlobalMemory, strString)
' Unlock the memory.
If TbApiGlobalUnlock(hGlobalMemory) <> 0 Then
GoTo Exit_TbClipBoardSetData
End If
' Open the Clipboard to copy data to.
If TbApiOpenClipboard(0&) = 0 Then
Exit Function
End If
' Clear the Clipboard.
x = TbApiEmptyClipboard()
' Copy the data to the Clipboard.
hClipMemory = TbApiSetClipboardData(TbCF_TEXT, hGlobalMemory)
TbClipBoardSetData = True
Exit_TbClipBoardSetData:
Call TbApiCloseClipboard
Exit Function
Err_TbClipBoardSetData:
' use our own errorhandler instead of: Call TbShowError("Function
TbClipBoardSetData []", Erl, Err.Number, Err.Description)
Resume Exit_TbClipBoardSetData
End Function
Quote:
> I am creating a app in Access 97 in which I would like to use the
clipboard
> object for cutting & pasting text, I inserted the VB objects & procedures
> type library into the references for the database, but whenever I refer
to
> the clipboard object, I get the error message "ActiveX component can't
> create object." The help for this error suggests that something is not
> installed correctly, however, the same code works fine in VB. Any
> suggestions?
> Rohn