{"id":1979,"date":"2024-06-24T23:30:39","date_gmt":"2024-06-24T14:30:39","guid":{"rendered":"https:\/\/euc-access-excel-db.com\/tips\/?p=1979"},"modified":"2024-06-24T23:41:22","modified_gmt":"2024-06-24T14:41:22","slug":"clipboard_api01","status":"publish","type":"post","link":"https:\/\/euc-access-excel-db.com\/tips\/ct07_se\/ct075012_xls2k_vba_tips\/clipboard_api01","title":{"rendered":"ExcelVBA \uff5e \u30af\u30ea\u30c3\u30d7\u30dc\u30fc\u30c9\u306b\u9001\u53d7\u4fe1\u3059\u308b\u305f\u3081\u306eAPI\uff0864bit\u300132bit\u3001\u517c\u7528\uff09"},"content":{"rendered":"<p>ExcelVBA \uff5e \u30af\u30ea\u30c3\u30d7\u30dc\u30fc\u30c9\u306b\u9001\u53d7\u4fe1\u3059\u308b\u305f\u3081\u306eAPI\uff0864bit\u300132bit\u3001\u517c\u7528\uff09<\/p>\n<p>Excel\u306e\u300cDataObject\uff08Microsoft Forms 2.0 Object Library\uff09\u300d\u3092\u4f7f\u3046\u65b9\u6cd5\u304c\u3001<br \/>\n\u306a\u305c\u304b\u30c0\u30e1\u3060\u3063\u305f\uff08\u77ed\u3044\u6587\u5b57\u5217\u306f\u884c\u3051\u305f\u306e\u3067\u3059\u304c\uff09\u3001API\u306e\u307b\u3046\u304c\u78ba\u5b9f\u306a\u306e\u3067\u3053\u3061\u3089\u3067\u3084\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \">\r\n'\r\n'\r\nOption Explicit\r\n\r\n#If VBA7 And Win64 Then\r\nPrivate Declare PtrSafe Function OpenClipboard Lib \"user32.dll\" (ByVal hWnd As LongPtr) As Long\r\nPrivate Declare PtrSafe Function EmptyClipboard Lib \"user32.dll\" () As Long\r\nPrivate Declare PtrSafe Function CloseClipboard Lib \"user32.dll\" () As Long\r\nPrivate Declare PtrSafe Function IsClipboardFormatAvailable Lib \"user32.dll\" (ByVal wFormat As Long) As Long\r\nPrivate Declare PtrSafe Function GetClipboardData Lib \"user32.dll\" (ByVal wFormat As Long) As LongPtr\r\nPrivate Declare PtrSafe Function SetClipboardData Lib \"user32.dll\" (ByVal wFormat As Long, ByVal hMem As LongPtr) As Long\r\nPrivate Declare PtrSafe Function GlobalAlloc Lib \"kernel32.dll\" (ByVal wFlags As Long, ByVal dwBytes As LongLong) As LongPtr\r\nPrivate Declare PtrSafe Function GlobalLock Lib \"kernel32.dll\" (ByVal hMem As LongPtr) As LongPtr\r\nPrivate Declare PtrSafe Function GlobalUnlock Lib \"kernel32.dll\" (ByVal hMem As LongPtr) As Long\r\nPrivate Declare PtrSafe Function GlobalSize Lib \"kernel32.dll\" (ByVal hMem As LongPtr) As LongLong\r\nPrivate Declare PtrSafe Sub MoveMemory Lib \"kernel32.dll\" Alias \"RtlMoveMemory\" (ByVal Destination As LongPtr, ByVal Source As LongPtr, ByVal Length As LongLong)\r\n#Else\r\nPrivate Declare Function OpenClipboard Lib \"user32.dll\" (ByVal hWnd As Long) As Long\r\nPrivate Declare Function EmptyClipboard Lib \"user32.dll\" () As Long\r\nPrivate Declare Function CloseClipboard Lib \"user32.dll\" () As Long\r\nPrivate Declare Function IsClipboardFormatAvailable Lib \"user32.dll\" (ByVal wFormat As Long) As Long\r\nPrivate Declare Function GetClipboardData Lib \"user32.dll\" (ByVal wFormat As Long) As Long\r\nPrivate Declare Function SetClipboardData Lib \"user32.dll\" (ByVal wFormat As Long, ByVal hMem As Long) As Long\r\nPrivate Declare Function GlobalAlloc Lib \"kernel32.dll\" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long\r\nPrivate Declare Function GlobalLock Lib \"kernel32.dll\" (ByVal hMem As Long) As Long\r\nPrivate Declare Function GlobalUnlock Lib \"kernel32.dll\" (ByVal hMem As Long) As Long\r\nPrivate Declare Function GlobalSize Lib \"kernel32.dll\" (ByVal hMem As Long) As Long\r\nPrivate Declare Function lstrcpy Lib \"kernel32.dll\" Alias \"lstrcpyW\" (ByVal lpString1 As Long, ByVal lpString2 As Long) As Long\r\nPrivate Declare Sub MoveMemory Lib \"kernel32.dll\" Alias \"RtlMoveMemory\" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)\r\n#End If\r\n\r\nPublic Sub SetClipboard(sUniText As String)\r\n#If VBA7 And Win64 Then\r\n    Dim iStrPtr As LongPtr\r\n    Dim iLen As LongLong\r\n    Dim iLock As LongPtr\r\n#Else\r\n    Dim iStrPtr As Long\r\n    Dim iLen As Long\r\n    Dim iLock As Long\r\n#End If\r\n    Const GMEM_MOVEABLE As Long = &H2\r\n    Const GMEM_ZEROINIT As Long = &H40\r\n    Const CF_UNICODETEXT As Long = &HD\r\n    OpenClipboard 0&\r\n    EmptyClipboard\r\n    iLen = LenB(sUniText)\r\n    iStrPtr = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, iLen + 2&)\r\n    iLock = GlobalLock(iStrPtr)\r\n    MoveMemory iLock, StrPtr(sUniText), iLen\r\n    GlobalUnlock iStrPtr\r\n    SetClipboardData CF_UNICODETEXT, iStrPtr\r\n    CloseClipboard\r\nEnd Sub\r\n\r\nPublic Function GetClipboard() As String\r\n#If VBA7 And Win64 Then\r\n    Dim iStrPtr As LongPtr\r\n    Dim iLen As LongLong\r\n    Dim iLock As LongPtr\r\n#Else\r\n    Dim iStrPtr As Long\r\n    Dim iLen As Long\r\n    Dim iLock As Long\r\n#End If\r\n    Dim sUniText As String\r\n    Const CF_UNICODETEXT As Long = 13&\r\n    OpenClipboard 0&\r\n    If IsClipboardFormatAvailable(CF_UNICODETEXT) Then\r\n        iStrPtr = GetClipboardData(CF_UNICODETEXT)\r\n        If iStrPtr Then\r\n            iLock = GlobalLock(iStrPtr)\r\n            iLen = GlobalSize(iStrPtr)\r\n            sUniText = String$(CLng(iLen) \\ 2& - 1&, vbNullChar)\r\n            MoveMemory StrPtr(sUniText), iLock, LenB(sUniText)\r\n            GlobalUnlock iStrPtr\r\n        End If\r\n        GetClipboard = sUniText\r\n    End If\r\n    CloseClipboard\r\nEnd Function\r\n\r\n'\r\n'\r\n<\/pre>\n<p><SPAN>\u3000\u3000<\/SPAN><\/p>\n<p><SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><br \/>\n<SPAN>\u3000\u3000<\/SPAN><\/p>\n","protected":false},"excerpt":{"rendered":"ExcelVBA \uff5e \u30af\u30ea\u30c3\u30d7\u30dc\u30fc\u30c9\u306b\u9001\u53d7\u4fe1\u3059\u308b\u305f\u3081\u306eAPI\uff0864bit\u300132bit\u3001\u517c\u7528\uff09 Excel\u306e\u300cDataObject\uff08Microsoft Forms 2.0 Object Library\uff09\u300d\u3092\u4f7f\u3046\u65b9\u6cd5\u304c\u3001 \u306a ...","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[234],"_links":{"self":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/posts\/1979"}],"collection":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/comments?post=1979"}],"version-history":[{"count":0,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/posts\/1979\/revisions"}],"wp:attachment":[{"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/media?parent=1979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/euc-access-excel-db.com\/tips\/wp-json\/wp\/v2\/categories?post=1979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}