Excel 2013 64 位 VBA:剪贴板 API 不起作用 [英] Excel 2013 64-bit VBA: Clipboard API doesn't work

查看:40
本文介绍了Excel 2013 64 位 VBA:剪贴板 API 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经能够在 Excel VBA 中使用 Windows API 调用来设置剪贴板上的文本.但是自从升级到 64 位 Office 2013 后,我就不能了.下面是一些没有错误的代码,但它也没有在剪贴板上设置任何文本.有人可以帮我测试和排除故障吗?

I used to be able to use Windows API calls in Excel VBA to set text on the clipboard. But ever since upgrading to 64-bit Office 2013, I cannot. Below is some code that does not error, but it is also not setting any text on the clipboard. Can someone help me test and troubleshoot?

将下面的代码粘贴到 VBA 中的代码模块后,您可以通过键入 Clipboard_SetData("Copy this to the clipboard.") 在即时窗口中测试它,它应该将该文本设置为剪贴板,您可以将其粘贴到任何其他应用程序中.

After pasting the code below into a code module in VBA, you can test it in the immediate windows by typing Clipboard_SetData("Copy this to the clipboard.") and it should set that text on the clipboard and you would be able to paste it into any other application.

(我使用的是 Windows 8,所以我无法使用 Microsoft Forms 或数据对象来操作剪贴板.它在 Windows 8 上无法正常工作.)

(I am using Windows 8, so I cannot use Microsoft Forms or the Data Object to manipulate the clipboard. It does not work properly on Windows 8.)

更新和下面的代码已更正,现在可以在 64 位 Excel 中正常工作,感谢 Jason Kurtz 在下面的回答.如果您觉得这有用,请为他的答案投票.

UPDATE and Code below has been corrected and now works properly in 64-bit Excel, thanks to Jason Kurtz' answer below. If you find this useful, please vote his answer up.

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

推荐答案

好的,我知道了...

您需要在您的代码版本中更改这一行:

You need to change this line in your version of the code:

Private Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As String, ByVal lpString2 As String) As LongPtr

为此:

Private Declare PtrSafe Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As LongPtr

如果您按原样单步执行代码,您将看到在调用 lstrcopy 时 lpGlobalMemory 的值发生了变化.当类型更改为 Any 时,值保持不变.

If you step through the code as you had it, you will see that the value of lpGlobalMemory changes when lstrcopy is called. When the types are changed to Any, the value stays the same.

在 Windows 7 上对我有用.希望它对你有用!

Works for me on windows 7. Hope it works for you!

这篇关于Excel 2013 64 位 VBA:剪贴板 API 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆