我曾经可以在Excel VBA中使用Windows API调用来设置剪贴板上的文本。但自从升级到64位Office 2013后,我就不能了。下面是一些没有错误的代码,但它也没有在剪贴板上设置任何文本。有人能帮我测试和排除故障吗?
将下面的代码粘贴到VBA中的代码模块后,您可以通过键入Clipboard_SetData("Copy this to the clipboard.")
在即时窗口中测试它,它应该将该文本设置在剪贴板上,您可以将其粘贴到任何其他应用程序中。
(我使用的是Windows 8,所以我不能使用Microsoft Forms或数据对象来操作剪贴板。它在Windows 8上不能正常工作。)
**更新和编辑:**下面的代码已被更正,现在可以在64位Excel中正常工作,感谢Jason Kurtz的回答。如果你觉得这有用,请投票支持他的回答。
Option Explicit
'Found 64-bit API declarations here: http://spreadsheet1.com/uploads/3/0/6/6/3066620/win32api_ptrsafe.txt
Private Declare PtrSafe Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalFree Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As Long
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
Private Declare PtrSafe Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr
Private Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096
Sub ClipBoard_SetData(MyString As String)
'32-bit code by Microsoft: http://msdn.microsoft.com/en-us/library/office/ff192913.aspx
Dim hGlobalMemory As LongPtr, lpGlobalMemory As LongPtr
Dim hClipMemory As LongPtr, X As Long
' Allocate moveable global memory.
hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)
' Lock the block to get a far pointer to this memory.
lpGlobalMemory = GlobalLock(hGlobalMemory)
' Copy the string to this global memory.
lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)
' Unlock the memory.
If GlobalUnlock(hGlobalMemory) <> 0 Then
MsgBox "Could not unlock memory location. Copy aborted."
'Debug.Print "GlobalFree returned: " & CStr(GlobalFree(hGlobalMemory))
GoTo OutOfHere
End If
' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then
MsgBox "Could not open the Clipboard. Copy aborted."
Exit Sub
End If
' Clear the Clipboard.
X = EmptyClipboard()
' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
OutOfHere:
If CloseClipboard() = 0 Then
MsgBox "Could not close Clipboard."
End If
End Sub
4条答案
按热度按时间2izufjch1#
好,我知道了......
您需要在您的代码版本中更改此行:
对此:
如果您按原样单步执行代码,您将看到在调用lstrcopy时lpGlobalMemory的值发生了变化。当类型更改为Any时,该值保持不变。
在Windows 7上对我有效。希望对你有效!
eanckbw92#
发布完整的代码为他人.测试和工作的32位版本的Excel 2007,2010,2013,2016和64位Excel 2013所有运行在Windows 10
vulvrdjw3#
我知道这个问题现在已经解决了,但是我更喜欢这种****简单得多的方法,它可以独立于体系结构工作,而且我喜欢用一个函数来读写剪贴板的方法。
ffx8fchx4#
完全按照如下所示使用代码:
http://msdn.microsoft.com/en-us/library/office/ff192913.aspx
除了在所有API声明的Declare之后插入PtrSafe。
代码本身应该位于模块中。
就像这样: