How to read latest clipboard entry from VBA?

I need to read the latest Windows clipboard entry to a VBA string variable.  How is that done?
Who is Participating?
rockiroadsConnect With a Mentor Commented:
I had some simple API calls somewhere, that link above is not that helpful
I cant find it sorry
so try this from MS instead

cquinnConnect With a Mentor Commented:
Option Compare Database
Option Explicit

Declare Function GlobalUnlock Lib "KERNEL32" (ByVal hMem As Long) As Long
Declare Function GlobalLock Lib "KERNEL32" (ByVal hMem As Long) As Long
Declare Function GlobalAlloc Lib "KERNEL32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function CloseClipboard Lib "user32" () As Long
Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Declare Function EmptyClipboard Lib "user32" () As Long
Declare Function lstrcpy Lib "KERNEL32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Declare Function GlobalSize Lib "KERNEL32" (ByVal hMem As Long) As Long
Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

Public 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
         hClipMemory = GetClipboardData(CF_TEXT)
         If IsNull(hClipMemory) Then
            MsgBox "Could not allocate memory"
            GoTo OutOfHere
         End If

         lpClipMemory = GlobalLock(hClipMemory)

         If Not IsNull(lpClipMemory) Then
            MyString = Space$(MAXSIZE)
            RetVal = lstrcpy(MyString, lpClipMemory)
            RetVal = GlobalUnlock(hClipMemory)

            MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
            MsgBox "Could not lock memory to copy string from."
         End If


         RetVal = CloseClipboard()
         ClipBoard_GetData = MyString

End Function

Public Function ClipBoard_SetData(MyString As String)

         Dim hGlobalMemory As Long, lpGlobalMemory As Long
         Dim hClipMemory As Long, x As Long

         hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)
         lpGlobalMemory = GlobalLock(hGlobalMemory)

         lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

         If GlobalUnlock(hGlobalMemory) <> 0 Then
            MsgBox "Could not unlock memory location. Copy aborted."
            GoTo OutOfHere2
         End If

         If OpenClipboard(0&) = 0 Then
            MsgBox "Could not open the Clipboard. Copy aborted."
            Exit Function
         End If

         x = EmptyClipboard()

         hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)


         If CloseClipboard() = 0 Then
            MsgBox "Could not close Clipboard."
         End If

End Function
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.