gpt4 book ai didi

excel - Office 2013 Excel .PutInClipboard 不同吗?

转载 作者:行者123 更新时间:2023-12-01 19:49:34 26 4
gpt4 key购买 nike

多年来我一直使用一个例程将纯文本字符串放入剪贴板,我可以将其粘贴到另一个程序中,例如:

targetData.SetText "This is a plain text string"
targetData.PutInClipboard

当我在 Excel Office 2013 中使用此功能时,数据不在剪贴板中,因此我无法粘贴它。这在以前的版本中从未发生过。

经过仔细检查,我发现该字符串确实进入剪贴板,但作为“系统字符串”而不是“文本”或“Unicode 文本”。

但是...大约 10% 的时间它会实际工作,因为它应该将字符串作为“文本”放入剪贴板。

有什么想法吗?

最佳答案

user2140261的评论是正确的解决方案:

How to: Send Information to the Clipboard

(以下内容只是从上面的链接复制而来)

如果需要将窗体或报表上事件控件的内容复制到剪贴板,则只需要以下代码:

Private Sub cmdCopy_Click() 
Me!txtNotes.SetFocus
DoCmd.RunCommand acCmdCopy
End Sub

但是,这是您旧例程所需的替代品:

<强>1。创建一个模块,将其命名为“WinAPI”或其他名称,将以下代码放入其中:

Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

<强>2。在定义旧例程的模块中,用以下代码替换旧例程:

Function ClipBoard_SetData(MyString As String) 
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long, 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."
GoTo OutOfHere2
End If

' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then
MsgBox "Could not open the Clipboard. Copy aborted."
Exit Function
End If

' Clear the Clipboard.
X = EmptyClipboard()

' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

OutOfHere2:

If CloseClipboard() = 0 Then
MsgBox "Could not close Clipboard."
End If

End Function

<强>3。然后,这样调用它:

' doesn't work on Windows 8: targetData.SetText "This is a plain text string"
'doesn't work on Windows 8: targetData.PutInClipboard
ClipBoard_SetData ("This is a plain text string")

关于excel - Office 2013 Excel .PutInClipboard 不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14738330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com