'コメント行にしてあるのは、クリップボード取得で使うものです
'両方の関数を使うときには、コメント行を活かすと共通の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 saSetClipBoard(strData As String) As Integer
Dim lngHwnd As LongPtr
Dim lngMem As LongPtr
Dim lngRet As LongPtr
Dim lngDataLen As Long
Dim blnErrflg As Boolean
Const GMEM_MOVEABLE = 2
Const CF_TEXT = 1
Const CF_UNICODETEXT = 13
Const MAXSIZE = 4096
blnErrflg = True
'クリップボードをオープン
If OpenClipboard(0&) <> 0 Then
'クリップボードを空にする
If EmptyClipboard() <> 0 Then
'グローバルメモリに書き込む領域を確保&ハンドルを取得
lngDataLen = LenB(strData) + 1
lngHwnd = GlobalAlloc(GMEM_MOVEABLE, lngDataLen)
If lngHwnd <> 0 Then
'グローバルメモリをロックしてそのポインタを取得
lngMem = GlobalLock(lngHwnd)
If lngMem <> 0 Then
'書き込むテキストをグローバルメモリにコピー
If lstrcpy(lngMem, strData) <> 0 Then
'クリップボードにメモリブロックのデータを書き込み
lngRet = SetClipboardData(CF_TEXT, lngHwnd)
blnErrflg = False
End If
'グローバルメモリブロックのロックを解除
lngRet = GlobalUnlock(lngHwnd)
End If
End If
End If
'クリップボードをクローズ
lngRet = CloseClipboard()
End If
'If blnErrflg Then MsgBox "エラー"
saSetClipBoard = blnErrflg
End Function