Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1430
  • Last Modified:

64 bit clipboard for microsoft access 2013

Hi

I have office 365 (64 bit) installed in my computer.  I am using Microsoft Access.

I would like to read the clipboard (containing text)J in my vba code.

I would appreciate some help.   Thanks
0
peispud
Asked:
peispud
2 Solutions
 
MacroShadowCommented:
Depending upon your need this may suffice:
DoCmd.RunCommand acCmdPaste

Open in new window

If not you can use Windows API but that is a little more complicated:
Declare PtrSafe Function OpenClipboard Lib "user32" Alias "OpenClipboard" (ByVal hwnd As LongPtr) As Long
Declare PtrSafe Function CloseClipboard Lib "user32" Alias "CloseClipboard" () As Long
Declare PtrSafe Function GetClipboardData Lib "user32" Alias "GetClipboardDataA" (ByVal wFormat As Long) As LongPtr
Declare PtrSafe Function GlobalAlloc Lib "kernel32" Alias "GlobalAlloc" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
Declare PtrSafe Function GlobalLock Lib "kernel32" Alias "GlobalLock" (ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function GlobalUnlock Lib "kernel32" Alias "GlobalUnlock" (ByVal hMem As LongPtr) As Long
Declare PtrSafe Function GlobalSize Lib "kernel32" Alias "GlobalSize" (ByVal hMem As LongPtr) As LongPtr
Declare PtrSafe Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As String) As LongPtr

Public Const GHND = &H42 
Public Const CF_TEXT = 1 
Public Const MAXSIZE = 4096

Function ClipBoard_GetData() 
   Dim hClipMemory As Long 
   Dim lpClipMemory As Long 
   Dim MyString As String 
   Dim RetVal As Long 
 
   If OpenClipboard(0&) = 0 Then 
      MsgBox "Cannot open Clipboard. Another app. may have it open" 
      Exit Function 
   End If 
          
   ' Obtain the handle to the global memory 
   ' block that is referencing the text. 
   hClipMemory = GetClipboardData(CF_TEXT) 
   If IsNull(hClipMemory) Then 
      MsgBox "Could not allocate memory" 
      GoTo OutOfHere 
   End If 
 
   ' Lock Clipboard memory so we can reference 
   ' the actual data string. 
   lpClipMemory = GlobalLock(hClipMemory) 
 
   If Not IsNull(lpClipMemory) Then 
      MyString = Space$(MAXSIZE) 
      RetVal = lstrcpy(MyString, lpClipMemory) 
      RetVal = GlobalUnlock(hClipMemory) 
       
      ' Peel off the null terminating character. 
      MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1) 
   Else 
      MsgBox "Could not lock memory to copy string from." 
   End If 
 
OutOfHere: 
 
   RetVal = CloseClipboard() 
   ClipBoard_GetData = MyString 
 
End Function

Open in new window


From Microsoft adapted  by MacroShadow for 64-bit.
0
 
IrogSintaCommented:
Here are a couple of functions: one to copy from and the other to paste to the clipboard:
Public Function CopyTextToClipboard(ByVal strText As String)
    With New DataObject
        .SetText strText
        .PutInClipboard
    End With
End Function
 
Public Function GetClipboardText() As String
    With New DataObject
        .GetFromClipboard
        GetClipboardText= .GetText
    End With
End Function

Open in new window

For the above to work, you will need to add a reference to MSForms 2.0 Object library.  If you don't see it on the list of available references, you can use the Browse button and select C:\windows\system32\FM20.DLL


If you'd rather not add a reference, another option is to use Late-Binding using the CLSID of DataObject.
Const DATAOBJECT_ID As String = "new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}"
 
Public Function CopyTextToClipboard(ByVal strText As String)
    With CreateObject(DATAOBJECT_ID)
        .SetText strText
        .PutInClipboard
    End With
End Function
 
Public Function GetClipboardText() As String
    With CreateObject(DATAOBJECT_ID)
        .GetFromClipboard
        GetClipboardText = .GetText
    End With
End Function

Open in new window

Ron
0
 
peispudAuthor Commented:
Thank you.
0
 
Helen FeddemaCommented:
This is what I use (it requires setting a reference to the MS Forms 2.0 object library):

   Dim strMessage As String
   Dim intResult As Integer
   Dim dat As MSForms.DataObject
   Dim strCode As String
   
   strMessage = "Import code from clipboard?"
   intResult = MsgBox(strMessage, vbOKCancel, "Question")
   
   If intResult = vbCancel Then
      GoTo ErrorHandlerExit
   End If
   
   Set dat = New MSForms.DataObject
   dat.GetFromClipboard
   strCode = dat.GetText
 
   

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Tackle projects and never again get stuck behind a technical roadblock.
Join Now