Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Re: Cut , Copy and Paste


Message #1 by gordon.wu@a... on Mon, 13 Jan 2003 05:26:07
As far as I know, for vba type applications you can't access the "clipboard" directly like you can in vb,  unless someone corrects
me,  you can do what you want by using the api calls.  You can use the code below inside a module and make calls to it from you
btnCopy_Click event.


'********** BEGIN WINDOWS CLIPBOARD DECLARATIONS **********
Const CF_TEXT = 1
Const GMEM_MOVEABLE = &H2
Const GMEM_ZEROINIT = &H40
Const GHND = &H42

Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function OpenClipboard Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "USER32" () As Long
Private Declare Function EmptyClipboard Lib "USER32" () As Long
Private Declare Function SetClipboardData Lib "USER32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardData Lib "USER32" (ByVal wFormat As Long) As Long
Private Declare Sub CopyMemoryLS Lib "Kernel32.dll" Alias "RtlMoveMemory" (ByVal dest As Any, ByVal src As Any, ByVal n As Long)

'********** END WINDOWS CLIPBOARD DECLARATIONS **********


Function ClipBoard_SetText(strCopyString As String) As Boolean           'used for Clipboard button
   Dim hGlobalMemory As Long, lpGlobalMemory As Long, hClipMemory As Long

   ' Allocate moveable global memory
   hGlobalMemory = GlobalAlloc(GHND, Len(strCopyString) + 1)
   ' Lock block to get far pointer to this memory
   lpGlobalMemory = GlobalLock(hGlobalMemory)
   ' Copy the string to global memory
   CopyMemoryLS lpGlobalMemory, strCopyString, Len(strCopyString)
   ' Unlock memory then copy to clipboard
   If GlobalUnlock(hGlobalMemory) = 0 Then
      If OpenClipboard(0&) <> 0 Then
      'If OpenClipboard(Screen.ActiveForm.Hwnd) <> 0 Then   'can't use when debugging
         Call EmptyClipboard
         hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
         ClipBoard_SetText = CBool(CloseClipboard)
      End If
   End If
End Function

Function ClipBoard_GetText() As String                                 
   Dim hClipMemory As Long, lpClipMemory As Long, strCBText As String
   Dim RetVal As Long, lngSize As Long
   
   If OpenClipboard(0&) <> 0 Then
   'If OpenClipboard(Screen.ActiveForm.Hwnd) <> 0 Then      'can't use when debugging
   'Get handle to global memory block that is referencing the text
      hClipMemory = GetClipboardData(CF_TEXT)
      If hClipMemory <> 0 Then
         'Lock Clipboard memory so we can reference the actual data string
         lpClipMemory = GlobalLock(hClipMemory)
         If lpClipMemory <> 0 Then
            lngSize = GlobalSize(lpClipMemory)              'size of string in clipboard
            strCBText = Space$(lngSize)                     'make VBA string to hold clipboard data
            CopyMemoryLS strCBText, lpClipMemory, lngSize   'copy from clipboard to our string
            RetVal = GlobalUnlock(hClipMemory)              'unlock the memory
            'Remove the null terminating character
            strCBText = Left(strCBText, InStr(1, strCBText, Chr$(0), 0) - 1)
         Else
            MsgBox "Could not lock memory to copy string from."
         End If
      End If
      Call CloseClipboard     'close the clipboard
   End If
   ClipBoard_GetText = strCBText
End Function


hth
Jack



  ----- Original Message ----- 
  From: gordon.wu@a... 
  To: professional vb 
  Sent: Monday, January 13, 2003 5:26 AM
  Subject: [pro_vb] Re: Cut , Copy and Paste


  I am using Access 97. How can I call the VB Clipboard Object?
  I write following code in a button click event procedure:

      Private Sub btnCopy_Click()
         'Copy the txtContent to windows clipboard
         Clipboard.SetText Me.txtContent  
      End Sub

  but it doesnot work. The Access cannot recorgnize the Clipboard object. 
  Any setting I need to do? Could anyone help me? Thanks in advance.


  > Thank you Roy, it works
  > 
  > Patrick


  Return to Index