vba Office 2013 Excel .PutInClipboard 有什么不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14738330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:30:46  来源:igfitidea点击:

Office 2013 Excel .PutInClipboard is Different?

excelvbams-officeclipboard

提问by Appraisal Guru

I've used a routine for years to put a plain text string into the clipboard that I can paste into another program such as:

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

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

When I use this in Excel Office 2013 the data isn't in the clipboard and therefore I can't paste it. This never happened in prior versions.

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

Under closer inspection I've found that the string does go to the clipboard but as "System String" but not as "Text" or "Unicode Text".

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

BUT... about 10% of the time it acutally works as it should putting the string into the clipboard as "Text".

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

Any ideas??

有任何想法吗??

回答by JohnB

user2140261's comment is the correct solution:

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

How to: Send Information to the Clipboard

如何:将信息发送到剪贴板

(The following is just copied from the above link)

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

If you need to copy the contents of the active control on a form or report to the Clipboard, you only need this code:

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

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

However, this the replacement you need for your old routine:

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

1. Create a module, name it "WinAPI" or something, put this code in it:

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. In the module where your old routine is defined, replace your old routine with this code:

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. Then, call it like this:

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")

回答by ALi_Mas

You should add "Microsoft forms 2.0 object library" to your references then you can use this feature. you can do this by going to Excel/developer/Visual basic(or your VBE module) and in the tools/references select browse and then in your system32 folder search for "FM20.DLL" file and then by selecting this file at the end of the references list "Microsoft forms 2.0 object library" will be appeared. Activate that . Now things are as good as old times ;)

您应该将“Microsoft forms 2.0 object library”添加到您的引用中,然后您就可以使用此功能。您可以通过转到 Excel/developer/Visual basic(或您的 VBE 模块)并在工具/参考中选择浏览,然后在您的 system32 文件夹中搜索“FM20.DLL”文件,然后在最后选择此文件来执行此操作将出现参考列表“Microsoft forms 2.0 object library”。激活那个。现在事情和过去一样好;)