'コメント行にしてあるのは、クリップボードセットで使うものです
'両方の関数を使うときには、コメント行を活かすと共通のDeclare文になります
Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long 'Get/Set
Declare PtrSafe Function CloseClipboard Lib "user32" () As Long 'Get/Set
'Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long 'Set
Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr 'Get
'Declare PtrSafe Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr 'Set
'Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As LongPtr 'Set
Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr 'Get/Set
Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As Long 'Get/Set
Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr 'Get/Set
Public Function saGetClipBoard() As String 'Text Only!
Dim hClipMemory As LongPtr
Dim lpClipMemory As LongPtr
Dim RetVal As LongPtr
Dim MyString As String
Const CF_TEXT = 1
Const CF_UNICODETEXT = 13
Const MAXSIZE = 4096
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)
' null 終了文字を取り除きます。
On Error Resume Next
MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1)
If Err <> 0 Then MyString = Null
Else
'MsgBox "Could not lock memory to copy string from."
End If
OutOfHere:
RetVal = CloseClipboard()
saGetClipBoard = MyString
End Function