Excel 2013 64-bit-VBA: Clipboard-API funktioniert nicht

Ich verwendet, um in der Lage sein, die Verwendung von Windows-API-Aufrufe in Excel VBA text in die Zwischenablage zu kopieren. Aber seit dem Upgrade auf 64-bit-Office 2013, kann ich nicht. Nachfolgend finden Sie einige code, der nicht Fehler, aber es ist auch nicht jeder text in die Zwischenablage. Kann mir jemand helfen, testen und beheben?

Nach dem einfügen den folgenden code in ein code-Modul in VBA, können Sie es testen, in unmittelbarer windows, indem Sie Clipboard_SetData("Copy this to the clipboard.") und es sollte festlegen, dass text in der Zwischenablage, und Sie wäre in der Lage, fügen Sie es in eine andere Anwendung.

(Ich bin mit Windows 8, also kann ich nicht verwenden, Microsoft Forms-oder das Daten-Objekt zur Bearbeitung der Zwischenablage. Es funktioniert nicht richtig unter Windows 8.)

UPDATE und EDIT: Code unten wurde korrigiert und funktioniert jetzt in 64-bit-Excel, Dank Jason Kurtz' Antwort unten. Wenn Sie diese nützlich finden, bitte Stimmen Sie seine Antwort.

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
Hat die SetClipboardData() Aufruf erfolgreich? Wenn nicht, was bedeutet GetLastError() Bericht?
Habe es gerade ausprobiert. Clipboard_SetData("fjdkla;jfd") \ Debug-Ausgabe: \ hGlobalMemory ist 287253201176 \ lpGlobalMemory ist 287450358016 \ lpGlobalMemory ist 287362598488 \ hClipMemory ist 287253201176 \ LastDLLError-0 \ ist ich Frage mich, warum lstrcopy gibt eine andere Adresse, als GlobalLock. Ich untersuchte die lstrcopy API-Seite und Microsoft warnt uns, es nicht zu verwenden. Ich Frage mich, ob es deaktiviert werden, indem Sie irgendeine Art von Windows 8 Sicherheits-feature. Wer weiß, wie zu StringCchCopy in VBA?
Die genannte Datei 'win32api_ptrsafe.txt" kann jetzt heruntergeladen werden von "Office 2010-Hilfe-Dateien: Win32API_PtrSafe mit 64-bit-Unterstützung' (microsoft.com/en-us/download/details.aspx?id=9970)

InformationsquelleAutor Baodad | 2013-09-07

Schreibe einen Kommentar